@amodx/ncs 0.0.23 → 0.0.27

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 (46) hide show
  1. package/Components/Component.types.d.ts +6 -1
  2. package/Components/ComponentArray.d.ts +4 -2
  3. package/Components/ComponentArray.js +19 -12
  4. package/Components/ComponentCursor.d.ts +8 -3
  5. package/Components/ComponentCursor.js +27 -9
  6. package/Contexts/ContextArray.d.ts +1 -1
  7. package/Contexts/ContextArray.js +6 -2
  8. package/Contexts/ContextCursor.d.ts +2 -1
  9. package/Contexts/ContextCursor.js +9 -1
  10. package/Data/serializeComponent.js +1 -1
  11. package/Data/serializeNode.js +4 -0
  12. package/Functional.d.ts +3 -3
  13. package/Functional.js +5 -3
  14. package/Graphs/Graph.d.ts +2 -1
  15. package/Graphs/Graph.js +20 -17
  16. package/Graphs/GraphClock.d.ts +8 -0
  17. package/Graphs/GraphClock.js +18 -0
  18. package/NCS.d.ts +10 -1
  19. package/Nodes/Node.types.d.ts +2 -2
  20. package/Nodes/NodeArray.d.ts +1 -0
  21. package/Nodes/NodeArray.js +6 -3
  22. package/Nodes/NodeComponents.d.ts +3 -3
  23. package/Nodes/NodeComponents.js +34 -13
  24. package/Nodes/NodeContext.js +6 -6
  25. package/Nodes/NodeCursor.d.ts +1 -1
  26. package/Nodes/NodeCursor.js +22 -11
  27. package/Nodes/NodeId.d.ts +3 -0
  28. package/Nodes/NodeId.js +10 -0
  29. package/Nodes/NodeTags.js +2 -0
  30. package/Register/registerComponent.d.ts +12 -2
  31. package/Register/registerComponent.js +71 -14
  32. package/Schema/Functions/createSchemaBinaryObjectCursorClass.js +4 -4
  33. package/Schema/Functions/createSchemaObjectCursorClass.js +4 -4
  34. package/Schema/Functions/createSchemaTypedArrayCursorClass.js +4 -4
  35. package/Schema/Schema.js +14 -10
  36. package/Schema/SchemaArrayCursor.d.ts +1 -2
  37. package/Schema/SchemaArrayCursor.js +2 -6
  38. package/Schema/SchemaView.d.ts +3 -0
  39. package/Schema/SchemaView.js +16 -14
  40. package/Systems/System.types.d.ts +2 -1
  41. package/Systems/SystemInstance.d.ts +2 -1
  42. package/Systems/SystemInstance.js +2 -2
  43. package/Tags/Tag.js +1 -1
  44. package/Tags/TagArray.js +1 -1
  45. package/Util/ItemPool.js +1 -1
  46. package/package.json +1 -1
package/Schema/Schema.js CHANGED
@@ -11,11 +11,14 @@ 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?.length) {
14
+ if (typeof data.value == "object" &&
15
+ data.value !== null &&
16
+ !data.children?.length &&
17
+ !Array.isArray(data.value)) {
15
18
  data.children ??= [];
16
19
  for (const key in data.value) {
17
20
  const value = data.value[key];
18
- const meta = structuredClone(data.meta || {});
21
+ const meta = data.meta || {};
19
22
  meta.child = true;
20
23
  data.children.push({
21
24
  id: key,
@@ -60,8 +63,8 @@ const traverseArray = (parent, data, meta) => {
60
63
  for (let i = 0; i < parent.children.length; i++) {
61
64
  const property = parent.children[i];
62
65
  if (!property.children) {
63
- data[Number(property.index)] = structuredClone(property.value);
64
- property.meta && (meta[property.index] = structuredClone(property.meta));
66
+ data[Number(property.index)] = property.value;
67
+ property.meta && (meta[property.index] = property.meta);
65
68
  }
66
69
  else {
67
70
  traverseArray(property, data, meta);
@@ -81,7 +84,7 @@ function buildMeta(data, meta, metaOverrides) {
81
84
  function traverseCreateFromObject(object, property) {
82
85
  for (const id in object) {
83
86
  const value = object[id];
84
- if (typeof value == "object") {
87
+ if (typeof value == "object" && value !== null) {
85
88
  property.children.push(traverseCreateFromObject(value, {
86
89
  id,
87
90
  value: {},
@@ -115,7 +118,11 @@ export class Schema {
115
118
  const data = [];
116
119
  for (const id in shape) {
117
120
  const value = shape[id];
118
- if (typeof value == "object") {
121
+ if (typeof value == "object" &&
122
+ value !== null &&
123
+ typeof value !== "undefined" &&
124
+ !Array.isArray(id) &&
125
+ !ArrayBuffer.isView(value)) {
119
126
  data.push(traverseCreateFromObject(value, {
120
127
  id,
121
128
  value: {},
@@ -155,10 +162,7 @@ export class Schema {
155
162
  }
156
163
  createData(newData = [], overrides) {
157
164
  for (let i = 0; i < this._data.length; i++) {
158
- newData[i] =
159
- typeof this._data[i] == "object"
160
- ? structuredClone(this._data[i])
161
- : this._data[i];
165
+ newData[i] = this._data[i];
162
166
  }
163
167
  traverseCreateData(this.root, newData, overrides);
164
168
  return newData;
@@ -3,8 +3,7 @@ import { SchemaArray } from "./SchemaArray";
3
3
  export declare class SchemaArrayCursor {
4
4
  schemaArray: SchemaArray;
5
5
  constructor(schemaArray: SchemaArray);
6
- get data(): any;
7
- set data(value: any);
6
+ _cachedData: any | null;
8
7
  _index: number;
9
8
  setIndex(index: number): void;
10
9
  getObserver(propertyIndex: number): Observable<any> | null;
@@ -5,15 +5,11 @@ export class SchemaArrayCursor {
5
5
  constructor(schemaArray) {
6
6
  this.schemaArray = schemaArray;
7
7
  }
8
- get data() {
9
- return this.schemaArray._data[this._index];
10
- }
11
- set data(value) {
12
- this.schemaArray._data[this._index] = value;
13
- }
8
+ _cachedData = null;
14
9
  _index = 0;
15
10
  setIndex(index) {
16
11
  this._index = index;
12
+ this._cachedData = this.schemaArray._data[index] ?? null;
17
13
  }
18
14
  getObserver(propertyIndex) {
19
15
  return this.schemaArray.getObserver(propertyIndex, this._index);
@@ -12,8 +12,11 @@ export declare class SchemaView<Shape extends {} = any> {
12
12
  _createData: SchemaCreateData;
13
13
  private _cursorClass;
14
14
  _dataPool: ItemPool<unknown>;
15
+ _cursorPool: ItemPool<SchemaCursor<Shape>>;
16
+ private _tempData;
15
17
  constructor(schema: Schema<Shape>, id: string, meta: PropertyMetaData[], byteOffset: number[], byteSize: number, _createData: SchemaCreateData, _cursorClass: any);
16
18
  returnData(returnData: any): void;
19
+ returnCursor(cursor: SchemaCursor): void;
17
20
  createData(overrides?: RecursivePartial<Shape> | null): any;
18
21
  createCursor(): SchemaCursor<Shape>;
19
22
  /** Converts data for use of remote components */
@@ -13,7 +13,6 @@ function traverseCreateJSON(parent, target, source) {
13
13
  }
14
14
  return target;
15
15
  }
16
- const tempData = [];
17
16
  export class SchemaView {
18
17
  schema;
19
18
  id;
@@ -23,6 +22,8 @@ export class SchemaView {
23
22
  _createData;
24
23
  _cursorClass;
25
24
  _dataPool = new ItemPool();
25
+ _cursorPool = new ItemPool();
26
+ _tempData = [];
26
27
  constructor(schema, id, meta, byteOffset, byteSize, _createData, _cursorClass) {
27
28
  this.schema = schema;
28
29
  this.id = id;
@@ -35,20 +36,20 @@ export class SchemaView {
35
36
  returnData(returnData) {
36
37
  this._dataPool.addItem(returnData);
37
38
  }
39
+ returnCursor(cursor) {
40
+ this._cursorPool.addItem(cursor);
41
+ }
38
42
  createData(overrides) {
39
43
  const data = this._createData;
40
- overrides && (tempData.length = 0);
44
+ overrides && (this._tempData.length = 0);
41
45
  let baseData = !overrides
42
46
  ? this.schema._data
43
- : this.schema.createData(tempData, overrides);
47
+ : this.schema.createData(this._tempData, overrides);
44
48
  if (this._dataPool.items.length) {
45
49
  const newData = this._dataPool.get();
46
50
  if (data.type == "object") {
47
51
  for (let i = 0; i < baseData.length; i++) {
48
- newData[i] =
49
- typeof baseData[i] == "object"
50
- ? structuredClone(baseData[i])
51
- : baseData[i];
52
+ newData[i] = baseData[i];
52
53
  }
53
54
  return newData;
54
55
  }
@@ -69,10 +70,7 @@ export class SchemaView {
69
70
  if (data.type == "object") {
70
71
  const newData = new Array(baseData.length);
71
72
  for (let i = 0; i < baseData.length; i++) {
72
- newData[i] =
73
- typeof baseData[i] == "object"
74
- ? structuredClone(baseData[i])
75
- : baseData[i];
73
+ newData[i] = baseData[i];
76
74
  }
77
75
  return newData;
78
76
  }
@@ -106,18 +104,22 @@ export class SchemaView {
106
104
  throw new Error(`NCS: Invalid create data`);
107
105
  }
108
106
  createCursor() {
107
+ const pooled = this._cursorPool.get();
108
+ if (pooled) {
109
+ return pooled;
110
+ }
109
111
  return new this._cursorClass(this, this.meta, this._createData, this.byteOffset);
110
112
  }
111
113
  /** Converts data for use of remote components */
112
114
  toRemote(cursor) {
113
115
  if (this._createData.type == "object") {
114
- return cursor.__cursor.data;
116
+ return cursor.__cursor._cachedData;
115
117
  }
116
118
  if (this._createData.type == "typed-array") {
117
- return cursor.__cursor.data;
119
+ return cursor.__cursor._cachedData;
118
120
  }
119
121
  if (this._createData.type == "binary-object") {
120
- return cursor.__cursor.data.buffer;
122
+ return cursor.__cursor._cachedData.buffer;
121
123
  }
122
124
  }
123
125
  /** Converts data for remote data to local data*/
@@ -1,3 +1,4 @@
1
+ import { GraphClock } from "../Graphs/GraphClock";
1
2
  import { QueryPrototype } from "../Queries/QueryPrototype";
2
3
  import { SystemInstance } from "./SystemInstance";
3
4
  export type SystemRegisterData = {
@@ -12,5 +13,5 @@ export type SystemRegisterData = {
12
13
  /**
13
14
  * The update function of the system.
14
15
  */
15
- update(system: SystemInstance, delta: number): void;
16
+ update(system: SystemInstance, clock: GraphClock): void;
16
17
  };
@@ -2,12 +2,13 @@ import { Graph } from "../Graphs/Graph";
2
2
  import { SystemRegisterData } from "./System.types";
3
3
  import { QueryInstance } from "../Queries/QueryInstance";
4
4
  import { NodeCursor } from "../Nodes/NodeCursor";
5
+ import { GraphClock } from "../Graphs/GraphClock";
5
6
  export declare class SystemInstance {
6
7
  graph: Graph;
7
8
  proto: SystemRegisterData;
8
9
  queries: QueryInstance[];
9
10
  node: NodeCursor;
10
11
  constructor(graph: Graph, proto: SystemRegisterData);
11
- update(delta: number): void;
12
+ update(clock: GraphClock): void;
12
13
  dispose(): void;
13
14
  }
@@ -12,8 +12,8 @@ export class SystemInstance {
12
12
  }
13
13
  this.graph._systems.push(this);
14
14
  }
15
- update(delta) {
16
- this.proto.update(this, delta);
15
+ update(clock) {
16
+ this.proto.update(this, clock);
17
17
  }
18
18
  dispose() {
19
19
  for (let i = 0; i < this.graph._systems.length; i++) {
package/Tags/Tag.js CHANGED
@@ -12,7 +12,7 @@ export class Tag {
12
12
  *traverseChildren() {
13
13
  const children = [...this.children];
14
14
  while (children.length) {
15
- const child = children.shift();
15
+ const child = children.pop();
16
16
  yield child;
17
17
  if (child.children.length)
18
18
  children.push(...child.children);
package/Tags/TagArray.js CHANGED
@@ -18,7 +18,7 @@ export class TagArray {
18
18
  }
19
19
  addTag(node) {
20
20
  let slot = this._freeSlots.length
21
- ? this._freeSlots.shift()
21
+ ? this._freeSlots.pop()
22
22
  : this._node.length;
23
23
  this._node[slot] = node;
24
24
  this.observers.tagAdded.notify(slot);
package/Util/ItemPool.js CHANGED
@@ -8,7 +8,7 @@ export class ItemPool {
8
8
  return true;
9
9
  }
10
10
  get() {
11
- const item = this.items.shift();
11
+ const item = this.items.pop();
12
12
  if (!item)
13
13
  return null;
14
14
  return item;
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@amodx/ncs","version":"0.0.23","module":"index.js","types":"index.d.ts","type":"module","description":"NCS (Node Component System) is a library for the base of any NCS/ECS system.","keywords":[],"scripts":{"build":"mkdir -p dist && rm -rf dist/* && cp package.json dist/package.json && cd ./src && npx tsc","compile":"cd ./src && npx tsc --watch"},"repository":{"url":"git+https://github.com/Amodx/Libraries.git"},"bugs":{"url":"https://github.com/Amodx/Libraries/issues"},"homepage":"https://github.com/Amodx/Libraries","author":"Amodx","license":"MIT","devDependencies":{},"main":"index.js","publishConfig":{"access":"public"}}
1
+ {"name":"@amodx/ncs","version":"0.0.27","module":"index.js","types":"index.d.ts","type":"module","description":"NCS (Node Component System) is a library for the base of any NCS/ECS system.","keywords":[],"scripts":{"build":"mkdir -p dist && rm -rf dist/* && cp package.json dist/package.json && cd ./src && npx tsc","compile":"cd ./src && npx tsc --watch"},"repository":{"url":"git+https://github.com/Amodx/Libraries.git"},"bugs":{"url":"https://github.com/Amodx/Libraries/issues"},"homepage":"https://github.com/Amodx/Libraries","author":"Amodx","license":"MIT","devDependencies":{},"main":"index.js","publishConfig":{"access":"public"}}