@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,115 @@
1
+ import { ComponentInstance } from "../Components/ComponentInstance";
2
+ import { NodeInstance } from "../Nodes/NodeInstance";
3
+ import { TraintContainer } from "./TraitContainer";
4
+ import { TraitObservers } from "./TraitObservers";
5
+ import { TraitPipelines } from "./TraitPipelines";
6
+ export class TraitInstance {
7
+ type;
8
+ schema;
9
+ data;
10
+ logic;
11
+ state;
12
+ _shared;
13
+ get shared() {
14
+ return this._shared;
15
+ }
16
+ _traits;
17
+ get traits() {
18
+ if (!this._traits) {
19
+ this._traits = new TraintContainer(this);
20
+ }
21
+ return this._traits;
22
+ }
23
+ get hasTraits() {
24
+ return Boolean(this._traits);
25
+ }
26
+ _observers;
27
+ get observers() {
28
+ if (!this._observers) {
29
+ this._observers = new TraitObservers();
30
+ }
31
+ return this._observers;
32
+ }
33
+ get hasObservers() {
34
+ return Boolean(this._observers);
35
+ }
36
+ _pipelines;
37
+ get pipelines() {
38
+ if (!this._pipelines) {
39
+ this._pipelines = new TraitPipelines();
40
+ }
41
+ return this._pipelines;
42
+ }
43
+ get hasPipelines() {
44
+ return Boolean(this._pipelines);
45
+ }
46
+ parent;
47
+ traitProotype;
48
+ getNode() {
49
+ let node = this.parent;
50
+ while (!(node instanceof NodeInstance)) {
51
+ if (node instanceof ComponentInstance) {
52
+ node = node.node;
53
+ break;
54
+ }
55
+ if (node instanceof TraitInstance) {
56
+ node = node.parent;
57
+ }
58
+ }
59
+ return node;
60
+ }
61
+ async init() {
62
+ if (!this.traitProotype.data.init)
63
+ return;
64
+ await this.traitProotype.data.init(this);
65
+ }
66
+ _disposed = false;
67
+ isDisposed() {
68
+ return this._disposed;
69
+ }
70
+ async dispose() {
71
+ this.hasPipelines &&
72
+ this.pipelines.isDisposedSet() &&
73
+ this.pipelines.disposed.pipe(this);
74
+ this.hasObservers &&
75
+ this.observers.isDisposedSet() &&
76
+ this.observers.disposed.notify();
77
+ if (this.traitProotype.data.dispose)
78
+ await this.traitProotype.data.dispose(this);
79
+ this._disposed = true;
80
+ if (this.hasTraits)
81
+ await this.traits.dispose();
82
+ this.traitProotype.destory(this);
83
+ delete this._traits;
84
+ delete this._observers;
85
+ delete this._pipelines;
86
+ }
87
+ copy() {
88
+ const data = {
89
+ schema: this.schema?.getSchema
90
+ ? this.schema.getSchema().store()
91
+ : {},
92
+ type: this.type,
93
+ state: this.state,
94
+ traits: (this.hasTraits && this.traits.traits.map((_) => _.toJSON())) || [],
95
+ };
96
+ return ((this.hasPipelines &&
97
+ this.pipelines.isCopySet() &&
98
+ this.pipelines.copy.pipe(data)) ||
99
+ data);
100
+ }
101
+ toJSON() {
102
+ const data = {
103
+ schema: this.schema?.getSchema
104
+ ? this.schema.getSchema().store()
105
+ : {},
106
+ type: this.type,
107
+ state: this.state,
108
+ traits: (this.hasTraits && this.traits.traits.map((_) => _.toJSON())) || [],
109
+ };
110
+ return ((this.hasPipelines &&
111
+ this.pipelines.isCopySet() &&
112
+ this.pipelines.copy.pipe(data)) ||
113
+ data);
114
+ }
115
+ }
@@ -0,0 +1,7 @@
1
+ import { TraitInstance } from "./TraitInstance";
2
+ import { ItemGraphMap } from "../Maps/ItemGraphMap";
3
+ export declare class TraitInstanceMap {
4
+ private static types;
5
+ static registerTrait(type: string): ItemGraphMap<any>;
6
+ static getMap(type: string): ItemGraphMap<TraitInstance<any, any, any, any>>;
7
+ }
@@ -0,0 +1,15 @@
1
+ import { ItemGraphMap } from "../Maps/ItemGraphMap";
2
+ export class TraitInstanceMap {
3
+ static types = new Map();
4
+ static registerTrait(type) {
5
+ const map = new ItemGraphMap();
6
+ this.types.set(type, map);
7
+ return map;
8
+ }
9
+ static getMap(type) {
10
+ const map = this.types.get(type);
11
+ if (!map)
12
+ throw new Error(`Map for trait ${type} does not exist`);
13
+ return map;
14
+ }
15
+ }
@@ -0,0 +1,8 @@
1
+ import { Observable } from "@amodx/core/Observers";
2
+ export interface TraitObservers {
3
+ }
4
+ export declare class TraitObservers {
5
+ private _disposed?;
6
+ get disposed(): Observable<void>;
7
+ isDisposedSet(): boolean;
8
+ }
@@ -0,0 +1,13 @@
1
+ import { Observable } from "@amodx/core/Observers";
2
+ export class TraitObservers {
3
+ _disposed;
4
+ get disposed() {
5
+ if (!this._disposed) {
6
+ this._disposed = new Observable();
7
+ }
8
+ return this._disposed;
9
+ }
10
+ isDisposedSet() {
11
+ return !!this._disposed;
12
+ }
13
+ }
@@ -0,0 +1,16 @@
1
+ import { Pipeline } from "@amodx/core/Pipelines";
2
+ import { TraitInstance } from "./TraitInstance";
3
+ import { TraitData } from "./TraitData";
4
+ export interface TraitPipelines {
5
+ }
6
+ export declare class TraitPipelines<TraitSchema extends object = {}> {
7
+ private _disposed?;
8
+ private _toJSON?;
9
+ private _copy?;
10
+ get disposed(): Pipeline<TraitInstance<TraitSchema>>;
11
+ get toJSON(): Pipeline<TraitData<TraitSchema>>;
12
+ get copy(): Pipeline<TraitData<TraitSchema>>;
13
+ isDisposedSet(): boolean;
14
+ isToJSONSet(): boolean;
15
+ isCopySet(): boolean;
16
+ }
@@ -0,0 +1,33 @@
1
+ import { Pipeline } from "@amodx/core/Pipelines";
2
+ export class TraitPipelines {
3
+ _disposed;
4
+ _toJSON;
5
+ _copy;
6
+ get disposed() {
7
+ if (!this._disposed) {
8
+ this._disposed = new Pipeline();
9
+ }
10
+ return this._disposed;
11
+ }
12
+ get toJSON() {
13
+ if (!this._toJSON) {
14
+ this._toJSON = new Pipeline();
15
+ }
16
+ return this._toJSON;
17
+ }
18
+ get copy() {
19
+ if (!this._copy) {
20
+ this._copy = new Pipeline();
21
+ }
22
+ return this._copy;
23
+ }
24
+ isDisposedSet() {
25
+ return !!this._disposed;
26
+ }
27
+ isToJSONSet() {
28
+ return !!this._toJSON;
29
+ }
30
+ isCopySet() {
31
+ return !!this._copy;
32
+ }
33
+ }
@@ -0,0 +1,16 @@
1
+ import { ObjectSchemaInstance, Schema } from "@amodx/schemas";
2
+ import { TraitData, TraitRegisterData } from "./TraitData";
3
+ import { TraitInstance } from "./TraitInstance";
4
+ import { ItemPool } from "../Pools/ItemPool";
5
+ export declare class TraitPrototype<TraitSchema extends object = {}, Data extends Record<string, any> = {}, Logic extends Record<string, any> = {}, Shared extends Record<string, any> = {}> {
6
+ data: TraitRegisterData<TraitSchema, Data, Logic, Shared>;
7
+ schemaController: Schema<TraitSchema> | null;
8
+ baseContextSchema: TraitSchema;
9
+ pool: ItemPool<TraitInstance<TraitSchema, Data, Logic, Shared>>;
10
+ constructor(data: TraitRegisterData<TraitSchema, Data, Logic, Shared>);
11
+ getSchema(overrides: Partial<TraitSchema>): ObjectSchemaInstance<TraitSchema>;
12
+ private getPooled;
13
+ private return;
14
+ create(parent: TraitInstance["parent"], data: TraitData): TraitInstance<TraitSchema, Data, Logic, Shared>;
15
+ destory(instance: TraitInstance<TraitSchema, Data, Logic, Shared>): void;
16
+ }
@@ -0,0 +1,74 @@
1
+ import { Schema } from "@amodx/schemas";
2
+ import { TraitInstance } from "./TraitInstance";
3
+ import { TraitInstanceMap } from "./TraitInstanceMap";
4
+ import { ItemPool } from "../Pools/ItemPool";
5
+ export class TraitPrototype {
6
+ data;
7
+ schemaController;
8
+ baseContextSchema;
9
+ pool = new ItemPool();
10
+ constructor(data) {
11
+ this.data = data;
12
+ this.schemaController =
13
+ Array.isArray(data.schema) && data.schema.length
14
+ ? Schema.Create(...data.schema)
15
+ : null;
16
+ this.baseContextSchema = (this.schemaController ? this.schemaController.createData() : {});
17
+ this.pool.maxSize = data.pool?.maxSize || 100;
18
+ }
19
+ getSchema(overrides) {
20
+ if (!this.schemaController)
21
+ return structuredClone(this.baseContextSchema);
22
+ return this.schemaController.instantiate(overrides);
23
+ }
24
+ getPooled() {
25
+ let comp = null;
26
+ if (this.data.pool?.maxSize) {
27
+ comp = this.pool.get();
28
+ }
29
+ return comp || new TraitInstance();
30
+ }
31
+ return(component) {
32
+ if (this.data.pool?.maxSize) {
33
+ return this.pool.addItem(component);
34
+ }
35
+ return null;
36
+ }
37
+ create(parent, data) {
38
+ const instance = this.getPooled();
39
+ instance.parent = parent;
40
+ if (this.data.schema && !instance.schema) {
41
+ instance.schema = this.getSchema(data.schema);
42
+ }
43
+ if (this.data.schema && instance.schema) {
44
+ instance.schema.getSchema().loadIn(this.data.schema);
45
+ }
46
+ if (this.data.logic) {
47
+ instance.logic = this.data.logic
48
+ ? typeof this.data.logic == "function"
49
+ ? this.data.logic(instance)
50
+ : structuredClone(this.data.logic)
51
+ : {};
52
+ }
53
+ if (this.data.data) {
54
+ instance.data = this.data.data
55
+ ? typeof this.data.data == "function"
56
+ ? this.data.data(instance)
57
+ : structuredClone(this.data.data)
58
+ : {};
59
+ }
60
+ if (data.state) {
61
+ instance.state = structuredClone(data.state);
62
+ }
63
+ const node = instance.getNode();
64
+ const map = TraitInstanceMap.getMap(data.type);
65
+ map.addNode(node, instance);
66
+ return instance;
67
+ }
68
+ destory(instance) {
69
+ const node = instance.getNode();
70
+ const map = TraitInstanceMap.getMap(instance.type);
71
+ map.removeNode(node, instance);
72
+ this.return(instance);
73
+ }
74
+ }
package/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ export * from "./NCS";
2
+ export * from "./Graphs/Graph";
3
+ export * from "./Components/ComponentData";
4
+ export * from "./Components/ComponentInstance";
5
+ export * from "./Traits/TraitData";
6
+ export * from "./Traits/TraitInstance";
7
+ export * from "./Nodes/NodeData";
8
+ export * from "./Nodes/NodeInstance";
package/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export * from "./NCS";
2
+ export * from "./Graphs/Graph";
3
+ export * from "./Components/ComponentData";
4
+ export * from "./Components/ComponentInstance";
5
+ export * from "./Traits/TraitData";
6
+ export * from "./Traits/TraitInstance";
7
+ export * from "./Nodes/NodeData";
8
+ export * from "./Nodes/NodeInstance";
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@amodx/ncs",
3
+ "version": "0.0.01",
4
+ "module": "index.js",
5
+ "types": "index.d.ts",
6
+ "type": "module",
7
+ "description": "NCS (Node Component System) is a library for the base of any NCS/ECS system.",
8
+ "keywords": [],
9
+ "scripts": {
10
+ "build": "mkdir -p dist && rm -rf dist/* && cp package.json dist/package.json && cd ./src && npx tsc",
11
+ "compile": "cd ./src && npx tsc --watch"
12
+ },
13
+ "repository": {
14
+ "url": "git+https://github.com/Amodx/Libraries.git"
15
+ },
16
+ "bugs": {
17
+ "url": "https://github.com/Amodx/Libraries/issues"
18
+ },
19
+ "homepage": "https://github.com/Amodx/Libraries",
20
+ "author": "Amodx",
21
+ "license": "MIT",
22
+ "devDependencies": {},
23
+ "main": "index.js",
24
+ "publishConfig": {
25
+ "access": "public"
26
+ }
27
+ }