@amodx/ncs 0.0.1 → 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.
Files changed (181) hide show
  1. package/Components/Component.types.d.ts +78 -0
  2. package/Components/ComponentArray.d.ts +30 -0
  3. package/Components/ComponentArray.js +82 -0
  4. package/Components/ComponentCursor.d.ts +27 -0
  5. package/Components/ComponentCursor.js +72 -0
  6. package/Contexts/Context.types.d.ts +76 -0
  7. package/Contexts/ContextArray.d.ts +14 -0
  8. package/Contexts/ContextArray.js +47 -0
  9. package/Contexts/ContextCursor.d.ts +24 -0
  10. package/Contexts/ContextCursor.js +47 -0
  11. package/Data/SerializedData.types.d.ts +44 -0
  12. package/Data/deserializeComponent.d.ts +3 -0
  13. package/Data/deserializeComponent.js +3 -0
  14. package/Data/deserializeNode.d.ts +3 -0
  15. package/Data/deserializeNode.js +34 -0
  16. package/Data/serializeComponent.d.ts +7 -0
  17. package/Data/serializeComponent.js +36 -0
  18. package/Data/serializeNode.d.ts +13 -0
  19. package/Data/serializeNode.js +83 -0
  20. package/Functional.d.ts +12 -0
  21. package/Functional.js +31 -0
  22. package/Graphs/Graph.d.ts +19 -21
  23. package/Graphs/Graph.js +85 -74
  24. package/NCS.d.ts +79 -48
  25. package/NCS.js +115 -32
  26. package/Nodes/Node.types.d.ts +39 -0
  27. package/Nodes/Node.types.js +16 -0
  28. package/Nodes/NodeArray.d.ts +25 -0
  29. package/Nodes/NodeArray.js +96 -0
  30. package/Nodes/NodeComponents.d.ts +19 -0
  31. package/Nodes/NodeComponents.js +183 -0
  32. package/Nodes/NodeContext.d.ts +14 -0
  33. package/Nodes/NodeContext.js +107 -0
  34. package/Nodes/NodeCursor.d.ts +65 -0
  35. package/Nodes/NodeCursor.js +348 -0
  36. package/Nodes/NodeEvents.d.ts +13 -14
  37. package/Nodes/NodeEvents.js +58 -28
  38. package/Nodes/NodeId.d.ts +4 -13
  39. package/Nodes/NodeId.js +16 -48
  40. package/Nodes/NodeObservers.d.ts +35 -20
  41. package/Nodes/NodeObservers.js +162 -37
  42. package/Nodes/NodeTags.d.ts +17 -0
  43. package/Nodes/NodeTags.js +102 -0
  44. package/Pools/NCSPools.d.ts +27 -4
  45. package/Pools/NCSPools.js +14 -2
  46. package/Queries/Query.types.d.ts +8 -0
  47. package/Queries/QueryInstance.d.ts +6 -4
  48. package/Queries/QueryInstance.js +89 -44
  49. package/Queries/QueryPrototype.d.ts +1 -1
  50. package/Register/NCSRegister.d.ts +17 -9
  51. package/Register/NCSRegister.js +25 -2
  52. package/Register/registerComponent.d.ts +31 -25
  53. package/Register/registerComponent.js +88 -36
  54. package/Register/registerContext.d.ts +10 -13
  55. package/Register/registerContext.js +16 -16
  56. package/Register/registerSystem.d.ts +1 -1
  57. package/Register/registerSystem.js +1 -1
  58. package/Register/registerTag.d.ts +15 -17
  59. package/Register/registerTag.js +20 -23
  60. package/Schema/Functions/createBaseSchemaCursor.d.ts +18 -0
  61. package/Schema/Functions/createBaseSchemaCursor.js +34 -0
  62. package/Schema/Functions/createSchemaBinaryObjectCursorClass.d.ts +22 -0
  63. package/Schema/Functions/createSchemaBinaryObjectCursorClass.js +134 -0
  64. package/Schema/Functions/createSchemaIndex.d.ts +3 -0
  65. package/Schema/Functions/createSchemaIndex.js +18 -0
  66. package/Schema/Functions/createSchemaObjectCursorClass.d.ts +18 -0
  67. package/Schema/Functions/createSchemaObjectCursorClass.js +66 -0
  68. package/Schema/Functions/createSchemaTypedArrayCursorClass.d.ts +17 -0
  69. package/Schema/Functions/createSchemaTypedArrayCursorClass.js +66 -0
  70. package/Schema/Property/Property.d.ts +11 -0
  71. package/Schema/Property/Property.js +16 -0
  72. package/Schema/Property/Property.types.d.ts +26 -0
  73. package/Schema/Property/Property.types.js +20 -0
  74. package/Schema/Schema.d.ts +28 -0
  75. package/Schema/Schema.js +223 -0
  76. package/Schema/Schema.types.d.ts +59 -0
  77. package/Schema/Schema.types.js +8 -0
  78. package/Schema/SchemaArray.d.ts +23 -0
  79. package/Schema/SchemaArray.js +87 -0
  80. package/Schema/SchemaArrayCursor.d.ts +19 -0
  81. package/Schema/SchemaArrayCursor.js +56 -0
  82. package/Schema/SchemaView.d.ts +24 -0
  83. package/Schema/SchemaView.js +141 -0
  84. package/Systems/{SystemData.d.ts → System.types.d.ts} +0 -4
  85. package/Systems/SystemInstance.d.ts +6 -4
  86. package/Systems/SystemInstance.js +14 -8
  87. package/Systems/SystemPrototype.d.ts +1 -1
  88. package/Systems/SystemPrototype.js +0 -1
  89. package/Tags/Tag.d.ts +1 -1
  90. package/Tags/Tag.js +1 -1
  91. package/Tags/{TagData.d.ts → Tag.types.d.ts} +0 -17
  92. package/Tags/TagArray.d.ts +18 -0
  93. package/Tags/TagArray.js +37 -0
  94. package/Tags/TagCursor.d.ts +20 -0
  95. package/Tags/TagCursor.js +45 -0
  96. package/Util/IdPalette.d.ts +13 -0
  97. package/Util/IdPalette.js +38 -0
  98. package/{Pools → Util}/ItemPool.d.ts +2 -3
  99. package/{Pools → Util}/ItemPool.js +2 -4
  100. package/Util/Observable.d.ts +28 -0
  101. package/Util/Observable.js +58 -0
  102. package/Util/Util.types.d.ts +4 -0
  103. package/index.d.ts +16 -6
  104. package/index.js +24 -6
  105. package/package.json +1 -1
  106. package/Components/ComponentData.d.ts +0 -112
  107. package/Components/ComponentInstance.d.ts +0 -36
  108. package/Components/ComponentInstance.js +0 -125
  109. package/Components/ComponentInstanceMap.d.ts +0 -7
  110. package/Components/ComponentInstanceMap.js +0 -15
  111. package/Components/ComponentObservers.d.ts +0 -8
  112. package/Components/ComponentObservers.js +0 -13
  113. package/Components/ComponentPipelines.d.ts +0 -16
  114. package/Components/ComponentPipelines.js +0 -33
  115. package/Components/ComponentPrototype.d.ts +0 -17
  116. package/Components/ComponentPrototype.js +0 -73
  117. package/Contexts/ContextData.d.ts +0 -59
  118. package/Contexts/ContextInstance.d.ts +0 -14
  119. package/Contexts/ContextInstance.js +0 -26
  120. package/Contexts/ContextInstanceAnchor.d.ts +0 -15
  121. package/Contexts/ContextInstanceAnchor.js +0 -33
  122. package/Contexts/ContextPrototype.d.ts +0 -9
  123. package/Contexts/ContextPrototype.js +0 -19
  124. package/Graphs/GraphEvents.d.ts +0 -9
  125. package/Graphs/GraphEvents.js +0 -35
  126. package/Graphs/GraphObservers.d.ts +0 -9
  127. package/Graphs/GraphObservers.js +0 -6
  128. package/Graphs/GraphUpdate.d.ts +0 -13
  129. package/Graphs/GraphUpdate.js +0 -38
  130. package/Maps/ItemGraphMap.d.ts +0 -11
  131. package/Maps/ItemGraphMap.js +0 -30
  132. package/Maps/ItemNodeMap.d.ts +0 -17
  133. package/Maps/ItemNodeMap.js +0 -41
  134. package/Nodes/Components/NodeComponentObservers.d.ts +0 -15
  135. package/Nodes/Components/NodeComponentObservers.js +0 -33
  136. package/Nodes/Components/NodeComponents.d.ts +0 -22
  137. package/Nodes/Components/NodeComponents.js +0 -107
  138. package/Nodes/Context/NodeContext.d.ts +0 -13
  139. package/Nodes/Context/NodeContext.js +0 -45
  140. package/Nodes/NodeData.d.ts +0 -40
  141. package/Nodes/NodeInstance.d.ts +0 -50
  142. package/Nodes/NodeInstance.js +0 -202
  143. package/Nodes/NodePipelines.d.ts +0 -16
  144. package/Nodes/NodePipelines.js +0 -33
  145. package/Nodes/Tags/NodeTagObservers.d.ts +0 -15
  146. package/Nodes/Tags/NodeTagObservers.js +0 -33
  147. package/Nodes/Tags/NodeTags.d.ts +0 -21
  148. package/Nodes/Tags/NodeTags.js +0 -95
  149. package/Queries/QueryData.d.ts +0 -8
  150. package/Register/ItemRegister.d.ts +0 -7
  151. package/Register/ItemRegister.js +0 -24
  152. package/Register/registerTrait.d.ts +0 -15
  153. package/Register/registerTrait.js +0 -39
  154. package/Tags/TagInstance.d.ts +0 -13
  155. package/Tags/TagInstance.js +0 -17
  156. package/Tags/TagInstanceMap.d.ts +0 -7
  157. package/Tags/TagInstanceMap.js +0 -15
  158. package/Tags/TagPrototype.d.ts +0 -16
  159. package/Tags/TagPrototype.js +0 -34
  160. package/Traits/TraitContaienrObservers.d.ts +0 -15
  161. package/Traits/TraitContaienrObservers.js +0 -33
  162. package/Traits/TraitContainer.d.ts +0 -22
  163. package/Traits/TraitContainer.js +0 -114
  164. package/Traits/TraitData.d.ts +0 -106
  165. package/Traits/TraitInstance.d.ts +0 -35
  166. package/Traits/TraitInstance.js +0 -115
  167. package/Traits/TraitInstanceMap.d.ts +0 -7
  168. package/Traits/TraitInstanceMap.js +0 -15
  169. package/Traits/TraitObservers.d.ts +0 -8
  170. package/Traits/TraitObservers.js +0 -13
  171. package/Traits/TraitPipelines.d.ts +0 -16
  172. package/Traits/TraitPipelines.js +0 -33
  173. package/Traits/TraitPrototype.d.ts +0 -16
  174. package/Traits/TraitPrototype.js +0 -74
  175. /package/Components/{ComponentData.js → Component.types.js} +0 -0
  176. /package/Contexts/{ContextData.js → Context.types.js} +0 -0
  177. /package/{Nodes/NodeData.js → Data/SerializedData.types.js} +0 -0
  178. /package/Queries/{QueryData.js → Query.types.js} +0 -0
  179. /package/Systems/{SystemData.js → System.types.js} +0 -0
  180. /package/Tags/{TagData.js → Tag.types.js} +0 -0
  181. /package/{Traits/TraitData.js → Util/Util.types.js} +0 -0
@@ -0,0 +1,83 @@
1
+ import { NodeId } from "../Nodes/NodeId";
2
+ import { createRemoteComponent, serializeComponent, } from "./serializeComponent";
3
+ export function serializeNodeData(data) {
4
+ return {
5
+ name: data[1],
6
+ };
7
+ }
8
+ /** Serialize the node data as is for storage*/
9
+ export function serializeNode(node) {
10
+ const id = node.id;
11
+ let components = null;
12
+ if (node.hasComponents) {
13
+ components = [];
14
+ for (const comp of node.traverseComponents()) {
15
+ components.push(serializeComponent(comp));
16
+ }
17
+ }
18
+ let tags = null;
19
+ if (node.hasTags) {
20
+ tags = [];
21
+ for (const tag of node.traverseTags()) {
22
+ tags.push(tag.type);
23
+ }
24
+ }
25
+ let children = null;
26
+ if (node.childrenArray?.length) {
27
+ children = [];
28
+ for (const child of node.children()) {
29
+ children.push(serializeNode(child));
30
+ }
31
+ }
32
+ return {
33
+ ...(id ? { id: NodeId.ToHexString(id) } : {}),
34
+ name: node.name,
35
+ ...(components ? { components } : {}),
36
+ ...(tags ? { tags } : {}),
37
+ ...(children ? { children } : {}),
38
+ };
39
+ }
40
+ /** Copy the node data as is */
41
+ export function copyNode(node) {
42
+ return [null, node.name, null, null, null];
43
+ }
44
+ /** Clone the node data and create new ids if needed. */
45
+ export function cloneNode(node) {
46
+ return {
47
+ name: node.name,
48
+ };
49
+ }
50
+ export function createRemoteNode(node, includeChildren = false, includedComponents = []) {
51
+ let children = null;
52
+ if (includeChildren) {
53
+ children = [];
54
+ for (const child of node.children()) {
55
+ children.push(createRemoteNode(child, includeChildren, includedComponents));
56
+ }
57
+ }
58
+ let components = null;
59
+ if (node.hasComponents) {
60
+ components = [];
61
+ for (const component of node.traverseComponents()) {
62
+ if (includedComponents.length) {
63
+ for (let i = 0; i < includedComponents.length; i++) {
64
+ if (includedComponents[i].type == component.type) {
65
+ components.push(createRemoteComponent(component));
66
+ break;
67
+ }
68
+ }
69
+ }
70
+ else {
71
+ components.push(createRemoteComponent(component));
72
+ }
73
+ }
74
+ }
75
+ let tags = null;
76
+ if (node.hasTags) {
77
+ tags = [];
78
+ for (const tag of node.traverseTags()) {
79
+ tags.push(tag.typeId);
80
+ }
81
+ }
82
+ return [node.id ? node.id : null, node.name, components, tags, children];
83
+ }
@@ -0,0 +1,12 @@
1
+ import { CreateNodeData } from "./Nodes/Node.types";
2
+ import { CreateComponentData } from "./Components/Component.types";
3
+ import { RegisteredTag } from "./Register/registerTag";
4
+ export declare function Node(): CreateNodeData;
5
+ export declare function Node(components?: CreateComponentData[], tags?: number[] | null, ...children: CreateNodeData[]): CreateNodeData;
6
+ export declare function Node(data: string, components?: CreateComponentData[], ...children: CreateNodeData[]): CreateNodeData;
7
+ export declare function Node(data: {
8
+ id?: true;
9
+ name?: string;
10
+ tags?: number[];
11
+ }, components?: CreateComponentData[], ...children: CreateNodeData[]): CreateNodeData;
12
+ export declare function Tag(id: string, ...children: RegisteredTag[]): RegisteredTag;
package/Functional.js ADDED
@@ -0,0 +1,31 @@
1
+ import { NCS } from "./NCS";
2
+ export function Node(dataOrComponents, maybeComponentsOrChildren, ...restChildren) {
3
+ if (!dataOrComponents) {
4
+ return NCS.createNode(null, "New Node", null, undefined, restChildren);
5
+ }
6
+ // Case A: dataOrComponents is an array => treat it as the components array
7
+ if (Array.isArray(dataOrComponents)) {
8
+ const components = dataOrComponents;
9
+ const children = restChildren ?? [];
10
+ return NCS.createNode(undefined, undefined, Array.isArray(maybeComponentsOrChildren)
11
+ ? maybeComponentsOrChildren
12
+ : undefined, components, children);
13
+ }
14
+ // Case B: dataOrComponents is a string => treat it as the name
15
+ if (typeof dataOrComponents === "string") {
16
+ const name = dataOrComponents;
17
+ const components = (maybeComponentsOrChildren ??
18
+ []);
19
+ return NCS.createNode(undefined, name, undefined, components, restChildren);
20
+ }
21
+ // Case C: dataOrComponents is the object form => { id?, name?, state?, tags? }
22
+ const components = (maybeComponentsOrChildren ?? []);
23
+ return NCS.createNode(dataOrComponents.id, dataOrComponents.name, dataOrComponents.tags, components, restChildren);
24
+ }
25
+ export function Tag(id, ...children) {
26
+ const tag = NCS.registerTag({ id });
27
+ children.forEach((_) => {
28
+ tag.tag.addChild(_.tag);
29
+ });
30
+ return tag;
31
+ }
package/Graphs/Graph.d.ts CHANGED
@@ -1,24 +1,22 @@
1
- import { NodeData } from "../Nodes/NodeData";
2
- import { NodeInstance } from "../Nodes/NodeInstance";
3
- import { NodeId } from "../Nodes/NodeId";
4
- import { GraphEvents } from "./GraphEvents";
5
- import { GraphObservers } from "./GraphObservers";
6
- export interface GraphDependencies {
7
- [key: string]: any;
8
- }
9
- export interface Graph {
10
- }
1
+ import { CreateNodeData } from "../Nodes/Node.types";
2
+ import { NodeArray } from "../Nodes/NodeArray";
3
+ import { ComponentArray } from "../Components/ComponentArray";
4
+ import { NodeCursor } from "../Nodes/NodeCursor";
5
+ import { ContextArray } from "../Contexts/ContextArray";
6
+ import { TagArray } from "../Tags/TagArray";
7
+ import { SystemInstance } from "../Systems/SystemInstance";
11
8
  export declare class Graph {
12
- dependencies: GraphDependencies;
13
- _nodeMap: Map<BigInt, Map<BigInt, NodeInstance>>;
14
- events: GraphEvents;
15
- observers: GraphObservers;
16
- root: NodeInstance;
17
- constructor(dependencies: GraphDependencies);
18
- getNode(id: NodeId | string): NodeInstance;
19
- loadInRoot(data: NodeData): Promise<void>;
20
- addNode(data: NodeData, parent?: NodeInstance): Promise<NodeInstance>;
21
- removeNode(id: NodeId): void;
9
+ _systems: SystemInstance[];
10
+ _nodes: NodeArray;
11
+ _components: ComponentArray[];
12
+ _contexts: ContextArray;
13
+ _tags: TagArray[];
14
+ _updatingComponents: ComponentArray[];
15
+ root: NodeCursor;
16
+ constructor();
17
+ getNode(index: number, cursor?: NodeCursor): NodeCursor;
18
+ getNodeFromId(id: bigint | string, cursor?: NodeCursor): NodeCursor;
19
+ addNode(data: CreateNodeData, parent?: number, cursor?: NodeCursor): NodeCursor;
20
+ removeNode(index: number): boolean;
22
21
  update(): void;
23
- toJSON(): NodeData;
24
22
  }
package/Graphs/Graph.js CHANGED
@@ -1,90 +1,101 @@
1
- import { NodeInstance } from "../Nodes/NodeInstance";
2
1
  import { NodeId } from "../Nodes/NodeId";
3
- import { GraphEvents } from "./GraphEvents";
4
- import { Node } from "../NCS";
5
- import { GraphUpdate } from "./GraphUpdate";
6
- import { GraphObservers } from "./GraphObservers";
7
- export class Graph {
8
- dependencies;
9
- _nodeMap = new Map();
10
- events = new GraphEvents();
11
- observers = new GraphObservers();
12
- root = new NodeInstance(null, Node({ name: "__root__" }), this);
13
- constructor(dependencies) {
14
- this.dependencies = dependencies;
2
+ import { NodeArray } from "../Nodes/NodeArray";
3
+ import { NodeCursor } from "../Nodes/NodeCursor";
4
+ import { ContextArray } from "../Contexts/ContextArray";
5
+ import { NCSPools } from "../Pools/NCSPools";
6
+ const parentCursor = NodeCursor.Get();
7
+ const nodeCursor = NodeCursor.Get();
8
+ function createNode(graph, data, parent) {
9
+ const newNode = graph._nodes.addNode(typeof data[0] == "string" ? NodeId.FromString(data[0]) : data[0], parent, data[1]);
10
+ nodeCursor.graph = graph;
11
+ nodeCursor.setNode(graph, newNode);
12
+ if (data[2]?.length) {
13
+ for (let i = 0; i < data[2].length; i++) {
14
+ nodeCursor.components.add(data[2][i]);
15
+ }
15
16
  }
16
- getNode(id) {
17
- if (typeof id == "string")
18
- id = NodeId.Create(id);
19
- const high = this._nodeMap.get(id.low);
20
- if (!high)
21
- throw new Error(`Node with id ${id.idString} does not exist`);
22
- const node = high.get(id.high);
23
- if (!node)
24
- throw new Error(`Node with id ${id.idString} does not exist`);
25
- return node;
17
+ if (data[3]?.length) {
18
+ for (let i = 0; i < data[3].length; i++) {
19
+ nodeCursor.tags.add(data[3][i]);
20
+ }
26
21
  }
27
- async loadInRoot(data) {
28
- if (this.root)
29
- this.root.dispose();
30
- const root = new NodeInstance({}, data, this);
31
- await root.addChildren(...data.children);
32
- this.root = root;
22
+ if (parent >= 0) {
23
+ parentCursor.setNode(graph, parent);
24
+ parentCursor.addChild(nodeCursor);
33
25
  }
34
- async addNode(data, parent = this.root) {
35
- const newNode = new NodeInstance(parent, data, this);
36
- newNode.parent = parent;
37
- parent.children.push(newNode);
38
- let high = this._nodeMap.get(newNode.id.low);
39
- if (!high) {
40
- high = new Map();
41
- this._nodeMap.set(newNode.id.low, high);
26
+ if (data[4]?.length) {
27
+ for (let i = 0; i < data[4].length; i++) {
28
+ createNode(graph, data[4][i], newNode);
42
29
  }
43
- high.set(newNode.id.high, newNode);
44
- if (data.components?.length) {
45
- await newNode.components.addComponents(...data.components);
30
+ }
31
+ nodeCursor.setNode(graph, newNode);
32
+ data[0] = null;
33
+ data[1] = "";
34
+ data[2] = null;
35
+ data[3] = null;
36
+ data[4] = null;
37
+ NCSPools.createNodeData.addItem(data);
38
+ return nodeCursor;
39
+ }
40
+ export class Graph {
41
+ _systems = [];
42
+ _nodes = new NodeArray();
43
+ _components = [];
44
+ _contexts = new ContextArray();
45
+ _tags = [];
46
+ _updatingComponents = [];
47
+ root = NodeCursor.Get();
48
+ constructor() {
49
+ const rootIndex = this._nodes.addNode(null, -1, "root");
50
+ this.root.setNode(this, rootIndex);
51
+ }
52
+ getNode(index, cursor = nodeCursor) {
53
+ const parentIndex = this._nodes._parents[index];
54
+ if (typeof parentIndex === "undefined")
55
+ throw new Error(`Node with index ${index} does not exist`);
56
+ cursor.setNode(this, index);
57
+ return cursor;
58
+ }
59
+ getNodeFromId(id, cursor = nodeCursor) {
60
+ if (typeof id == "string")
61
+ id = NodeId.FromString(id);
62
+ const nodeIndex = this._nodes._idMap.get(id);
63
+ if (typeof nodeIndex === "undefined")
64
+ throw new Error(`Node with id ${id} does not exist`);
65
+ cursor.setNode(this, nodeIndex);
66
+ return cursor;
67
+ }
68
+ addNode(data, parent = this.root.index, cursor = nodeCursor) {
69
+ const newNode = createNode(this, data, parent);
70
+ if (newNode.hasComponents) {
71
+ const components = newNode.components.components;
72
+ for (let i = 0; i < components.length; i += 2) {
73
+ this._components[components[i]].init(components[i + 1]);
74
+ }
46
75
  }
47
- parent.hasObservers &&
48
- parent.observers.isChildAddedSet() &&
49
- parent.observers.childAdded.notify(newNode);
50
- this.observers.nodeAdded.notify(newNode);
51
- this.observers.nodesUpdated.notify();
52
- if (data.children?.length) {
53
- for (const child of data.children) {
54
- await this.addNode(child, newNode);
76
+ for (const child of newNode.traverseChildren()) {
77
+ if (child.hasComponents) {
78
+ const components = child.components.components;
79
+ for (let i = 0; i < components.length; i += 2) {
80
+ this._components[components[i]].init(components[i + 1]);
81
+ }
55
82
  }
56
83
  }
57
- return newNode;
84
+ newNode.toRef(cursor);
85
+ return cursor;
58
86
  }
59
- removeNode(id) {
60
- const low = this._nodeMap.get(id.low);
61
- if (!low)
62
- return;
63
- const node = low.get(id.high);
87
+ removeNode(index) {
88
+ const node = this._nodes.removeNode(index);
64
89
  if (!node)
65
- return;
66
- if (!node.isDisposed())
67
- node.dispose();
68
- this._nodeMap.delete(id.low);
69
- low.delete(id.high);
70
- this.observers.nodeRemoved.notify(node);
71
- this.observers.nodesUpdated.notify();
90
+ return false;
91
+ return true;
72
92
  }
73
93
  update() {
74
- {
75
- const updating = GraphUpdate.getUpdatingComponents(this);
76
- for (const component of updating) {
77
- component.componentPrototype.data.update(component);
78
- }
94
+ for (let i = 0; i < this._updatingComponents.length; i++) {
95
+ this._updatingComponents[i].update();
79
96
  }
80
- {
81
- const updating = GraphUpdate.getUpdatingSystems(this);
82
- for (const system of updating) {
83
- system.systemPrototype.update(system);
84
- }
97
+ for (let i = 0; i < this._systems.length; i++) {
98
+ this._systems[i].update();
85
99
  }
86
100
  }
87
- toJSON() {
88
- return this.root.toJSON();
89
- }
90
101
  }
package/NCS.d.ts CHANGED
@@ -1,54 +1,85 @@
1
- import { Pipeline } from "@amodx/core/Pipelines";
2
- import { Graph, GraphDependencies } from "./Graphs/Graph";
3
- import { NodeData, NodeStateData } from "./Nodes/NodeData";
4
- import { NodeInstance } from "./Nodes/NodeInstance";
5
- import { QueryData } from "./Queries/QueryData";
1
+ import { Graph } from "./Graphs/Graph";
2
+ import { CreateNodeData } from "./Nodes/Node.types";
3
+ import { QueryData } from "./Queries/Query.types";
6
4
  import { QueryPrototype } from "./Queries/QueryPrototype";
7
- import { ComponentData } from "./Components/ComponentData";
8
- import { TraitData } from "./Traits/TraitData";
9
- import { ContextData } from "./Contexts/ContextData";
10
- import { TagData } from "./Tags/TagData";
5
+ import { CreateComponentData } from "./Components/Component.types";
11
6
  import { registerContext } from "./Register/registerContext";
12
- import { registerTrait } from "./Register/registerTrait";
13
7
  import { registerTag } from "./Register/registerTag";
14
8
  import { registerSystem } from "./Register/registerSystem";
15
- export declare class NCS {
16
- static Pipelines: {
17
- OnComponentDataCreate: Pipeline<ComponentData>;
18
- OnTraitDataCreate: Pipeline<TraitData>;
19
- OnNodeDataCreate: Pipeline<NodeData>;
20
- OnContextDataCreate: Pipeline<ContextData>;
21
- OnTagDataCreate: Pipeline<TagData>;
22
- };
23
- static createGraph(dependencies: GraphDependencies): Graph;
24
- static createNode(name?: string | null, state?: NodeStateData | null, components?: ComponentData[], children?: NodeData[]): NodeData;
25
- static createQuery(data: QueryData): QueryPrototype;
26
- static registerSystem: typeof registerSystem;
27
- static registerComponent: <ComponentSchema extends object = {}, Data extends object = {}, Logic extends object = {}, Shared extends object = {}>(data: import("./Components/ComponentData").ComponentRegisterData<ComponentSchema, Data, Logic, Shared>) => import("./Components/ComponentData").ComponentRegisterData<ComponentSchema, Data, Logic, Shared> & {
28
- getNodes: (grpah: Graph) => Set<NodeInstance>;
29
- getComponents: (grpah: Graph) => Set<import(".").ComponentInstance<ComponentSchema, Data, Logic, Shared>>;
30
- set: (node: NodeInstance, componentSchema?: Partial<ComponentSchema> | undefined, traits?: TraitData[], state?: import("./Components/ComponentData").ComponentStateData) => Promise<import(".").ComponentInstance<ComponentSchema, Data, Logic, Shared>>;
31
- get: (node: NodeInstance) => import(".").ComponentInstance<ComponentSchema, Data, Logic, Shared> | null;
32
- getChild: (node: NodeInstance) => import(".").ComponentInstance<ComponentSchema, Data, Logic, Shared> | null;
33
- getParent: (node: NodeInstance) => import(".").ComponentInstance<ComponentSchema, Data, Logic, Shared> | null;
34
- getAll: (node: NodeInstance) => import(".").ComponentInstance<ComponentSchema, Data, Logic, Shared>[] | null;
35
- remove: (node: NodeInstance) => Promise<import(".").ComponentInstance<ComponentSchema, Data, Logic, Shared> | null>;
36
- removeAll: (node: NodeInstance) => Promise<import(".").ComponentInstance<ComponentSchema, Data, Logic, Shared>[] | null>;
9
+ import { Schema } from "./Schema/Schema";
10
+ import { copyComponent, createRemoteComponent, serializeComponent, serializeComponentData } from "./Data/serializeComponent";
11
+ import { deserializeComponentData } from "./Data/deserializeComponent";
12
+ import { cloneNode, copyNode, createRemoteNode, serializeNode, serializeNodeData } from "./Data/serializeNode";
13
+ import { deserializeNodeData } from "./Data/deserializeNode";
14
+ import { ExtractSchemaClass, SchemaCreateData, SchemaProperty } from "./Schema/Schema.types";
15
+ export declare const NCS: {
16
+ /** Create a graph. */
17
+ createGraph(): Graph;
18
+ /** Create node data to add a node to a graph. */
19
+ createNode(id?: true | null, name?: string | null, tags?: number[] | null, components?: CreateComponentData[], children?: CreateNodeData[]): CreateNodeData;
20
+ /** Create a schema from an object. */
21
+ schemaFromObject<T extends Record<string, any>>(data: T): Schema<T>;
22
+ /** Create a schema for a component or context. */
23
+ schema<T extends Record<string, SchemaProperty<any>>>(schema: T, views?: SchemaCreateData[]): Schema<ExtractSchemaClass<T>>;
24
+ /** Create a property for a schema. */
25
+ property<T>(value: T, meta?: SchemaProperty<T>["meta"]): SchemaProperty<T>;
26
+ /** Cast and set the type for the Data and Shared properties of a component. */
27
+ data<T>(data?: any): T;
28
+ /** Create a query to work with nodes based on specific components and tags. */
29
+ createQuery(data: QueryData): QueryPrototype;
30
+ /** Register a system for use with NCS. */
31
+ registerSystem: typeof registerSystem;
32
+ /** Register a component for use with NCS. */
33
+ registerComponent: <Data extends unknown = any, Shared extends unknown = any, ComponentSchema extends object = any>(data: import("./Components/Component.types").ComponentRegisterData<ComponentSchema, Data, Shared>) => import("./Components/Component.types").ComponentRegisterData<ComponentSchema, Data, Shared> & {
34
+ getNodes(grpah: Graph): Generator<import(".").NodeCursor>;
35
+ getComponents(grpah: Graph): Generator<import(".").ComponentCursor<ComponentSchema, Data, Shared>, any, any>;
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
+ has(node: import(".").NodeCursor): boolean;
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
+ getAll(node: import(".").NodeCursor): import(".").ComponentCursor<ComponentSchema, Data, Shared>[] | null;
45
+ remove(node: import(".").NodeCursor): boolean;
46
+ removeAll(node: import(".").NodeCursor): boolean;
37
47
  nodeData: {
38
- get: (node: NodeData) => ComponentData<ComponentSchema> | null;
39
- set: (node: NodeData, componentSchema?: Partial<ComponentSchema> | undefined, traits?: TraitData[], state?: import("./Components/ComponentData").ComponentStateData) => void;
40
- getAll: (node: NodeData) => ComponentData<ComponentSchema>[] | null;
41
- remove: (node: NodeData) => ComponentData<ComponentSchema> | null;
42
- removeAll: (node: NodeData) => ComponentData<ComponentSchema>[] | null;
48
+ get(node: import(".").SerializedNodeData): import(".").SerializedComponentData<ComponentSchema> | null;
49
+ set(node: import(".").SerializedNodeData, componentSchema?: Partial<ComponentSchema> | undefined): void;
50
+ getAll(node: import(".").SerializedNodeData): import(".").SerializedComponentData<ComponentSchema>[] | null;
51
+ remove(node: import(".").SerializedNodeData): import(".").SerializedComponentData<ComponentSchema> | null;
52
+ removeAll(node: import(".").SerializedNodeData): import(".").SerializedComponentData<ComponentSchema>[] | null;
43
53
  };
44
- prototype: import("./Components/ComponentPrototype").ComponentPrototype<ComponentSchema, Data, Logic, Shared>;
45
- default: import(".").ComponentInstance<ComponentSchema, Data, Logic, Shared>;
46
- } & ((schema?: Partial<ComponentSchema> | null | undefined, state?: Partial<import("./Components/ComponentData").ComponentStateData> | null | undefined, ...traits: TraitData[]) => ComponentData<ComponentSchema>);
47
- static registerTrait: typeof registerTrait;
48
- static registerContext: typeof registerContext;
49
- static registerTag: typeof registerTag;
50
- }
51
- export declare function Node(data: {
52
- name?: string;
53
- state?: NodeStateData;
54
- }, components?: ComponentData[], ...children: NodeData[]): NodeData;
54
+ type: string;
55
+ typeId: number;
56
+ data: import("./Components/Component.types").ComponentRegisterData<ComponentSchema, Data, Shared>;
57
+ default: import(".").ComponentCursor<ComponentSchema, Data, Shared>;
58
+ } & ((schema?: Partial<ComponentSchema> | null | undefined, schemaView?: string | null) => CreateComponentData<ComponentSchema>);
59
+ /** Register a context for use with NCS. */
60
+ registerContext: typeof registerContext;
61
+ /** Register a tag for use with NCS. */
62
+ registerTag: typeof registerTag;
63
+ /** Serialize a component for storage. */
64
+ serializeComponent: typeof serializeComponent;
65
+ /** Serialize component data for storage. */
66
+ serializeComponentData: typeof serializeComponentData;
67
+ /** Serialize a component to use in another context. */
68
+ createRemoteComponent: typeof createRemoteComponent;
69
+ /** Copy a component. */
70
+ copyComponent: typeof copyComponent;
71
+ /** Deserialize a component from storage into component data. */
72
+ deserializeComponentData: typeof deserializeComponentData;
73
+ /** Serialize a node for storage. */
74
+ serializeNode: typeof serializeNode;
75
+ /** Serialize node data for storage. */
76
+ serializeNodeData: typeof serializeNodeData;
77
+ /** Copy a node as is. */
78
+ copyNode: typeof copyNode;
79
+ /** Copy a node and change IDs. */
80
+ cloneNode: typeof cloneNode;
81
+ /** Serialize a node for use in another context. */
82
+ createRemoteNode: typeof createRemoteNode;
83
+ /** Deserialize a node from storage into node data. */
84
+ deserializeNodeData: typeof deserializeNodeData;
85
+ };
package/NCS.js CHANGED
@@ -1,41 +1,124 @@
1
- import { Pipeline } from "@amodx/core/Pipelines";
2
1
  import { Graph } from "./Graphs/Graph";
3
2
  import { NodeId } from "./Nodes/NodeId";
4
3
  import { QueryPrototype } from "./Queries/QueryPrototype";
5
4
  import { registerContext } from "./Register/registerContext";
6
5
  import { registerComponent } from "./Register/registerComponent";
7
- import { registerTrait } from "./Register/registerTrait";
8
6
  import { registerTag } from "./Register/registerTag";
9
7
  import { registerSystem } from "./Register/registerSystem";
10
- export class NCS {
11
- static Pipelines = {
12
- OnComponentDataCreate: new Pipeline(),
13
- OnTraitDataCreate: new Pipeline(),
14
- OnNodeDataCreate: new Pipeline(),
15
- OnContextDataCreate: new Pipeline(),
16
- OnTagDataCreate: new Pipeline(),
17
- };
18
- static createGraph(dependencies) {
19
- return new Graph(dependencies);
8
+ import { NCSPools } from "./Pools/NCSPools";
9
+ import { Schema } from "./Schema/Schema";
10
+ import { copyComponent, createRemoteComponent, serializeComponent, serializeComponentData, } from "./Data/serializeComponent";
11
+ import { deserializeComponentData } from "./Data/deserializeComponent";
12
+ import { cloneNode, copyNode, createRemoteNode, serializeNode, serializeNodeData, } from "./Data/serializeNode";
13
+ import { deserializeNodeData } from "./Data/deserializeNode";
14
+ import { SchemaProperty, } from "./Schema/Schema.types";
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
+ });
44
+ }
20
45
  }
21
- static createNode(name, state, components = [], children = []) {
22
- return NCS.Pipelines.OnNodeDataCreate.pipe({
23
- id: NodeId.Create().idString,
24
- components,
25
- children,
26
- name: name ? name : "",
27
- state: state ? state : {},
28
- });
29
- }
30
- static createQuery(data) {
31
- return new QueryPrototype(data);
32
- }
33
- static registerSystem = registerSystem;
34
- static registerComponent = registerComponent;
35
- static registerTrait = registerTrait;
36
- static registerContext = registerContext;
37
- static registerTag = registerTag;
38
- }
39
- export function Node(data, components, ...children) {
40
- return NCS.createNode(data.name, data.state, components, children);
46
+ return parent;
41
47
  }
48
+ export const NCS = {
49
+ /** Create a graph. */
50
+ createGraph() {
51
+ return new Graph();
52
+ },
53
+ /** Create node data to add a node to a graph. */
54
+ createNode(id, name, tags, components, children = []) {
55
+ const data = NCSPools.createNodeData.get() || [];
56
+ data[0] = id ? NodeId.Create() : null;
57
+ data[1] = name || "New Node";
58
+ data[2] = components || null;
59
+ data[3] = tags || null;
60
+ data[4] = children || null;
61
+ return data;
62
+ },
63
+ /** Create a schema from an object. */
64
+ schemaFromObject(data) {
65
+ return Schema.FromObject(data);
66
+ },
67
+ /** Create a schema for a component or context. */
68
+ schema(schema, views) {
69
+ const data = traverseCreateSchema(schema, {
70
+ id: "root",
71
+ value: {},
72
+ children: [],
73
+ }).children;
74
+ const s = new Schema(data);
75
+ if (views) {
76
+ for (const view of views) {
77
+ s.createView(view);
78
+ }
79
+ }
80
+ return s;
81
+ },
82
+ /** Create a property for a schema. */
83
+ property(value, meta = {}) {
84
+ return new SchemaProperty(value, meta);
85
+ },
86
+ /** Cast and set the type for the Data and Shared properties of a component. */
87
+ data(data = {}) {
88
+ return data;
89
+ },
90
+ /** Create a query to work with nodes based on specific components and tags. */
91
+ createQuery(data) {
92
+ return new QueryPrototype(data);
93
+ },
94
+ /** Register a system for use with NCS. */
95
+ registerSystem,
96
+ /** Register a component for use with NCS. */
97
+ registerComponent,
98
+ /** Register a context for use with NCS. */
99
+ registerContext,
100
+ /** Register a tag for use with NCS. */
101
+ registerTag,
102
+ /** Serialize a component for storage. */
103
+ serializeComponent,
104
+ /** Serialize component data for storage. */
105
+ serializeComponentData,
106
+ /** Serialize a component to use in another context. */
107
+ createRemoteComponent,
108
+ /** Copy a component. */
109
+ copyComponent,
110
+ /** Deserialize a component from storage into component data. */
111
+ deserializeComponentData,
112
+ /** Serialize a node for storage. */
113
+ serializeNode,
114
+ /** Serialize node data for storage. */
115
+ serializeNodeData,
116
+ /** Copy a node as is. */
117
+ copyNode,
118
+ /** Copy a node and change IDs. */
119
+ cloneNode,
120
+ /** Serialize a node for use in another context. */
121
+ createRemoteNode,
122
+ /** Deserialize a node from storage into node data. */
123
+ deserializeNodeData,
124
+ };