@colyseus/schema 3.0.76 → 4.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 (160) hide show
  1. package/build/cjs/index.js +781 -434
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/index.mjs +779 -435
  4. package/build/esm/index.mjs.map +1 -1
  5. package/build/umd/index.js +781 -434
  6. package/lib/Metadata.js +1 -5
  7. package/lib/Metadata.js.map +1 -1
  8. package/lib/Reflection.d.ts +50 -17
  9. package/lib/Reflection.js +151 -202
  10. package/lib/Reflection.js.map +1 -1
  11. package/lib/Schema.d.ts +13 -1
  12. package/lib/Schema.js +73 -9
  13. package/lib/Schema.js.map +1 -1
  14. package/lib/annotations.d.ts +6 -1
  15. package/lib/annotations.js +8 -34
  16. package/lib/annotations.js.map +1 -1
  17. package/lib/bench_encode.js +34 -1
  18. package/lib/bench_encode.js.map +1 -1
  19. package/lib/codegen/api.js +35 -2
  20. package/lib/codegen/api.js.map +1 -1
  21. package/lib/codegen/cli.js +4 -1
  22. package/lib/codegen/cli.js.map +1 -1
  23. package/lib/codegen/parser.js +35 -2
  24. package/lib/codegen/parser.js.map +1 -1
  25. package/lib/codegen/types.js +34 -1
  26. package/lib/codegen/types.js.map +1 -1
  27. package/lib/decoder/DecodeOperation.d.ts +2 -2
  28. package/lib/decoder/DecodeOperation.js +3 -3
  29. package/lib/decoder/DecodeOperation.js.map +1 -1
  30. package/lib/decoder/Decoder.d.ts +3 -3
  31. package/lib/decoder/Decoder.js +2 -2
  32. package/lib/decoder/Decoder.js.map +1 -1
  33. package/lib/decoder/ReferenceTracker.d.ts +0 -1
  34. package/lib/decoder/ReferenceTracker.js +9 -7
  35. package/lib/decoder/ReferenceTracker.js.map +1 -1
  36. package/lib/decoder/strategy/Callbacks.d.ts +154 -0
  37. package/lib/decoder/strategy/Callbacks.js +340 -0
  38. package/lib/decoder/strategy/Callbacks.js.map +1 -0
  39. package/lib/decoder/strategy/{StateCallbacks.d.ts → getDecoderStateCallbacks.d.ts} +6 -0
  40. package/lib/decoder/strategy/{StateCallbacks.js → getDecoderStateCallbacks.js} +17 -10
  41. package/lib/decoder/strategy/getDecoderStateCallbacks.js.map +1 -0
  42. package/lib/encoder/ChangeTree.d.ts +2 -2
  43. package/lib/encoder/ChangeTree.js.map +1 -1
  44. package/lib/encoder/EncodeOperation.d.ts +2 -2
  45. package/lib/encoder/EncodeOperation.js +3 -3
  46. package/lib/encoder/EncodeOperation.js.map +1 -1
  47. package/lib/encoder/Encoder.d.ts +6 -6
  48. package/lib/encoder/Encoder.js +19 -18
  49. package/lib/encoder/Encoder.js.map +1 -1
  50. package/lib/encoder/Root.js +17 -14
  51. package/lib/encoder/Root.js.map +1 -1
  52. package/lib/encoder/StateView.js +13 -12
  53. package/lib/encoder/StateView.js.map +1 -1
  54. package/lib/encoding/decode.d.ts +2 -2
  55. package/lib/encoding/encode.d.ts +3 -1
  56. package/lib/encoding/encode.js.map +1 -1
  57. package/lib/index.d.ts +3 -2
  58. package/lib/index.js +7 -3
  59. package/lib/index.js.map +1 -1
  60. package/lib/types/HelperTypes.d.ts +7 -14
  61. package/lib/types/HelperTypes.js.map +1 -1
  62. package/lib/types/custom/ArraySchema.d.ts +2 -1
  63. package/lib/types/custom/ArraySchema.js.map +1 -1
  64. package/lib/types/custom/CollectionSchema.d.ts +2 -1
  65. package/lib/types/custom/CollectionSchema.js.map +1 -1
  66. package/lib/types/custom/MapSchema.d.ts +3 -2
  67. package/lib/types/custom/MapSchema.js.map +1 -1
  68. package/lib/types/custom/SetSchema.d.ts +2 -1
  69. package/lib/types/custom/SetSchema.js.map +1 -1
  70. package/lib/types/symbols.d.ts +1 -0
  71. package/lib/types/symbols.js +2 -1
  72. package/lib/types/symbols.js.map +1 -1
  73. package/lib/utils.js +1 -1
  74. package/lib/utils.js.map +1 -1
  75. package/package.json +12 -16
  76. package/src/Metadata.ts +1 -5
  77. package/src/Reflection.ts +185 -174
  78. package/src/Schema.ts +81 -13
  79. package/src/annotations.ts +14 -40
  80. package/src/codegen/parser.ts +1 -1
  81. package/src/decoder/DecodeOperation.ts +9 -9
  82. package/src/decoder/Decoder.ts +6 -6
  83. package/src/decoder/ReferenceTracker.ts +10 -8
  84. package/src/decoder/strategy/Callbacks.ts +547 -0
  85. package/src/decoder/strategy/{StateCallbacks.ts → getDecoderStateCallbacks.ts} +17 -11
  86. package/src/encoder/ChangeTree.ts +4 -7
  87. package/src/encoder/EncodeOperation.ts +9 -9
  88. package/src/encoder/Encoder.ts +47 -22
  89. package/src/encoder/Root.ts +20 -15
  90. package/src/encoder/StateView.ts +15 -13
  91. package/src/encoding/encode.ts +1 -1
  92. package/src/index.ts +3 -2
  93. package/src/types/HelperTypes.ts +13 -11
  94. package/src/types/custom/ArraySchema.ts +2 -1
  95. package/src/types/custom/CollectionSchema.ts +4 -2
  96. package/src/types/custom/MapSchema.ts +4 -2
  97. package/src/types/custom/SetSchema.ts +3 -1
  98. package/src/types/symbols.ts +1 -0
  99. package/src/utils.ts +2 -2
  100. package/lib/Decoder.d.ts +0 -16
  101. package/lib/Decoder.js +0 -182
  102. package/lib/Decoder.js.map +0 -1
  103. package/lib/Encoder.d.ts +0 -13
  104. package/lib/Encoder.js +0 -79
  105. package/lib/Encoder.js.map +0 -1
  106. package/lib/changes/ChangeSet.d.ts +0 -12
  107. package/lib/changes/ChangeSet.js +0 -35
  108. package/lib/changes/ChangeSet.js.map +0 -1
  109. package/lib/changes/ChangeTree.d.ts +0 -53
  110. package/lib/changes/ChangeTree.js +0 -202
  111. package/lib/changes/ChangeTree.js.map +0 -1
  112. package/lib/changes/DecodeOperation.d.ts +0 -15
  113. package/lib/changes/DecodeOperation.js +0 -186
  114. package/lib/changes/DecodeOperation.js.map +0 -1
  115. package/lib/changes/EncodeOperation.d.ts +0 -18
  116. package/lib/changes/EncodeOperation.js +0 -130
  117. package/lib/changes/EncodeOperation.js.map +0 -1
  118. package/lib/changes/ReferenceTracker.d.ts +0 -14
  119. package/lib/changes/ReferenceTracker.js +0 -83
  120. package/lib/changes/ReferenceTracker.js.map +0 -1
  121. package/lib/changes/consts.d.ts +0 -14
  122. package/lib/changes/consts.js +0 -18
  123. package/lib/changes/consts.js.map +0 -1
  124. package/lib/decoder/strategy/StateCallbacks.js.map +0 -1
  125. package/lib/decoding/decode.d.ts +0 -48
  126. package/lib/decoding/decode.js +0 -267
  127. package/lib/decoding/decode.js.map +0 -1
  128. package/lib/ecs.d.ts +0 -11
  129. package/lib/ecs.js +0 -160
  130. package/lib/ecs.js.map +0 -1
  131. package/lib/filters/index.d.ts +0 -8
  132. package/lib/filters/index.js +0 -24
  133. package/lib/filters/index.js.map +0 -1
  134. package/lib/spec.d.ts +0 -13
  135. package/lib/spec.js +0 -42
  136. package/lib/spec.js.map +0 -1
  137. package/lib/types/ArraySchema.d.ts +0 -238
  138. package/lib/types/ArraySchema.js +0 -555
  139. package/lib/types/ArraySchema.js.map +0 -1
  140. package/lib/types/CollectionSchema.d.ts +0 -35
  141. package/lib/types/CollectionSchema.js +0 -150
  142. package/lib/types/CollectionSchema.js.map +0 -1
  143. package/lib/types/MapSchema.d.ts +0 -38
  144. package/lib/types/MapSchema.js +0 -215
  145. package/lib/types/MapSchema.js.map +0 -1
  146. package/lib/types/SetSchema.d.ts +0 -32
  147. package/lib/types/SetSchema.js +0 -162
  148. package/lib/types/SetSchema.js.map +0 -1
  149. package/lib/types/typeRegistry.d.ts +0 -5
  150. package/lib/types/typeRegistry.js +0 -13
  151. package/lib/types/typeRegistry.js.map +0 -1
  152. package/lib/usage.d.ts +0 -1
  153. package/lib/usage.js +0 -22
  154. package/lib/usage.js.map +0 -1
  155. package/lib/v3.d.ts +0 -1
  156. package/lib/v3.js +0 -427
  157. package/lib/v3.js.map +0 -1
  158. package/lib/v3_experiment.d.ts +0 -1
  159. package/lib/v3_experiment.js +0 -407
  160. package/lib/v3_experiment.js.map +0 -1
@@ -1,162 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SetSchema = void 0;
4
- const ChangeTree_1 = require("../changes/ChangeTree");
5
- const spec_1 = require("../spec");
6
- const utils_1 = require("./utils");
7
- class SetSchema {
8
- onAdd(callback, triggerAll = true) {
9
- return (0, utils_1.addCallback)((this.$callbacks || (this.$callbacks = [])), spec_1.OPERATION.ADD, callback, (triggerAll)
10
- ? this.$items
11
- : undefined);
12
- }
13
- onRemove(callback) { return (0, utils_1.addCallback)(this.$callbacks || (this.$callbacks = []), spec_1.OPERATION.DELETE, callback); }
14
- onChange(callback) { return (0, utils_1.addCallback)(this.$callbacks || (this.$callbacks = []), spec_1.OPERATION.REPLACE, callback); }
15
- static is(type) {
16
- return type['set'] !== undefined;
17
- }
18
- constructor(initialValues) {
19
- this.$changes = new ChangeTree_1.ChangeTree(this);
20
- this.$items = new Map();
21
- this.$indexes = new Map();
22
- this.$refId = 0;
23
- if (initialValues) {
24
- initialValues.forEach((v) => this.add(v));
25
- }
26
- }
27
- add(value) {
28
- // immediatelly return false if value already added.
29
- if (this.has(value)) {
30
- return false;
31
- }
32
- // set "index" for reference.
33
- const index = this.$refId++;
34
- if ((value['$changes']) !== undefined) {
35
- value['$changes'].setParent(this, this.$changes.root, index);
36
- }
37
- const operation = this.$changes.indexes[index]?.op ?? spec_1.OPERATION.ADD;
38
- this.$changes.indexes[index] = index;
39
- this.$indexes.set(index, index);
40
- this.$items.set(index, value);
41
- this.$changes.change(index, operation);
42
- return index;
43
- }
44
- entries() {
45
- return this.$items.entries();
46
- }
47
- delete(item) {
48
- const entries = this.$items.entries();
49
- let index;
50
- let entry;
51
- while (entry = entries.next()) {
52
- if (entry.done) {
53
- break;
54
- }
55
- if (item === entry.value[1]) {
56
- index = entry.value[0];
57
- break;
58
- }
59
- }
60
- if (index === undefined) {
61
- return false;
62
- }
63
- this.$changes.delete(index);
64
- this.$indexes.delete(index);
65
- return this.$items.delete(index);
66
- }
67
- clear(changes) {
68
- // discard previous operations.
69
- this.$changes.discard(true, true);
70
- this.$changes.indexes = {};
71
- // clear previous indexes
72
- this.$indexes.clear();
73
- //
74
- // When decoding:
75
- // - enqueue items for DELETE callback.
76
- // - flag child items for garbage collection.
77
- //
78
- if (changes) {
79
- utils_1.removeChildRefs.call(this, changes);
80
- }
81
- // clear items
82
- this.$items.clear();
83
- this.$changes.operation({ index: 0, op: spec_1.OPERATION.CLEAR });
84
- // touch all structures until reach root
85
- this.$changes.touchParents();
86
- }
87
- has(value) {
88
- const values = this.$items.values();
89
- let has = false;
90
- let entry;
91
- while (entry = values.next()) {
92
- if (entry.done) {
93
- break;
94
- }
95
- if (value === entry.value) {
96
- has = true;
97
- break;
98
- }
99
- }
100
- return has;
101
- }
102
- forEach(callbackfn) {
103
- this.$items.forEach((value, key, _) => callbackfn(value, key, this));
104
- }
105
- values() {
106
- return this.$items.values();
107
- }
108
- get size() {
109
- return this.$items.size;
110
- }
111
- setIndex(index, key) {
112
- this.$indexes.set(index, key);
113
- }
114
- getIndex(index) {
115
- return this.$indexes.get(index);
116
- }
117
- getByIndex(index) {
118
- return this.$items.get(this.$indexes.get(index));
119
- }
120
- deleteByIndex(index) {
121
- const key = this.$indexes.get(index);
122
- this.$items.delete(key);
123
- this.$indexes.delete(index);
124
- }
125
- toArray() {
126
- return Array.from(this.$items.values());
127
- }
128
- toJSON() {
129
- const values = [];
130
- this.forEach((value, key) => {
131
- values.push((typeof (value['toJSON']) === "function")
132
- ? value['toJSON']()
133
- : value);
134
- });
135
- return values;
136
- }
137
- //
138
- // Decoding utilities
139
- //
140
- clone(isDecoding) {
141
- let cloned;
142
- if (isDecoding) {
143
- // client-side
144
- cloned = Object.assign(new SetSchema(), this);
145
- }
146
- else {
147
- // server-side
148
- cloned = new SetSchema();
149
- this.forEach((value) => {
150
- if (value['$changes']) {
151
- cloned.add(value['clone']());
152
- }
153
- else {
154
- cloned.add(value);
155
- }
156
- });
157
- }
158
- return cloned;
159
- }
160
- }
161
- exports.SetSchema = SetSchema;
162
- //# sourceMappingURL=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ION } from \"../spec\";\nimport { SchemaDecoderCallbacks } from \"../Schema\";\nimport { addCallback, removeChildRefs } from \"./utils\";\nimport { DataChange } from \"..\";\n\nexport class SetSchema<V=any> implements SchemaDecoderCallbacks {\n protected $changes: ChangeTree = new ChangeTree(this);\n\n protected $items: Map<number, V> = new Map<number, V>();\n protected $indexes: Map<number, number> = new Map<number, number>();\n\n protected $refId: number = 0;\n\n //\n // Decoding callbacks\n //\n public $callbacks: { [operation: number]: Array<(item: V, key: string) => void> };\n public onAdd(callback: (item: V, key: string) => void, triggerAll: boolean = true) {\n return addCallback(\n (this.$callbacks || (this.$callbacks = [])),\n OPERATION.ADD,\n callback,\n (triggerAll)\n ? this.$items\n : undefined\n );\n }\n public onRemove(callback: (item: V, key: string) => void) { return addCallback(this.$callbacks || (this.$callbacks = []), OPERATION.DELETE, callback); }\n public onChange(callback: (item: V, key: string) => void) { return addCallback(this.$callbacks || (this.$callbacks = []), OPERATION.REPLACE, callback); }\n\n static is(type: any) {\n return type['set'] !== undefined;\n }\n\n constructor (initialValues?: Array<V>) {\n if (initialValues) {\n initialValues.forEach((v) => this.add(v));\n }\n }\n\n add(value: V) {\n // immediatelly return false if value already added.\n if (this.has(value)) { return false; }\n\n // set \"index\" for reference.\n const index = this.$refId++;\n\n if ((value['$changes']) !== undefined) {\n (value['$changes'] as ChangeTree).setParent(this, this.$changes.root, index);\n }\n\n const operation = this.$changes.indexes[index]?.op ?? OPERATION.ADD;\n\n this.$changes.indexes[index] = index;\n\n this.$indexes.set(index, index);\n this.$items.set(index, value);\n\n this.$changes.change(index, operation);\n return index;\n }\n\n entries () {\n return this.$items.entries();\n }\n\n delete(item: V) {\n const entries = this.$items.entries();\n\n let index: number;\n let entry: IteratorResult<[number, V]>;\n while (entry = entries.next()) {\n if (entry.done) { break; }\n\n if (item === entry.value[1]) {\n index = entry.value[0];\n break;\n }\n }\n\n if (index === undefined) {\n return false;\n }\n\n this.$changes.delete(index);\n this.$indexes.delete(index);\n\n return this.$items.delete(index);\n }\n\n clear(changes?: DataChange[]) {\n // discard previous operations.\n this.$changes.discard(true, true);\n this.$changes.indexes = {};\n\n // clear previous indexes\n this.$indexes.clear();\n\n //\n // When decoding:\n // - enqueue items for DELETE callback.\n // - flag child items for garbage collection.\n //\n if (changes) {\n removeChildRefs.call(this, changes);\n }\n\n // clear items\n this.$items.clear();\n\n this.$changes.operation({ index: 0, op: OPERATION.CLEAR });\n\n // touch all structures until reach root\n this.$changes.touchParents();\n }\n\n has (value: V): boolean {\n const values = this.$items.values();\n\n let has = false;\n let entry: IteratorResult<V>;\n\n while (entry = values.next()) {\n if (entry.done) { break; }\n if (value === entry.value) {\n has = true;\n break;\n }\n }\n\n return has;\n }\n\n forEach(callbackfn: (value: V, key: number, collection: SetSchema<V>) => void) {\n this.$items.forEach((value, key, _) => callbackfn(value, key, this));\n }\n\n values() {\n return this.$items.values();\n }\n\n get size () {\n return this.$items.size;\n }\n\n protected setIndex(index: number, key: number) {\n this.$indexes.set(index, key);\n }\n\n protected getIndex(index: number) {\n return this.$indexes.get(index);\n }\n\n protected getByIndex(index: number) {\n return this.$items.get(this.$indexes.get(index));\n }\n\n protected deleteByIndex(index: number) {\n const key = this.$indexes.get(index);\n this.$items.delete(key);\n this.$indexes.delete(index);\n }\n\n toArray() {\n return Array.from(this.$items.values());\n }\n\n toJSON() {\n const values: V[] = [];\n\n this.forEach((value, key) => {\n values.push(\n (typeof (value['toJSON']) === \"function\")\n ? value['toJSON']()\n : value\n );\n });\n\n return values;\n }\n\n //\n // Decoding utilities\n //\n clone(isDecoding?: boolean): SetSchema<V> {\n let cloned: SetSchema;\n\n if (isDecoding) {\n // client-side\n cloned = Object.assign(new SetSchema(), this);\n\n } else {\n // server-side\n cloned = new SetSchema();\n this.forEach((value) => {\n if (value['$changes']) {\n cloned.add(value['clone']());\n } else {\n cloned.add(value);\n }\n })\n }\n\n return cloned;\n }\n\n}"]}
@@ -1,5 +0,0 @@
1
- export interface TypeDefinition {
2
- constructor: any;
3
- }
4
- export declare function registerType(identifier: string, definition: TypeDefinition): void;
5
- export declare function getType(identifier: string): TypeDefinition;
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getType = exports.registerType = void 0;
4
- const registeredTypes = {};
5
- function registerType(identifier, definition) {
6
- registeredTypes[identifier] = definition;
7
- }
8
- exports.registerType = registerType;
9
- function getType(identifier) {
10
- return registeredTypes[identifier];
11
- }
12
- exports.getType = getType;
13
- //# sourceMappingURL=typeRegistry.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"typeRegistry.js","sourceRoot":"","sources":["../../src/types/typeRegistry.ts"],"names":[],"mappings":";;;AAUA,MAAM,eAAe,GAA4C,EAAE,CAAC;AAEpE,SAAgB,YAAY,CAAC,UAAkB,EAAE,UAA0B;IACvE,eAAe,CAAC,UAAU,CAAC,GAAG,UAAU,CAAC;AAC7C,CAAC;AAFD,oCAEC;AAED,SAAgB,OAAO,CAAC,UAAkB;IACtC,OAAO,eAAe,CAAC,UAAU,CAAC,CAAC;AACvC,CAAC;AAFD,0BAEC","sourcesContent":["export interface TypeDefinition {\n constructor: any,\n\n // //\n // // TODO: deprecate proxy on next version\n // // the proxy is used for compatibility with versions <1.0.0 of @colyseus/schema\n // //\n // getProxy?: any,\n}\n\nconst registeredTypes: {[identifier: string] : TypeDefinition} = {};\n\nexport function registerType(identifier: string, definition: TypeDefinition) {\n registeredTypes[identifier] = definition;\n}\n\nexport function getType(identifier: string): TypeDefinition {\n return registeredTypes[identifier];\n}\n"]}
package/lib/usage.d.ts DELETED
@@ -1 +0,0 @@
1
- export {};
package/lib/usage.js DELETED
@@ -1,22 +0,0 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- var __metadata = (this && this.__metadata) || function (k, v) {
9
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const index_1 = require("./index");
13
- class MySchema extends index_1.Schema {
14
- }
15
- __decorate([
16
- (0, index_1.type)("string"),
17
- __metadata("design:type", String)
18
- ], MySchema.prototype, "hello", void 0);
19
- const state = new MySchema();
20
- state.hello = "world";
21
- console.log(state.encode());
22
- //# sourceMappingURL=usage.js.map
package/lib/usage.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"usage.js","sourceRoot":"","sources":["../src/usage.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,mCAAuC;AAGvC,MAAM,QAAS,SAAQ,cAAM;CAE5B;AADmB;IAAf,IAAA,YAAI,EAAC,QAAQ,CAAC;;uCAAe;AAIlC,MAAM,KAAK,GAAG,IAAI,QAAQ,EAAE,CAAC;AAC7B,KAAK,CAAC,KAAK,GAAG,OAAO,CAAC;AAEtB,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC","sourcesContent":["import { Schema, type } from \"./index\";\n\n\nclass MySchema extends Schema {\n @type(\"string\") hello: string;\n}\n\n\nconst state = new MySchema();\nstate.hello = \"world\";\n\nconsole.log(state.encode());\n\n"]}
package/lib/v3.d.ts DELETED
@@ -1 +0,0 @@
1
- import "./symbol.shim";
package/lib/v3.js DELETED
@@ -1,427 +0,0 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- var __metadata = (this && this.__metadata) || function (k, v) {
9
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const util = require("node:util");
13
- require("./symbol.shim");
14
- const annotations_1 = require("./annotations");
15
- // import { Reflection, ReflectionField, ReflectionType } from "./Reflection";
16
- const Schema_1 = require("./Schema");
17
- const ArraySchema_1 = require("./types/ArraySchema");
18
- const MapSchema_1 = require("./types/MapSchema");
19
- const Encoder_1 = require("./encoder/Encoder");
20
- const Decoder_1 = require("./decoder/Decoder");
21
- const EncodeOperation_1 = require("./encoder/EncodeOperation");
22
- const symbols_1 = require("./types/symbols");
23
- const DecodeOperation_1 = require("./decoder/DecodeOperation");
24
- const Reflection_1 = require("./Reflection");
25
- const StateView_1 = require("./encoder/StateView");
26
- function logSingleCall(label, callback) {
27
- const time = Date.now();
28
- const res = callback();
29
- console.log(`${label}:`, Date.now() - time);
30
- return res;
31
- }
32
- function logTime(label, callback) {
33
- const time = Date.now();
34
- for (let i = 0; i < 500000; i++) {
35
- callback();
36
- }
37
- console.log(`${label}:`, Date.now() - time);
38
- }
39
- // // @ts-ignore
40
- // globalThis.perform = function perform() {
41
- // for (let i = 0; i < 500000; i++) {
42
- // encoder.encodeAll();
43
- // }
44
- // }
45
- // const timeout = setInterval(() => {}, 1000);
46
- // function decorate({ get, set }, context: ClassAccessorDecoratorContext): ClassAccessorDecoratorResult<any, any> {
47
- // const field = context.name.toString();
48
- // // const fieldIndex = Metadata.addField(context.metadata, field, type);
49
- // const parent = Object.getPrototypeOf(context.metadata);
50
- // let lastIndex = (parent && parent[-1] as number) ?? -1;
51
- // lastIndex++;
52
- // context.metadata[field] = { type: "number" };
53
- // Object.defineProperty(context.metadata, lastIndex, {
54
- // value: field,
55
- // enumerable: false,
56
- // configurable: true,
57
- // });
58
- // Object.defineProperty(context.metadata, -1, {
59
- // value: lastIndex,
60
- // enumerable: false,
61
- // configurable: true
62
- // });
63
- // return {
64
- // init(value) { return value; },
65
- // get() { return get.call(this); },
66
- // set(value: any) { set.call(this, value); },
67
- // };
68
- // }
69
- // class Fruit {
70
- // @decorate accessor frutose: number = 1;
71
- // }
72
- // class Banana extends Fruit {
73
- // @decorate accessor potassium: number = 10;
74
- // }
75
- // class Berry extends Fruit {
76
- // @decorate accessor antioxidants: number = 10;
77
- // }
78
- // class Strawberry extends Berry {
79
- // @decorate accessor fiber: number = 10;
80
- // }
81
- // class Grape extends Berry {
82
- // @decorate accessor vitaminc: number = 5;
83
- // }
84
- // console.log("fruit:", Fruit[Symbol.metadata], Object.keys(Fruit[Symbol.metadata]));
85
- // console.log("banana:", Banana[Symbol.metadata], Object.keys(Banana[Symbol.metadata]));
86
- // console.log("strawberry:", Strawberry[Symbol.metadata], Object.keys(Strawberry[Symbol.metadata]));
87
- // console.log("grape:", Grape[Symbol.metadata], Object.keys(Grape[Symbol.metadata]));
88
- // console.log("GRAPE =>");
89
- // function printFields(metadata) {
90
- // let i = 0;
91
- // const len = metadata[-1]
92
- // console.log({ len });
93
- // for (let i = 0; i <= len; i++) {
94
- // console.log("over len...", i, metadata[i])
95
- // }
96
- // }
97
- // console.log("Grape...");
98
- // printFields(Grape[Symbol.metadata]);
99
- // console.log("Banana...");
100
- // printFields(Banana[Symbol.metadata]);
101
- // class Item extends Schema {
102
- // @type("string") accessor name: string;
103
- // }
104
- // class RootState extends Schema {
105
- // @type([Item]) accessor items = new ArraySchema<Item>();
106
- // }
107
- // const s = new RootState();
108
- // s.items.push(new Item().assign({ name: "hello" }));
109
- // const encoder = new Encoder(s);
110
- // const encoded = encoder.encode();
111
- // const decoder = new Decoder(new RootState());
112
- // decoder.decode(encoded);
113
- function log(message) {
114
- console.log(util.inspect(message, false, 10, true));
115
- }
116
- Schema_1.Schema[symbols_1.$encoder] = EncodeOperation_1.encodeSchemaOperation;
117
- Schema_1.Schema[symbols_1.$decoder] = DecodeOperation_1.decodeSchemaOperation;
118
- MapSchema_1.MapSchema[symbols_1.$encoder] = EncodeOperation_1.encodeKeyValueOperation;
119
- MapSchema_1.MapSchema[symbols_1.$decoder] = DecodeOperation_1.decodeKeyValueOperation;
120
- ArraySchema_1.ArraySchema[symbols_1.$encoder] = EncodeOperation_1.encodeKeyValueOperation;
121
- ArraySchema_1.ArraySchema[symbols_1.$decoder] = DecodeOperation_1.decodeKeyValueOperation;
122
- // // No need to extend Schema!
123
- // class Vec3 {
124
- // x: number;
125
- // y: number;
126
- // z: number;
127
- // constructor() {
128
- // // Need to initialize property descriptors
129
- // Schema.initialize(this);
130
- // }
131
- // }
132
- // // Define fields to encode/decode
133
- // Metadata.setFields(Vec3, {
134
- // x: "number",
135
- // y: "number",
136
- // z: "number",
137
- // });
138
- // //
139
- // Vec3[$track] = function (
140
- // changeTree: ChangeTree,
141
- // index: number,
142
- // operation: OPERATION = OPERATION.ADD
143
- // ) {
144
- // changeTree.change(index, operation);
145
- // };
146
- // Vec3[$encoder] = encodeSchemaOperation;
147
- // Vec3[$decoder] = decodeSchemaOperation;
148
- // // @ts-ignore
149
- // if (!Vec3.prototype.toJSON) { Vec3.prototype.toJSON = Schema.prototype.toJSON; }
150
- // -------------------------------------------------------------------------------
151
- class Vec3 extends Schema_1.Schema {
152
- }
153
- __decorate([
154
- (0, annotations_1.type)("number"),
155
- __metadata("design:type", Number)
156
- ], Vec3.prototype, "x", void 0);
157
- __decorate([
158
- (0, annotations_1.type)("number"),
159
- __metadata("design:type", Number)
160
- ], Vec3.prototype, "y", void 0);
161
- __decorate([
162
- (0, annotations_1.type)("number"),
163
- __metadata("design:type", Number)
164
- ], Vec3.prototype, "z", void 0);
165
- // Vec3[$track] = function (changeTree, index) {
166
- // changeTree.change(0, OPERATION.ADD);
167
- // };
168
- // Vec3[$encoder] = function (encoder, bytes, changeTree, index, operation, it) {
169
- // encode.number(bytes, changeTree.ref.x, it);
170
- // encode.number(bytes, changeTree.ref.y, it);
171
- // encode.number(bytes, changeTree.ref.z, it);
172
- // };
173
- // Vec3[$decoder] = function (
174
- // decoder: Decoder<any>,
175
- // bytes: number[],
176
- // it: decode.Iterator,
177
- // ref: Vec3,
178
- // allChanges: DataChange[]
179
- // ) {
180
- // ref.x = decode.number(bytes, it);
181
- // ref.y = decode.number(bytes, it);
182
- // ref.z = decode.number(bytes, it);
183
- // };
184
- class Base extends Schema_1.Schema {
185
- }
186
- class Entity extends Schema_1.Schema {
187
- constructor() {
188
- super(...arguments);
189
- this.position = new Vec3().assign({ x: 0, y: 0, z: 0 });
190
- }
191
- }
192
- __decorate([
193
- (0, annotations_1.type)(Vec3),
194
- __metadata("design:type", Object)
195
- ], Entity.prototype, "position", void 0);
196
- class Card extends Schema_1.Schema {
197
- }
198
- __decorate([
199
- (0, annotations_1.type)("string"),
200
- __metadata("design:type", String)
201
- ], Card.prototype, "suit", void 0);
202
- __decorate([
203
- (0, annotations_1.type)("number"),
204
- __metadata("design:type", Number)
205
- ], Card.prototype, "num", void 0);
206
- class Player extends Entity {
207
- constructor() {
208
- super(...arguments);
209
- this.rotation = new Vec3().assign({ x: 0, y: 0, z: 0 });
210
- this.secret = "private info only for this player";
211
- // @type([Card])
212
- // cards = new ArraySchema<Card>(
213
- // new Card().assign({ suit: "Hearts", num: 1 }),
214
- // new Card().assign({ suit: "Spaces", num: 2 }),
215
- // new Card().assign({ suit: "Diamonds", num: 3 }),
216
- // );
217
- }
218
- }
219
- __decorate([
220
- (0, annotations_1.type)(Vec3),
221
- __metadata("design:type", Object)
222
- ], Player.prototype, "rotation", void 0);
223
- __decorate([
224
- (0, annotations_1.type)("string"),
225
- __metadata("design:type", String)
226
- ], Player.prototype, "secret", void 0);
227
- class Team extends Schema_1.Schema {
228
- constructor() {
229
- super(...arguments);
230
- this.entities = new MapSchema_1.MapSchema();
231
- }
232
- }
233
- __decorate([
234
- (0, annotations_1.type)({ map: Entity }),
235
- __metadata("design:type", Object)
236
- ], Team.prototype, "entities", void 0);
237
- class State extends Schema_1.Schema {
238
- constructor() {
239
- super(...arguments);
240
- this.num = 0;
241
- this.str = "Hello world!";
242
- this.teams = new ArraySchema_1.ArraySchema();
243
- // @type({ map: Entity }) entities = new MapSchema<Entity>();
244
- // @type(Entity) entity1 = new Player().assign({
245
- // position: new Vec3().assign({ x: 1, y: 2, z: 3 }),
246
- // rotation: new Vec3().assign({ x: 4, y: 5, z: 6 }),
247
- // });
248
- // @type(Entity) entity2 = new Player().assign({
249
- // position: new Vec3().assign({ x: 1, y: 2, z: 3 }),
250
- // rotation: new Vec3().assign({ x: 4, y: 5, z: 6 }),
251
- // });
252
- }
253
- }
254
- __decorate([
255
- (0, annotations_1.type)("number"),
256
- __metadata("design:type", Number)
257
- ], State.prototype, "num", void 0);
258
- __decorate([
259
- (0, annotations_1.type)("string"),
260
- __metadata("design:type", Object)
261
- ], State.prototype, "str", void 0);
262
- __decorate([
263
- annotations_1.owned,
264
- (0, annotations_1.type)([Team]),
265
- __metadata("design:type", Object)
266
- ], State.prototype, "teams", void 0);
267
- const state = new State();
268
- // for (let i=0;i<1000;i++) {
269
- // state.entities.set("one" + i, new Player().assign({
270
- // position: new Vec3().assign({ x: 1, y: 2, z: 3 }),
271
- // rotation: new Vec3().assign({ x: 4, y: 5, z: 6 }),
272
- // }));
273
- // }
274
- // state.entities.set("one", new Player().assign({
275
- // position: new Vec3().assign({ x: 1, y: 2, z: 3 }),
276
- // rotation: new Vec3().assign({ x: 4, y: 5, z: 6 }),
277
- // }));
278
- // state.entities.set("two", new Player().assign({
279
- // position: new Vec3().assign({ x: 10, y: 10, z: 3 }),
280
- // rotation: new Vec3().assign({ x: 4, y: 5, z: 6 }),
281
- // }));
282
- function addTeam() {
283
- const team = new Team();
284
- team.entities.set("one", new Player().assign({
285
- position: new Vec3().assign({ x: 1, y: 2, z: 3 }),
286
- rotation: new Vec3().assign({ x: 4, y: 5, z: 6 }),
287
- }));
288
- team.entities.set("two", new Player().assign({
289
- position: new Vec3().assign({ x: 7, y: 8, z: 9 }),
290
- rotation: new Vec3().assign({ x: 2, y: 3, z: 4 }),
291
- }));
292
- state.teams.push(team);
293
- }
294
- addTeam();
295
- addTeam();
296
- const it = { offset: 0 };
297
- const encoder = new Encoder_1.Encoder(state);
298
- // logTime("encode time", () => encoder.encodeAll());
299
- // const encoded = encoder.encode(it);
300
- console.log("> will encode all...", state.toJSON());
301
- const encoded = encoder.encode(it);
302
- console.log("HEAP TOTAL:", process.memoryUsage().heapTotal / 1024 / 1024, "MB");
303
- console.log("HEAP USED:", process.memoryUsage().heapUsed / 1024 / 1024, "MB");
304
- // console.log("encoded.buffer =>", `(${encoded.byteLength} bytes)`);
305
- const sharedOffset = it.offset;
306
- // const team1View = new StateView<State>();
307
- // team1View.owns(state.teams[0]);
308
- const view = new StateView_1.StateView();
309
- view.owns(state.teams[0]);
310
- // view1['owned'].add(state[$changes]);
311
- // view1['owned'].add(state.teams[$changes]);
312
- // view1.owns(state.teams[0]);
313
- // view1.owns(state.entities);
314
- // view1.owns(state.entities.get("one"));
315
- const view2 = new StateView_1.StateView();
316
- console.log(">>> VIEW 2");
317
- view2.owns(state.teams[1]);
318
- // view2.owns(state.entities.get("two"));
319
- console.log("> will encode view 1...");
320
- const viewEncoded1 = encoder.encodeView(view, sharedOffset, it, encoder.sharedBuffer);
321
- console.log("done. view1 encoded =>", `(${viewEncoded1.byteLength} bytes)`);
322
- console.log("> will encode view 2...");
323
- const viewEncoded2 = encoder.encodeView(view2, sharedOffset, it, encoder.sharedBuffer);
324
- console.log("done. view2 encoded =>", `(${viewEncoded2.byteLength} bytes)`);
325
- // setTimeout(() => {
326
- // for (let i = 0; i < 500000; i++) {
327
- // encoder.encodeAll();
328
- // }
329
- // }, 1000)
330
- // logTime("encode time", () => encoder.encodeAll());
331
- // console.log(`encode: (${encoded.length})`, encoded);
332
- console.log("----------------------------------- ENCODE reflection...");
333
- const encodedReflection = Reflection_1.Reflection.encode(state, encoder.context);
334
- console.log("----------------------------------- DECODE reflection...");
335
- const decodedState = Reflection_1.Reflection.decode(encodedReflection);
336
- // const decodedState = new State();
337
- const decoder = new Decoder_1.Decoder(decodedState);
338
- console.log("> will decode...");
339
- // decoder.decode(encoded);
340
- const changes = decoder.decode(viewEncoded1);
341
- // decoder.decode(viewEncoded2);
342
- console.log("CHANGES =>", changes);
343
- // log(new DataView(encoded.buffer, 0, it.offset));
344
- // log(new DataView(viewEncoded1.buffer, 0, it.offset));
345
- log(decodedState.toJSON());
346
- // console.log("encoder.$root.changes =>", encoder.$root.changes.length);
347
- // console.log("encoder.$root.filteredChanges =>", encoder.$root.filteredChanges.length);
348
- // state.teams[0].entities.get("one").position.x = 100;
349
- // // encoder.
350
- // const it2 = { offset: 0 };
351
- // // const encoded = encoder.encode(it);
352
- // const encoded2 = encoder.encode(it2);
353
- // console.log(`> shared encode... (${encoded2.byteLength} bytes)`);
354
- // const viewEncoded3 = encoder.encodeView(view1, sharedOffset, it, encoder.sharedBuffer);
355
- // console.log(`> view1... (${viewEncoded3.byteLength} bytes)`);
356
- // log(decoder.root.toJSON());
357
- // logTime("decode time", () => decoder.decode(encoded));
358
- // console.log("changes =>", changes);
359
- // const rotation = state.entity.rotation;
360
- // rotation.x = 100;
361
- // state.entity.rotation = undefined;
362
- // const encoded2 = encoder.encode();
363
- // console.log({ encoded2 });
364
- // decoder.decode(encoded2);
365
- // console.log("decoded =>", decoded.toJSON());
366
- // console.profile();
367
- // const time = Date.now();
368
- // for (let i = 0; i < 300000; i++) {
369
- // const state = new State();
370
- // encoder['setRoot'](state);
371
- // encoder.encode();
372
- // }
373
- // console.log("encode time:", Date.now() - time);
374
- // console.profileEnd();
375
- // state.players.set("entity", new Entity());
376
- // state.players.set("one", new Player());
377
- // console.log("state:", state);
378
- // console.log("state.players", state.players);
379
- // console.log("state.players.one", state.players.get("one"));
380
- // console.log("state.players.one.position", state.players.get("one").position);
381
- // const encoder = new Encoder(state);
382
- // // encoder.change()
383
- // let encoded = encoder.encodeAll();
384
- // console.log({ encoded })
385
- // const decoder = new Decoder(new State());
386
- // decoder.decode(encoded);
387
- // log(decoder['root'].toJSON());
388
- // const reflection = Reflection.encode(state);
389
- // console.log("encoded =>", reflection);
390
- // log(Reflection.decode(reflection))
391
- ///////...............................................
392
- // function type (type: string) {
393
- // return function (_: any, context: ClassFieldDecoratorContext) {
394
- // context.addInitializer(function() {
395
- // console.log("INITIALIZER!", this);
396
- // setTimeout(() => {
397
- // context.access.set(this, "WHAT");
398
- // }, 1000);
399
- // });
400
- // context.access.get = function(object) {
401
- // return object[context.name];
402
- // }
403
- // context.access.set = function(object, value) {
404
- // console.log("setter...", object, value);
405
- // object[context.name] = `Setter[${value}]`;
406
- // };
407
- // return function(value) {
408
- // console.log("SET", this, value);
409
- // if (typeof(value) === "string") {
410
- // value = `Initializer[${value}]`;
411
- // } else {
412
- // value = value * 100;
413
- // }
414
- // return value;
415
- // };
416
- // }
417
- // }
418
- // class MyState {
419
- // @type("string") name: string = "Hello world!";
420
- // @type("number") num: number = 1;
421
- // }
422
- // const state = new MyState();
423
- // console.log("name: ", state.name);
424
- // console.log("num: ", state.num);
425
- // state.name = `${state.name} modified...`;
426
- // setTimeout(() => console.log(state.name), 1100);
427
- //# sourceMappingURL=v3.js.map