@amodx/ncs 0.0.2 → 0.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@ import { ContextRegisterData } from "./Context.types";
2
2
  import { ContextArray } from "./ContextArray";
3
3
  import { NodeCursor } from "../Nodes/NodeCursor";
4
4
  import { SchemaCursor } from "../Schema/Schema.types";
5
+ import { Graph } from "../Graphs/Graph";
5
6
  export declare class ContextCursor<ContextSchema extends {} = {}, Data extends {} = {}> {
6
7
  static Get(): ContextCursor<{}, {}>;
7
8
  static Retrun(cursor: ContextCursor): boolean;
@@ -10,9 +11,12 @@ export declare class ContextCursor<ContextSchema extends {} = {}, Data extends {
10
11
  _type: number;
11
12
  get index(): number;
12
13
  get data(): Data;
14
+ set data(data: Data);
13
15
  schema: SchemaCursor<ContextSchema>;
14
16
  get nodes(): number[];
15
17
  get type(): string;
18
+ private _graph;
19
+ get graph(): Graph;
16
20
  arrays: ContextArray;
17
21
  private constructor();
18
22
  setContext(node: NodeCursor, index: number): void;
@@ -19,6 +19,9 @@ export class ContextCursor {
19
19
  get data() {
20
20
  return this.arrays._data[this._index];
21
21
  }
22
+ set data(data) {
23
+ this.arrays._data[this._index] = data;
24
+ }
22
25
  schema;
23
26
  get nodes() {
24
27
  return this.arrays._node[this._index];
@@ -26,11 +29,16 @@ export class ContextCursor {
26
29
  get type() {
27
30
  return NCSRegister.contexts.idPalette.getStringId(this.arrays._type[this._index]);
28
31
  }
32
+ _graph;
33
+ get graph() {
34
+ return this._graph;
35
+ }
29
36
  arrays;
30
37
  constructor() { }
31
38
  setContext(node, index) {
32
39
  this._index = index;
33
40
  this.arrays = node.graph._contexts;
41
+ this._graph = node.graph;
34
42
  this._type = this.arrays._type[index];
35
43
  }
36
44
  dispose() {
package/NCS.d.ts CHANGED
@@ -35,12 +35,12 @@ export declare const NCS: {
35
35
  getComponents(grpah: Graph): Generator<import(".").ComponentCursor<ComponentSchema, Data, Shared>, any, any>;
36
36
  set(node: import(".").NodeCursor, componentSchema?: import("./Util/Util.types").Nullable<Partial<ComponentSchema>> | undefined, schemaCursor?: import("./Util/Util.types").Nullable<import("./Schema/Schema.types").SchemaCursor<ComponentSchema>> | undefined, cursor?: import(".").ComponentCursor<ComponentSchema, Data, Shared> | undefined): import(".").ComponentCursor<ComponentSchema, Data, Shared>;
37
37
  has(node: import(".").NodeCursor): boolean;
38
- get(node: import(".").NodeCursor, cursor?: import(".").ComponentCursor<ComponentSchema, Data, Shared> | undefined): import(".").ComponentCursor<ComponentSchema, Data, Shared> | null;
39
- getRequired(node: import(".").NodeCursor, cursor?: import(".").ComponentCursor<ComponentSchema, Data, Shared> | undefined): import(".").ComponentCursor<ComponentSchema, Data, Shared>;
40
- getChild(node: import(".").NodeCursor, cursor?: import(".").ComponentCursor<ComponentSchema, Data, Shared> | undefined): import(".").ComponentCursor<ComponentSchema, Data, Shared> | null;
41
- getRequiredChild(node: import(".").NodeCursor, cursor?: import(".").ComponentCursor<ComponentSchema, Data, Shared> | undefined): import(".").ComponentCursor<ComponentSchema, Data, Shared>;
42
- getParent(node: import(".").NodeCursor, cursor?: import(".").ComponentCursor<ComponentSchema, Data, Shared> | undefined): import(".").ComponentCursor<ComponentSchema, Data, Shared> | null;
43
- getRequiredParent(node: import(".").NodeCursor, cursor?: import(".").ComponentCursor<ComponentSchema, Data, Shared> | undefined): import(".").ComponentCursor<ComponentSchema, Data, Shared>;
38
+ get(node: import(".").NodeCursor, cursor?: import(".").ComponentCursor<ComponentSchema, Data, Shared> | undefined, nodeCursor?: import(".").NodeCursor): import(".").ComponentCursor<ComponentSchema, Data, Shared> | null;
39
+ getRequired(node: import(".").NodeCursor, cursor?: import(".").ComponentCursor<ComponentSchema, Data, Shared> | undefined, nodeCursor?: import(".").NodeCursor): import(".").ComponentCursor<ComponentSchema, Data, Shared>;
40
+ getChild(node: import(".").NodeCursor, cursor?: import(".").ComponentCursor<ComponentSchema, Data, Shared> | undefined, nodeCursor?: import(".").NodeCursor): import(".").ComponentCursor<ComponentSchema, Data, Shared> | null;
41
+ getRequiredChild(node: import(".").NodeCursor, cursor?: import(".").ComponentCursor<ComponentSchema, Data, Shared> | undefined, nodeCursor?: import(".").NodeCursor): import(".").ComponentCursor<ComponentSchema, Data, Shared>;
42
+ getParent(node: import(".").NodeCursor, cursor?: import(".").ComponentCursor<ComponentSchema, Data, Shared> | undefined, nodeCursor?: import(".").NodeCursor): import(".").ComponentCursor<ComponentSchema, Data, Shared> | null;
43
+ getRequiredParent(node: import(".").NodeCursor, cursor?: import(".").ComponentCursor<ComponentSchema, Data, Shared> | undefined, nodeCursor?: import(".").NodeCursor): import(".").ComponentCursor<ComponentSchema, Data, Shared>;
44
44
  getAll(node: import(".").NodeCursor): import(".").ComponentCursor<ComponentSchema, Data, Shared>[] | null;
45
45
  remove(node: import(".").NodeCursor): boolean;
46
46
  removeAll(node: import(".").NodeCursor): boolean;
package/NCS.js CHANGED
@@ -12,33 +12,39 @@ import { deserializeComponentData } from "./Data/deserializeComponent";
12
12
  import { cloneNode, copyNode, createRemoteNode, serializeNode, serializeNodeData, } from "./Data/serializeNode";
13
13
  import { deserializeNodeData } from "./Data/deserializeNode";
14
14
  import { SchemaProperty, } from "./Schema/Schema.types";
15
- const traverseCreateSchema = (object, parent) => {
16
- for (const key in object) {
17
- parent.children ??= [];
18
- const value = object[key];
19
- if (value instanceof SchemaProperty) {
20
- if (value.value instanceof SchemaProperty &&
21
- typeof value.value.value == "object") {
22
- const newParent = {
23
- id: key,
24
- meta: value.meta,
25
- value: value.value,
26
- children: [],
27
- };
28
- parent.children.push(newParent);
29
- traverseCreateSchema(value.value.value, newParent);
30
- }
31
- else {
32
- parent.children.push({
33
- id: key,
34
- meta: value.meta,
35
- value: value.value,
36
- });
37
- }
15
+ function traverseCreateSchema(obj, parent) {
16
+ parent.children ??= [];
17
+ for (const key in obj) {
18
+ const schemaProp = obj[key];
19
+ // If this isn't a SchemaProperty, either skip or handle differently
20
+ if (!(schemaProp instanceof SchemaProperty)) {
21
+ continue;
22
+ }
23
+ // If the SchemaProperty's value is itself an object, we treat it as a parent
24
+ if (schemaProp.value !== null &&
25
+ typeof schemaProp.value === "object" &&
26
+ !Array.isArray(schemaProp.value)) {
27
+ // Make a new "parent" property data
28
+ const newParent = {
29
+ id: key,
30
+ meta: schemaProp.meta,
31
+ value: schemaProp.value,
32
+ };
33
+ parent.children.push(newParent);
34
+ // Recurse into the object’s own keys (which themselves may be SchemaProperties)
35
+ traverseCreateSchema(schemaProp.value, newParent);
36
+ }
37
+ else {
38
+ // Otherwise it's a leaf (number, boolean, string, etc.)
39
+ parent.children.push({
40
+ id: key,
41
+ meta: schemaProp.meta,
42
+ value: schemaProp.value,
43
+ });
38
44
  }
39
45
  }
40
46
  return parent;
41
- };
47
+ }
42
48
  export const NCS = {
43
49
  /** Create a graph. */
44
50
  createGraph() {
@@ -60,11 +66,12 @@ export const NCS = {
60
66
  },
61
67
  /** Create a schema for a component or context. */
62
68
  schema(schema, views) {
63
- const s = new Schema(traverseCreateSchema(schema, {
69
+ const data = traverseCreateSchema(schema, {
64
70
  id: "root",
65
71
  value: {},
66
72
  children: [],
67
- }).children);
73
+ }).children;
74
+ const s = new Schema(data);
68
75
  if (views) {
69
76
  for (const view of views) {
70
77
  s.createView(view);
@@ -11,9 +11,9 @@ export declare class NodeComponents {
11
11
  add(comp: CreateComponentData): number;
12
12
  remove(type: string): true | undefined;
13
13
  has(type: string): boolean;
14
- get(type: string, cursor?: ComponentCursor<{}, any, any>): ComponentCursor<any, any, any> | null;
14
+ get(type: string, cursor?: ComponentCursor<{}, any, any>, nodeCursor?: NodeCursor): ComponentCursor<any, any, any> | null;
15
15
  getAll(type: string): ComponentCursor<any, any, any>[];
16
16
  removeAll(type: string): boolean;
17
- getChild(type: string, cursor?: ComponentCursor<{}, any, any>): ComponentCursor<any, any, any> | null;
18
- getParent(type: string, cursor?: ComponentCursor<{}, any, any>): ComponentCursor<any, any, any> | null;
17
+ getChild(type: string, cursor?: ComponentCursor<{}, any, any>, nodeCursor?: NodeCursor): ComponentCursor<any, any, any> | null;
18
+ getParent(type: string, cursor?: ComponentCursor<{}, any, any>, nodeCursor?: NodeCursor): ComponentCursor<any, any, any> | null;
19
19
  }
@@ -1,4 +1,5 @@
1
1
  import { ComponentCursor } from "../Components/ComponentCursor";
2
+ import { NodeCursor } from "./NodeCursor";
2
3
  import { NCSRegister } from "../Register/NCSRegister";
3
4
  import { ComponentArray } from "../Components/ComponentArray";
4
5
  import { NCSPools } from "../Pools/NCSPools";
@@ -110,14 +111,16 @@ export class NodeComponents {
110
111
  }
111
112
  return false;
112
113
  }
113
- get(type, cursor = ComponentCursor.Get()) {
114
+ get(type, cursor = ComponentCursor.Get(), nodeCursor = NodeCursor.Get()) {
114
115
  const components = this.components;
115
116
  if (!components)
116
117
  return null;
117
118
  const numberId = NCSRegister.components.idPalette.getNumberId(type);
118
119
  for (let i = 0; i < components.length; i += 2) {
119
120
  if (components[i] == numberId) {
120
- cursor.setInstance(this.node, numberId, components[i + 1]);
121
+ if (nodeCursor !== this.node)
122
+ this.node.cloneCursor(nodeCursor);
123
+ cursor.setInstance(nodeCursor, numberId, components[i + 1]);
121
124
  return cursor;
122
125
  }
123
126
  }
@@ -157,21 +160,21 @@ export class NodeComponents {
157
160
  }
158
161
  return true;
159
162
  }
160
- getChild(type, cursor = ComponentCursor.Get()) {
163
+ getChild(type, cursor = ComponentCursor.Get(), nodeCursor = NodeCursor.Get()) {
161
164
  for (const child of this.node.traverseChildren()) {
162
165
  if (!child.components)
163
166
  continue;
164
- const found = child.components.get(type, cursor);
167
+ const found = child.components.get(type, cursor, nodeCursor);
165
168
  if (found)
166
169
  return found;
167
170
  }
168
171
  return null;
169
172
  }
170
- getParent(type, cursor = ComponentCursor.Get()) {
173
+ getParent(type, cursor = ComponentCursor.Get(), nodeCursor = NodeCursor.Get()) {
171
174
  for (const parent of this.node.traverseParents()) {
172
175
  if (!parent.components)
173
176
  continue;
174
- const found = parent.components.get(type, cursor);
177
+ const found = parent.components.get(type, cursor, nodeCursor);
175
178
  if (found)
176
179
  return found;
177
180
  }
@@ -15,7 +15,9 @@ export declare class NodeEvents {
15
15
  private constructor();
16
16
  hasListener(id: string): boolean;
17
17
  clearListeners(id: string): false | undefined;
18
- addListener<Data>(id: string, run: (data: Data) => void): void;
18
+ addListener<Data>(id: string, run: (data: Data) => void, options?: {
19
+ once?: boolean;
20
+ }): void;
19
21
  removeListener(id: string, run: ObservableFunction<any>): boolean;
20
22
  dispatch<Data>(id: string, data: Data): boolean;
21
23
  dispatchDeep<Data>(id: string, data: Data): void;
@@ -41,7 +41,7 @@ export class NodeEvents {
41
41
  NCSPools.observers.addItem(observer);
42
42
  this.node.arrays._events[numberId][this.index] = undefined;
43
43
  }
44
- addListener(id, run) {
44
+ addListener(id, run, options) {
45
45
  const numberId = this.node.arrays._eventPalette.isRegistered(id)
46
46
  ? this.node.arrays._eventPalette.getNumberId(id)
47
47
  : this.node.arrays._eventPalette.register(id);
@@ -55,7 +55,12 @@ export class NodeEvents {
55
55
  observer = NCSPools.observers.get() || new Observable();
56
56
  observers[this.index] = observer;
57
57
  }
58
- observer.subscribe(run);
58
+ if (options?.once) {
59
+ observer.subscribeOnce(run);
60
+ }
61
+ else {
62
+ observer.subscribe(run);
63
+ }
59
64
  }
60
65
  removeListener(id, run) {
61
66
  const numberId = this.node.arrays._eventPalette.getNumberId(id);
@@ -10,12 +10,12 @@ type RegisteredComponent<ComponentSchema extends object = any, Data extends any
10
10
  getComponents(grpah: Graph): Generator<ComponentCursor<ComponentSchema, Data, Shared>>;
11
11
  set(node: NodeCursor, componentSchema?: Nullable<Partial<ComponentSchema>>, schemaCursor?: Nullable<SchemaCursor<ComponentSchema>>, cursor?: ComponentCursor<ComponentSchema, Data, Shared>): ComponentCursor<ComponentSchema, Data, Shared>;
12
12
  has(node: NodeCursor): boolean;
13
- get(node: NodeCursor, cursor?: ComponentCursor<ComponentSchema, Data, Shared>): ComponentCursor<ComponentSchema, Data, Shared> | null;
14
- getRequired(node: NodeCursor, cursor?: ComponentCursor<ComponentSchema, Data, Shared>): ComponentCursor<ComponentSchema, Data, Shared>;
15
- getChild(node: NodeCursor, cursor?: ComponentCursor<ComponentSchema, Data, Shared>): ComponentCursor<ComponentSchema, Data, Shared> | null;
16
- getRequiredChild(node: NodeCursor, cursor?: ComponentCursor<ComponentSchema, Data, Shared>): ComponentCursor<ComponentSchema, Data, Shared>;
17
- getParent(node: NodeCursor, cursor?: ComponentCursor<ComponentSchema, Data, Shared>): ComponentCursor<ComponentSchema, Data, Shared> | null;
18
- getRequiredParent(node: NodeCursor, cursor?: ComponentCursor<ComponentSchema, Data, Shared>): ComponentCursor<ComponentSchema, Data, Shared>;
13
+ get(node: NodeCursor, cursor?: ComponentCursor<ComponentSchema, Data, Shared>, nodeCursor?: NodeCursor): ComponentCursor<ComponentSchema, Data, Shared> | null;
14
+ getRequired(node: NodeCursor, cursor?: ComponentCursor<ComponentSchema, Data, Shared>, nodeCursor?: NodeCursor): ComponentCursor<ComponentSchema, Data, Shared>;
15
+ getChild(node: NodeCursor, cursor?: ComponentCursor<ComponentSchema, Data, Shared>, nodeCursor?: NodeCursor): ComponentCursor<ComponentSchema, Data, Shared> | null;
16
+ getRequiredChild(node: NodeCursor, cursor?: ComponentCursor<ComponentSchema, Data, Shared>, nodeCursor?: NodeCursor): ComponentCursor<ComponentSchema, Data, Shared>;
17
+ getParent(node: NodeCursor, cursor?: ComponentCursor<ComponentSchema, Data, Shared>, nodeCursor?: NodeCursor): ComponentCursor<ComponentSchema, Data, Shared> | null;
18
+ getRequiredParent(node: NodeCursor, cursor?: ComponentCursor<ComponentSchema, Data, Shared>, nodeCursor?: NodeCursor): ComponentCursor<ComponentSchema, Data, Shared>;
19
19
  getAll(node: NodeCursor): ComponentCursor<ComponentSchema, Data, Shared>[] | null;
20
20
  remove(node: NodeCursor): boolean;
21
21
  removeAll(node: NodeCursor): boolean;
@@ -49,29 +49,29 @@ export const registerComponent = (data) => {
49
49
  has(node) {
50
50
  return node.components.has(data.type);
51
51
  },
52
- get(node, cursor) {
53
- return node.components.get(data.type, cursor);
52
+ get(node, cursor, nodeCursor) {
53
+ return node.components.get(data.type, cursor, nodeCursor);
54
54
  },
55
- getRequired(node, cursor) {
56
- const found = node.components.get(data.type, cursor);
55
+ getRequired(node, cursor, nodeCursor) {
56
+ const found = node.components.get(data.type, cursor, nodeCursor);
57
57
  if (!found)
58
58
  throw new Error(`Node [${node.name}] does not have required component [${data.type}].`);
59
59
  return found;
60
60
  },
61
- getChild(node, cursor) {
62
- return node.components.getChild(data.type, cursor);
61
+ getChild(node, cursor, nodeCursor) {
62
+ return node.components.getChild(data.type, cursor, nodeCursor);
63
63
  },
64
- getRequiredChild(node, cursor) {
65
- const comp = node.components.getChild(data.type, cursor);
64
+ getRequiredChild(node, cursor, nodeCursor) {
65
+ const comp = node.components.getChild(data.type, cursor, nodeCursor);
66
66
  if (!comp)
67
67
  throw new Error(`Node [${node.name}] does not have a child with required component [${data.type}].`);
68
68
  return comp;
69
69
  },
70
- getParent(node, cursor) {
71
- return node.components.getParent(data.type, cursor);
70
+ getParent(node, cursor, nodeCursor) {
71
+ return node.components.getParent(data.type, cursor, nodeCursor);
72
72
  },
73
- getRequiredParent(node, cursor = ComponentCursor.Get()) {
74
- const comp = node.components.getParent(data.type, cursor);
73
+ getRequiredParent(node, cursor, nodeCursor) {
74
+ const comp = node.components.getParent(data.type, cursor, nodeCursor);
75
75
  if (!comp)
76
76
  throw new Error(`Node [${node.name}] does not have a parent with required component [${data.type}].`);
77
77
  return comp;
@@ -1,4 +1,5 @@
1
1
  import { NCSRegister } from "./NCSRegister";
2
+ import { ContextCursor } from "../Contexts/ContextCursor";
2
3
  import { NCSPools } from "../Pools/NCSPools";
3
4
  export function registerContext(data) {
4
5
  NCSRegister.contexts.register(data.type, data);
@@ -12,9 +13,13 @@ export function registerContext(data) {
12
13
  };
13
14
  return Object.assign(createContext, data, {
14
15
  data,
15
- set: (parent, schema, schemaViewId, data) => {
16
+ set: (parent, schema, schemaViewId, data, cursor = ContextCursor.Get()) => {
16
17
  const newContext = parent.context.add(createContext(schema, schemaViewId, data));
17
- return newContext;
18
+ if (data) {
19
+ parent.graph._contexts._data[newContext] = data;
20
+ }
21
+ cursor.setContext(parent, newContext);
22
+ return cursor;
18
23
  },
19
24
  get: (parent) => {
20
25
  return parent.context.get(data.type);
package/Schema/Schema.js CHANGED
@@ -11,7 +11,7 @@ const traverseCreate = (parent, properties, index) => {
11
11
  parent.children ??= [];
12
12
  for (let i = 0; i < properties.length; i++) {
13
13
  const data = properties[i];
14
- if (typeof data.value == "object" && !data.children) {
14
+ if (typeof data.value == "object" && !data.children?.length) {
15
15
  data.children ??= [];
16
16
  for (const key in data.value) {
17
17
  const value = data.value[key];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amodx/ncs",
3
- "version": "0.0.02",
3
+ "version": "0.0.21",
4
4
  "module": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "type": "module",