@amodx/ncs 0.0.1

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 (115) hide show
  1. package/Components/ComponentData.d.ts +112 -0
  2. package/Components/ComponentData.js +1 -0
  3. package/Components/ComponentInstance.d.ts +36 -0
  4. package/Components/ComponentInstance.js +125 -0
  5. package/Components/ComponentInstanceMap.d.ts +7 -0
  6. package/Components/ComponentInstanceMap.js +15 -0
  7. package/Components/ComponentObservers.d.ts +8 -0
  8. package/Components/ComponentObservers.js +13 -0
  9. package/Components/ComponentPipelines.d.ts +16 -0
  10. package/Components/ComponentPipelines.js +33 -0
  11. package/Components/ComponentPrototype.d.ts +17 -0
  12. package/Components/ComponentPrototype.js +73 -0
  13. package/Contexts/ContextData.d.ts +59 -0
  14. package/Contexts/ContextData.js +1 -0
  15. package/Contexts/ContextInstance.d.ts +14 -0
  16. package/Contexts/ContextInstance.js +26 -0
  17. package/Contexts/ContextInstanceAnchor.d.ts +15 -0
  18. package/Contexts/ContextInstanceAnchor.js +33 -0
  19. package/Contexts/ContextPrototype.d.ts +9 -0
  20. package/Contexts/ContextPrototype.js +19 -0
  21. package/Graphs/Graph.d.ts +24 -0
  22. package/Graphs/Graph.js +90 -0
  23. package/Graphs/GraphEvents.d.ts +9 -0
  24. package/Graphs/GraphEvents.js +35 -0
  25. package/Graphs/GraphObservers.d.ts +9 -0
  26. package/Graphs/GraphObservers.js +6 -0
  27. package/Graphs/GraphUpdate.d.ts +13 -0
  28. package/Graphs/GraphUpdate.js +38 -0
  29. package/Maps/ItemGraphMap.d.ts +11 -0
  30. package/Maps/ItemGraphMap.js +30 -0
  31. package/Maps/ItemNodeMap.d.ts +17 -0
  32. package/Maps/ItemNodeMap.js +41 -0
  33. package/NCS.d.ts +54 -0
  34. package/NCS.js +41 -0
  35. package/Nodes/Components/NodeComponentObservers.d.ts +15 -0
  36. package/Nodes/Components/NodeComponentObservers.js +33 -0
  37. package/Nodes/Components/NodeComponents.d.ts +22 -0
  38. package/Nodes/Components/NodeComponents.js +107 -0
  39. package/Nodes/Context/NodeContext.d.ts +13 -0
  40. package/Nodes/Context/NodeContext.js +45 -0
  41. package/Nodes/NodeData.d.ts +40 -0
  42. package/Nodes/NodeData.js +1 -0
  43. package/Nodes/NodeEvents.d.ts +25 -0
  44. package/Nodes/NodeEvents.js +62 -0
  45. package/Nodes/NodeId.d.ts +15 -0
  46. package/Nodes/NodeId.js +56 -0
  47. package/Nodes/NodeInstance.d.ts +50 -0
  48. package/Nodes/NodeInstance.js +202 -0
  49. package/Nodes/NodeObservers.d.ts +24 -0
  50. package/Nodes/NodeObservers.js +63 -0
  51. package/Nodes/NodePipelines.d.ts +16 -0
  52. package/Nodes/NodePipelines.js +33 -0
  53. package/Nodes/Tags/NodeTagObservers.d.ts +15 -0
  54. package/Nodes/Tags/NodeTagObservers.js +33 -0
  55. package/Nodes/Tags/NodeTags.d.ts +21 -0
  56. package/Nodes/Tags/NodeTags.js +95 -0
  57. package/Pools/ItemPool.d.ts +7 -0
  58. package/Pools/ItemPool.js +18 -0
  59. package/Pools/NCSPools.d.ts +7 -0
  60. package/Pools/NCSPools.js +5 -0
  61. package/Queries/QueryData.d.ts +8 -0
  62. package/Queries/QueryData.js +1 -0
  63. package/Queries/QueryInstance.d.ts +12 -0
  64. package/Queries/QueryInstance.js +78 -0
  65. package/Queries/QueryPrototype.d.ts +10 -0
  66. package/Queries/QueryPrototype.js +23 -0
  67. package/Register/ItemRegister.d.ts +7 -0
  68. package/Register/ItemRegister.js +24 -0
  69. package/Register/NCSRegister.d.ts +13 -0
  70. package/Register/NCSRegister.js +8 -0
  71. package/Register/registerComponent.d.ts +29 -0
  72. package/Register/registerComponent.js +54 -0
  73. package/Register/registerContext.d.ts +16 -0
  74. package/Register/registerContext.js +37 -0
  75. package/Register/registerSystem.d.ts +12 -0
  76. package/Register/registerSystem.js +12 -0
  77. package/Register/registerTag.d.ts +21 -0
  78. package/Register/registerTag.js +43 -0
  79. package/Register/registerTrait.d.ts +15 -0
  80. package/Register/registerTrait.js +39 -0
  81. package/Systems/SystemData.d.ts +20 -0
  82. package/Systems/SystemData.js +1 -0
  83. package/Systems/SystemInstance.d.ts +11 -0
  84. package/Systems/SystemInstance.js +19 -0
  85. package/Systems/SystemPrototype.d.ts +10 -0
  86. package/Systems/SystemPrototype.js +23 -0
  87. package/Tags/Tag.d.ts +14 -0
  88. package/Tags/Tag.js +51 -0
  89. package/Tags/TagData.d.ts +38 -0
  90. package/Tags/TagData.js +1 -0
  91. package/Tags/TagInstance.d.ts +13 -0
  92. package/Tags/TagInstance.js +17 -0
  93. package/Tags/TagInstanceMap.d.ts +7 -0
  94. package/Tags/TagInstanceMap.js +15 -0
  95. package/Tags/TagPrototype.d.ts +16 -0
  96. package/Tags/TagPrototype.js +34 -0
  97. package/Traits/TraitContaienrObservers.d.ts +15 -0
  98. package/Traits/TraitContaienrObservers.js +33 -0
  99. package/Traits/TraitContainer.d.ts +22 -0
  100. package/Traits/TraitContainer.js +114 -0
  101. package/Traits/TraitData.d.ts +106 -0
  102. package/Traits/TraitData.js +1 -0
  103. package/Traits/TraitInstance.d.ts +35 -0
  104. package/Traits/TraitInstance.js +115 -0
  105. package/Traits/TraitInstanceMap.d.ts +7 -0
  106. package/Traits/TraitInstanceMap.js +15 -0
  107. package/Traits/TraitObservers.d.ts +8 -0
  108. package/Traits/TraitObservers.js +13 -0
  109. package/Traits/TraitPipelines.d.ts +16 -0
  110. package/Traits/TraitPipelines.js +33 -0
  111. package/Traits/TraitPrototype.d.ts +16 -0
  112. package/Traits/TraitPrototype.js +74 -0
  113. package/index.d.ts +8 -0
  114. package/index.js +8 -0
  115. package/package.json +27 -0
@@ -0,0 +1,90 @@
1
+ import { NodeInstance } from "../Nodes/NodeInstance";
2
+ 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;
15
+ }
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;
26
+ }
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;
33
+ }
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);
42
+ }
43
+ high.set(newNode.id.high, newNode);
44
+ if (data.components?.length) {
45
+ await newNode.components.addComponents(...data.components);
46
+ }
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);
55
+ }
56
+ }
57
+ return newNode;
58
+ }
59
+ removeNode(id) {
60
+ const low = this._nodeMap.get(id.low);
61
+ if (!low)
62
+ return;
63
+ const node = low.get(id.high);
64
+ 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();
72
+ }
73
+ update() {
74
+ {
75
+ const updating = GraphUpdate.getUpdatingComponents(this);
76
+ for (const component of updating) {
77
+ component.componentPrototype.data.update(component);
78
+ }
79
+ }
80
+ {
81
+ const updating = GraphUpdate.getUpdatingSystems(this);
82
+ for (const system of updating) {
83
+ system.systemPrototype.update(system);
84
+ }
85
+ }
86
+ }
87
+ toJSON() {
88
+ return this.root.toJSON();
89
+ }
90
+ }
@@ -0,0 +1,9 @@
1
+ import { NodeEventCursor } from "../Nodes/NodeEvents";
2
+ export declare class GraphEvents {
3
+ private events;
4
+ hasListener(id: string): boolean;
5
+ clearListeners(id: string): false | undefined;
6
+ addListener(id: string, run: (data: NodeEventCursor) => void): void;
7
+ removeListener(id: string, run: Function): boolean;
8
+ dispatch<Data>(id: string, data: NodeEventCursor): boolean;
9
+ }
@@ -0,0 +1,35 @@
1
+ import { Observable } from "@amodx/core/Observers";
2
+ export class GraphEvents {
3
+ events = new Map();
4
+ hasListener(id) {
5
+ return this.events.has(id);
6
+ }
7
+ clearListeners(id) {
8
+ let observer = this.events.get(id);
9
+ if (!observer)
10
+ return false;
11
+ observer.clear();
12
+ }
13
+ addListener(id, run) {
14
+ let observer = this.events.get(id);
15
+ if (!observer) {
16
+ observer = new Observable();
17
+ this.events.set(id, observer);
18
+ }
19
+ observer.subscribe(run);
20
+ }
21
+ removeListener(id, run) {
22
+ let observer = this.events.get(id);
23
+ if (!observer)
24
+ return false;
25
+ observer.unsubscribe(run);
26
+ return true;
27
+ }
28
+ dispatch(id, data) {
29
+ let observer = this.events.get(id);
30
+ if (!observer)
31
+ return false;
32
+ observer.notify(data);
33
+ return true;
34
+ }
35
+ }
@@ -0,0 +1,9 @@
1
+ import { Observable } from "@amodx/core/Observers";
2
+ import { NodeInstance } from "../Nodes/NodeInstance";
3
+ export interface GraphObservers {
4
+ }
5
+ export declare class GraphObservers {
6
+ nodeAdded: Observable<NodeInstance>;
7
+ nodeRemoved: Observable<NodeInstance>;
8
+ nodesUpdated: Observable<void>;
9
+ }
@@ -0,0 +1,6 @@
1
+ import { Observable } from "@amodx/core/Observers";
2
+ export class GraphObservers {
3
+ nodeAdded = new Observable();
4
+ nodeRemoved = new Observable();
5
+ nodesUpdated = new Observable();
6
+ }
@@ -0,0 +1,13 @@
1
+ import { SystemInstance } from "../Systems/SystemInstance";
2
+ import { ComponentInstance } from "../Components/ComponentInstance";
3
+ import { Graph } from "./Graph";
4
+ export declare class GraphUpdate {
5
+ private static updatingComponents;
6
+ static addComponentToUpdate(component: ComponentInstance<any, any, any, any>): void;
7
+ static removeComponentFromUpate(component: ComponentInstance<any, any, any, any>): void;
8
+ static getUpdatingComponents(graph: Graph): Set<ComponentInstance<{}, {}, {}, {}>>;
9
+ private static updatingSystems;
10
+ static addSystemToUpdate(system: SystemInstance): void;
11
+ static removeSystemFromUpdate(system: SystemInstance): void;
12
+ static getUpdatingSystems(graph: Graph): Set<SystemInstance>;
13
+ }
@@ -0,0 +1,38 @@
1
+ export class GraphUpdate {
2
+ static updatingComponents = new Map();
3
+ static addComponentToUpdate(component) {
4
+ let updating = this.updatingComponents.get(component.node.graph);
5
+ if (!updating) {
6
+ updating = new Set();
7
+ this.updatingComponents.set(component.node.graph, updating);
8
+ }
9
+ updating.add(component);
10
+ }
11
+ static removeComponentFromUpate(component) {
12
+ let updating = this.updatingComponents.get(component.node.graph);
13
+ if (!updating)
14
+ return;
15
+ updating.delete(component);
16
+ }
17
+ static getUpdatingComponents(graph) {
18
+ return this.updatingComponents.get(graph);
19
+ }
20
+ static updatingSystems = new Map();
21
+ static addSystemToUpdate(system) {
22
+ let updating = this.updatingSystems.get(system.graph);
23
+ if (!updating) {
24
+ updating = new Set();
25
+ this.updatingSystems.set(system.graph, updating);
26
+ }
27
+ updating.add(system);
28
+ }
29
+ static removeSystemFromUpdate(system) {
30
+ let updating = this.updatingSystems.get(system.graph);
31
+ if (!updating)
32
+ return;
33
+ updating.delete(system);
34
+ }
35
+ static getUpdatingSystems(graph) {
36
+ return this.updatingSystems.get(graph);
37
+ }
38
+ }
@@ -0,0 +1,11 @@
1
+ import { NodeInstance } from "../Nodes/NodeInstance";
2
+ import { Graph } from "../Graphs/Graph";
3
+ import { ItemNodeMap } from "../Maps/ItemNodeMap";
4
+ export declare class ItemGraphMap<MappedItem> {
5
+ private graphs;
6
+ getMap(graph: Graph): ItemNodeMap<MappedItem>;
7
+ addNode(node: NodeInstance, item: MappedItem): void;
8
+ removeNode(node: NodeInstance, item: MappedItem): void;
9
+ getNodes(graph: Graph): Set<NodeInstance>;
10
+ getItems(graph: Graph): Set<MappedItem>;
11
+ }
@@ -0,0 +1,30 @@
1
+ import { ItemNodeMap } from "../Maps/ItemNodeMap";
2
+ export class ItemGraphMap {
3
+ graphs = new Map();
4
+ getMap(graph) {
5
+ let graphMap = this.graphs.get(graph);
6
+ if (!graphMap) {
7
+ graphMap = new ItemNodeMap();
8
+ this.graphs.set(graph, graphMap);
9
+ }
10
+ return graphMap;
11
+ }
12
+ addNode(node, item) {
13
+ const graphMap = this.getMap(node.graph);
14
+ graphMap.addNode(node, item);
15
+ }
16
+ removeNode(node, item) {
17
+ if (!this.graphs.has(node.graph))
18
+ return;
19
+ const graphMap = this.getMap(node.graph);
20
+ graphMap.addNode(node, item);
21
+ }
22
+ getNodes(graph) {
23
+ const graphMap = this.getMap(graph);
24
+ return graphMap.getNodes();
25
+ }
26
+ getItems(graph) {
27
+ const graphMap = this.getMap(graph);
28
+ return graphMap.getItems();
29
+ }
30
+ }
@@ -0,0 +1,17 @@
1
+ import { Observable } from "@amodx/core/Observers";
2
+ import { NodeInstance } from "../Nodes/NodeInstance";
3
+ declare class NodeGraphMapObservers {
4
+ nodeAdded: Observable<NodeInstance>;
5
+ nodeRemoved: Observable<NodeInstance>;
6
+ }
7
+ export declare class ItemNodeMap<MappedItem> {
8
+ private nodes;
9
+ private items;
10
+ private nodeMap;
11
+ observers: NodeGraphMapObservers;
12
+ addNode(node: NodeInstance, mappedItem: MappedItem): void;
13
+ removeNode(node: NodeInstance, mappedItem: MappedItem): void;
14
+ getNodes(): Set<NodeInstance>;
15
+ getItems(): Set<MappedItem>;
16
+ }
17
+ export {};
@@ -0,0 +1,41 @@
1
+ import { Observable } from "@amodx/core/Observers";
2
+ class NodeGraphMapObservers {
3
+ nodeAdded = new Observable();
4
+ nodeRemoved = new Observable();
5
+ }
6
+ export class ItemNodeMap {
7
+ nodes = new Set();
8
+ items = new Set();
9
+ nodeMap = new Map();
10
+ observers = new NodeGraphMapObservers();
11
+ addNode(node, mappedItem) {
12
+ this.nodes.add(node);
13
+ this.items.add(mappedItem);
14
+ let mapped = this.nodeMap.get(node);
15
+ if (!mapped) {
16
+ mapped = [];
17
+ this.nodeMap.set(node, mapped);
18
+ }
19
+ mapped.push(mappedItem);
20
+ this.observers.nodeAdded.notify(node);
21
+ }
22
+ removeNode(node, mappedItem) {
23
+ this.nodes.delete(node);
24
+ this.items.delete(mappedItem);
25
+ let mapped = this.nodeMap.get(node);
26
+ if (!mapped)
27
+ return;
28
+ mapped = mapped.filter((_) => _ != mappedItem);
29
+ if (!mapped.length) {
30
+ this.nodeMap.delete(node);
31
+ this.nodes.delete(node);
32
+ this.observers.nodeRemoved.notify(node);
33
+ }
34
+ }
35
+ getNodes() {
36
+ return this.nodes;
37
+ }
38
+ getItems() {
39
+ return this.items;
40
+ }
41
+ }
package/NCS.d.ts ADDED
@@ -0,0 +1,54 @@
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";
6
+ 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";
11
+ import { registerContext } from "./Register/registerContext";
12
+ import { registerTrait } from "./Register/registerTrait";
13
+ import { registerTag } from "./Register/registerTag";
14
+ 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>;
37
+ 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;
43
+ };
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;
package/NCS.js ADDED
@@ -0,0 +1,41 @@
1
+ import { Pipeline } from "@amodx/core/Pipelines";
2
+ import { Graph } from "./Graphs/Graph";
3
+ import { NodeId } from "./Nodes/NodeId";
4
+ import { QueryPrototype } from "./Queries/QueryPrototype";
5
+ import { registerContext } from "./Register/registerContext";
6
+ import { registerComponent } from "./Register/registerComponent";
7
+ import { registerTrait } from "./Register/registerTrait";
8
+ import { registerTag } from "./Register/registerTag";
9
+ 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);
20
+ }
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);
41
+ }
@@ -0,0 +1,15 @@
1
+ import { Observable } from "@amodx/core/Observers";
2
+ import { ComponentInstance } from "../../Components/ComponentInstance";
3
+ export interface NodeComponentObservers {
4
+ }
5
+ export declare class NodeComponentObservers {
6
+ private _componentAdded?;
7
+ private _componentRemoved?;
8
+ private _componentsUpdated?;
9
+ get componentAdded(): Observable<ComponentInstance<any>>;
10
+ get componentRemoved(): Observable<ComponentInstance<any>>;
11
+ get componentsUpdated(): Observable<void>;
12
+ isComponentAddedSet(): boolean;
13
+ isComponentRemovedSet(): boolean;
14
+ isComponentsUpdatedSet(): boolean;
15
+ }
@@ -0,0 +1,33 @@
1
+ import { Observable } from "@amodx/core/Observers";
2
+ export class NodeComponentObservers {
3
+ _componentAdded;
4
+ _componentRemoved;
5
+ _componentsUpdated;
6
+ get componentAdded() {
7
+ if (!this._componentAdded) {
8
+ this._componentAdded = new Observable();
9
+ }
10
+ return this._componentAdded;
11
+ }
12
+ get componentRemoved() {
13
+ if (!this._componentRemoved) {
14
+ this._componentRemoved = new Observable();
15
+ }
16
+ return this._componentRemoved;
17
+ }
18
+ get componentsUpdated() {
19
+ if (!this._componentsUpdated) {
20
+ this._componentsUpdated = new Observable();
21
+ }
22
+ return this._componentsUpdated;
23
+ }
24
+ isComponentAddedSet() {
25
+ return !!this._componentAdded;
26
+ }
27
+ isComponentRemovedSet() {
28
+ return !!this._componentRemoved;
29
+ }
30
+ isComponentsUpdatedSet() {
31
+ return !!this._componentsUpdated;
32
+ }
33
+ }
@@ -0,0 +1,22 @@
1
+ import { NodeInstance } from "../NodeInstance";
2
+ import { ComponentData } from "../../Components/ComponentData";
3
+ import { ComponentInstance } from "../../Components/ComponentInstance";
4
+ import { NodeComponentObservers } from "./NodeComponentObservers";
5
+ export declare class NodeComponents {
6
+ node: NodeInstance;
7
+ private _observers?;
8
+ get observers(): NodeComponentObservers;
9
+ get hasObservers(): boolean;
10
+ components: ComponentInstance[];
11
+ constructor(node: NodeInstance);
12
+ dispose(): Promise<void>;
13
+ add(comp: ComponentData, init?: boolean): Promise<ComponentInstance<any, any, any, any>>;
14
+ addComponents(...components: ComponentData[]): Promise<void>;
15
+ removeByIndex(index: number): Promise<boolean>;
16
+ remove(type: string): Promise<boolean>;
17
+ get(type: string): ComponentInstance<any, any, any, any> | null;
18
+ getAll(type: string): ComponentInstance<any, any, any, any>[];
19
+ removeAll(type: string): Promise<ComponentInstance<any, any, any, any>[]>;
20
+ getChild(type: string): ComponentInstance<any, any, any, any> | null;
21
+ getParent(type: string): ComponentInstance<any, any, any, any> | null;
22
+ }
@@ -0,0 +1,107 @@
1
+ import { NodeComponentObservers } from "./NodeComponentObservers";
2
+ import { NCSRegister } from "../../Register/NCSRegister";
3
+ export class NodeComponents {
4
+ node;
5
+ _observers;
6
+ get observers() {
7
+ if (!this._observers) {
8
+ this._observers = new NodeComponentObservers();
9
+ }
10
+ return this._observers;
11
+ }
12
+ get hasObservers() {
13
+ return Boolean(this._observers);
14
+ }
15
+ components = [];
16
+ constructor(node) {
17
+ this.node = node;
18
+ }
19
+ async dispose() {
20
+ for (const comp of this.components) {
21
+ await comp.dispose();
22
+ }
23
+ }
24
+ async add(comp, init = true) {
25
+ const compType = NCSRegister.components.get(comp.type, comp.namespace || "main");
26
+ const newComponent = compType.create(this.node, comp);
27
+ this.components.push(newComponent);
28
+ if (init)
29
+ await newComponent.init();
30
+ if (comp.traits.length) {
31
+ await newComponent.traits.addTraits(...comp.traits);
32
+ }
33
+ this.hasObservers &&
34
+ this.observers.isComponentAddedSet() &&
35
+ this.hasObservers &&
36
+ this.observers.componentAdded.notify(newComponent);
37
+ return newComponent;
38
+ }
39
+ async addComponents(...components) {
40
+ const newComponents = [];
41
+ for (const comp of components) {
42
+ newComponents.push(await this.add(comp, false));
43
+ }
44
+ for (const comp of newComponents) {
45
+ await comp.init();
46
+ }
47
+ this.hasObservers &&
48
+ this.observers.isComponentsUpdatedSet() &&
49
+ this.observers.componentsUpdated.notify();
50
+ }
51
+ async removeByIndex(index) {
52
+ const component = this.components[index];
53
+ if (component) {
54
+ const child = this.components.splice(index, 1)[0];
55
+ this.hasObservers &&
56
+ this.observers.isComponentRemovedSet() &&
57
+ this.observers.componentRemoved.notify(child);
58
+ this.hasObservers &&
59
+ this.observers.isComponentsUpdatedSet() &&
60
+ this.observers.componentsUpdated.notify();
61
+ await component.dispose();
62
+ return true;
63
+ }
64
+ return false;
65
+ }
66
+ remove(type) {
67
+ return this.removeByIndex(this.components.findIndex((_) => _.type == type));
68
+ }
69
+ get(type) {
70
+ return this.components.find((_) => _.type == type) || null;
71
+ }
72
+ getAll(type) {
73
+ return this.components.filter((_) => _.type == type);
74
+ }
75
+ async removeAll(type) {
76
+ const filtered = this.getAll(type);
77
+ this.components = this.components.filter((_) => _.type != type);
78
+ for (const comp of filtered) {
79
+ await comp.dispose();
80
+ }
81
+ for (const comp of filtered) {
82
+ this.hasObservers &&
83
+ this.observers.isComponentRemovedSet() &&
84
+ this.observers.componentRemoved.notify(comp);
85
+ }
86
+ this.hasObservers &&
87
+ this.observers.isComponentsUpdatedSet() &&
88
+ this.observers.componentsUpdated.notify();
89
+ return filtered;
90
+ }
91
+ getChild(type) {
92
+ for (const child of this.node.traverseChildren()) {
93
+ const found = child.components.get(type);
94
+ if (found)
95
+ return found;
96
+ }
97
+ return null;
98
+ }
99
+ getParent(type) {
100
+ for (const parent of this.node.traverseParents()) {
101
+ const found = parent.components.get(type);
102
+ if (found)
103
+ return found;
104
+ }
105
+ return null;
106
+ }
107
+ }
@@ -0,0 +1,13 @@
1
+ import { ContextInstance } from "../../Contexts/ContextInstance";
2
+ import { NodeInstance } from "../NodeInstance";
3
+ import { ContextAnchorInstance } from "../../Contexts/ContextInstanceAnchor";
4
+ import { ContextData } from "../../Contexts/ContextData";
5
+ export declare class NodeContext {
6
+ node: NodeInstance;
7
+ contexts: Map<string, ContextInstance<{}, {}>>;
8
+ anchors: Map<string, ContextAnchorInstance<{}, {}>>;
9
+ constructor(node: NodeInstance);
10
+ add(context: ContextData): ContextInstance<{}, {}> | ContextInstance<any, any>;
11
+ remove(type: string): ContextInstance<{}, {}> | undefined;
12
+ get(type: string): ContextInstance | ContextAnchorInstance | null;
13
+ }
@@ -0,0 +1,45 @@
1
+ import { ContextInstance } from "../../Contexts/ContextInstance";
2
+ import { ContextAnchorInstance } from "../../Contexts/ContextInstanceAnchor";
3
+ import { NCSRegister } from "../../Register/NCSRegister";
4
+ export class NodeContext {
5
+ node;
6
+ contexts = new Map();
7
+ anchors = new Map();
8
+ constructor(node) {
9
+ this.node = node;
10
+ }
11
+ add(context) {
12
+ if (this.contexts.has(context.type)) {
13
+ return this.contexts.get(context.type);
14
+ }
15
+ const newContext = new ContextInstance(this.node, NCSRegister.contexts.get(context.type, context.namespace || "main"), context);
16
+ this.contexts.set(context.type, newContext);
17
+ return newContext;
18
+ }
19
+ remove(type) {
20
+ const context = this.contexts.get(type);
21
+ if (!context)
22
+ return;
23
+ this.contexts.delete(type);
24
+ return context;
25
+ }
26
+ get(type) {
27
+ if (this.contexts.has(type))
28
+ return this.contexts.get(type);
29
+ if (this.anchors.has(type))
30
+ return this.anchors.get(type);
31
+ for (const parent of this.node.traverseParents()) {
32
+ if (parent.hasContexts) {
33
+ const found = parent.context.get(type);
34
+ if (found) {
35
+ const newAnchor = found instanceof ContextInstance
36
+ ? new ContextAnchorInstance(this.node, found)
37
+ : new ContextAnchorInstance(this.node, found.getContext());
38
+ this.anchors.set(type, newAnchor);
39
+ return newAnchor;
40
+ }
41
+ }
42
+ }
43
+ return null;
44
+ }
45
+ }