@colyseus/schema 2.0.32 → 3.0.0-alpha.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 +3428 -2677
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/index.mjs +3324 -2445
  4. package/build/esm/index.mjs.map +1 -1
  5. package/build/umd/index.js +3428 -2677
  6. package/lib/Decoder.d.ts +16 -0
  7. package/lib/Decoder.js +182 -0
  8. package/lib/Decoder.js.map +1 -0
  9. package/lib/Encoder.d.ts +13 -0
  10. package/lib/Encoder.js +79 -0
  11. package/lib/Encoder.js.map +1 -0
  12. package/lib/Metadata.d.ts +36 -0
  13. package/lib/Metadata.js +91 -0
  14. package/lib/Metadata.js.map +1 -0
  15. package/lib/Reflection.d.ts +7 -5
  16. package/lib/Reflection.js +62 -58
  17. package/lib/Reflection.js.map +1 -1
  18. package/lib/Schema.d.ts +39 -51
  19. package/lib/Schema.js +189 -731
  20. package/lib/Schema.js.map +1 -1
  21. package/lib/annotations.d.ts +26 -45
  22. package/lib/annotations.js +363 -194
  23. package/lib/annotations.js.map +1 -1
  24. package/lib/changes/ChangeSet.d.ts +12 -0
  25. package/lib/changes/ChangeSet.js +35 -0
  26. package/lib/changes/ChangeSet.js.map +1 -0
  27. package/lib/changes/DecodeOperation.d.ts +15 -0
  28. package/lib/changes/DecodeOperation.js +186 -0
  29. package/lib/changes/DecodeOperation.js.map +1 -0
  30. package/lib/changes/EncodeOperation.d.ts +18 -0
  31. package/lib/changes/EncodeOperation.js +130 -0
  32. package/lib/changes/EncodeOperation.js.map +1 -0
  33. package/lib/changes/consts.d.ts +14 -0
  34. package/lib/changes/consts.js +18 -0
  35. package/lib/changes/consts.js.map +1 -0
  36. package/lib/decoder/DecodeOperation.d.ts +24 -0
  37. package/lib/decoder/DecodeOperation.js +256 -0
  38. package/lib/decoder/DecodeOperation.js.map +1 -0
  39. package/lib/decoder/Decoder.d.ts +21 -0
  40. package/lib/decoder/Decoder.js +114 -0
  41. package/lib/decoder/Decoder.js.map +1 -0
  42. package/lib/decoder/ReferenceTracker.d.ts +26 -0
  43. package/lib/decoder/ReferenceTracker.js +131 -0
  44. package/lib/decoder/ReferenceTracker.js.map +1 -0
  45. package/lib/decoder/strategy/RawChanges.d.ts +3 -0
  46. package/lib/decoder/strategy/RawChanges.js +8 -0
  47. package/lib/decoder/strategy/RawChanges.js.map +1 -0
  48. package/lib/decoder/strategy/StateCallbacks.d.ts +20 -0
  49. package/lib/decoder/strategy/StateCallbacks.js +240 -0
  50. package/lib/decoder/strategy/StateCallbacks.js.map +1 -0
  51. package/lib/decoding/decode.d.ts +48 -0
  52. package/lib/decoding/decode.js +267 -0
  53. package/lib/decoding/decode.js.map +1 -0
  54. package/lib/ecs.d.ts +11 -0
  55. package/lib/ecs.js +160 -0
  56. package/lib/ecs.js.map +1 -0
  57. package/lib/encoder/ChangeTree.d.ts +72 -0
  58. package/lib/encoder/ChangeTree.js +384 -0
  59. package/lib/encoder/ChangeTree.js.map +1 -0
  60. package/lib/encoder/EncodeOperation.d.ts +25 -0
  61. package/lib/encoder/EncodeOperation.js +156 -0
  62. package/lib/encoder/EncodeOperation.js.map +1 -0
  63. package/lib/encoder/Encoder.d.ts +23 -0
  64. package/lib/encoder/Encoder.js +192 -0
  65. package/lib/encoder/Encoder.js.map +1 -0
  66. package/lib/encoder/StateView.d.ts +21 -0
  67. package/lib/encoder/StateView.js +196 -0
  68. package/lib/encoder/StateView.js.map +1 -0
  69. package/lib/encoding/assert.d.ts +9 -0
  70. package/lib/encoding/assert.js +47 -0
  71. package/lib/encoding/assert.js.map +1 -0
  72. package/lib/encoding/decode.js +1 -1
  73. package/lib/encoding/decode.js.map +1 -1
  74. package/lib/encoding/encode.d.ts +19 -16
  75. package/lib/encoding/encode.js +88 -81
  76. package/lib/encoding/encode.js.map +1 -1
  77. package/lib/encoding/spec.d.ts +25 -0
  78. package/lib/encoding/spec.js +30 -0
  79. package/lib/encoding/spec.js.map +1 -0
  80. package/lib/index.d.ts +18 -10
  81. package/lib/index.js +39 -17
  82. package/lib/index.js.map +1 -1
  83. package/lib/symbol.shim.d.ts +6 -0
  84. package/lib/symbol.shim.js +4 -0
  85. package/lib/symbol.shim.js.map +1 -0
  86. package/lib/types/ArraySchema.d.ts +1 -1
  87. package/lib/types/ArraySchema.js +0 -7
  88. package/lib/types/ArraySchema.js.map +1 -1
  89. package/lib/types/HelperTypes.d.ts +10 -2
  90. package/lib/types/HelperTypes.js.map +1 -1
  91. package/lib/types/custom/ArraySchema.d.ts +245 -0
  92. package/lib/types/custom/ArraySchema.js +659 -0
  93. package/lib/types/custom/ArraySchema.js.map +1 -0
  94. package/lib/types/custom/CollectionSchema.d.ts +42 -0
  95. package/lib/types/custom/CollectionSchema.js +165 -0
  96. package/lib/types/custom/CollectionSchema.js.map +1 -0
  97. package/lib/types/custom/MapSchema.d.ts +43 -0
  98. package/lib/types/custom/MapSchema.js +200 -0
  99. package/lib/types/custom/MapSchema.js.map +1 -0
  100. package/lib/types/custom/SetSchema.d.ts +39 -0
  101. package/lib/types/custom/SetSchema.js +177 -0
  102. package/lib/types/custom/SetSchema.js.map +1 -0
  103. package/lib/types/registry.d.ts +6 -0
  104. package/lib/types/registry.js +19 -0
  105. package/lib/types/registry.js.map +1 -0
  106. package/lib/types/symbols.d.ts +29 -0
  107. package/lib/types/symbols.js +33 -0
  108. package/lib/types/symbols.js.map +1 -0
  109. package/lib/types/utils.d.ts +0 -8
  110. package/lib/types/utils.js +1 -33
  111. package/lib/types/utils.js.map +1 -1
  112. package/lib/usage.d.ts +1 -0
  113. package/lib/usage.js +22 -0
  114. package/lib/usage.js.map +1 -0
  115. package/lib/utils.d.ts +13 -2
  116. package/lib/utils.js +36 -15
  117. package/lib/utils.js.map +1 -1
  118. package/lib/v3.d.ts +1 -0
  119. package/lib/v3.js +427 -0
  120. package/lib/v3.js.map +1 -0
  121. package/lib/v3_bench.d.ts +1 -0
  122. package/lib/v3_bench.js +130 -0
  123. package/lib/v3_bench.js.map +1 -0
  124. package/lib/v3_experiment.d.ts +1 -0
  125. package/lib/v3_experiment.js +407 -0
  126. package/lib/v3_experiment.js.map +1 -0
  127. package/package.json +5 -5
  128. package/src/Metadata.ts +135 -0
  129. package/src/Reflection.ts +75 -66
  130. package/src/Schema.ts +213 -931
  131. package/src/annotations.ts +430 -243
  132. package/src/decoder/DecodeOperation.ts +372 -0
  133. package/src/decoder/Decoder.ts +155 -0
  134. package/src/decoder/ReferenceTracker.ts +151 -0
  135. package/src/decoder/strategy/RawChanges.ts +9 -0
  136. package/src/decoder/strategy/StateCallbacks.ts +326 -0
  137. package/src/encoder/ChangeTree.ts +492 -0
  138. package/src/encoder/EncodeOperation.ts +237 -0
  139. package/src/encoder/Encoder.ts +246 -0
  140. package/src/encoder/StateView.ts +229 -0
  141. package/src/encoding/assert.ts +58 -0
  142. package/src/encoding/decode.ts +1 -1
  143. package/src/encoding/encode.ts +91 -82
  144. package/src/encoding/spec.ts +29 -0
  145. package/src/index.ts +22 -19
  146. package/src/symbol.shim.ts +12 -0
  147. package/src/types/HelperTypes.ts +16 -2
  148. package/src/types/{ArraySchema.ts → custom/ArraySchema.ts} +342 -248
  149. package/src/types/{CollectionSchema.ts → custom/CollectionSchema.ts} +56 -46
  150. package/src/types/{MapSchema.ts → custom/MapSchema.ts} +88 -115
  151. package/src/types/{SetSchema.ts → custom/SetSchema.ts} +58 -47
  152. package/src/types/{typeRegistry.ts → registry.ts} +6 -6
  153. package/src/types/symbols.ts +36 -0
  154. package/src/types/utils.ts +0 -46
  155. package/src/utils.ts +50 -21
  156. package/src/v3_bench.ts +107 -0
  157. package/src/changes/ChangeTree.ts +0 -295
  158. package/src/changes/ReferenceTracker.ts +0 -91
  159. package/src/filters/index.ts +0 -23
  160. package/src/spec.ts +0 -49
@@ -1,295 +0,0 @@
1
- import { OPERATION } from "../spec";
2
- import { Schema } from "../Schema";
3
- import { SchemaDefinition, FilterChildrenCallback } from "../annotations";
4
-
5
- import { MapSchema } from "../types/MapSchema";
6
- import { ArraySchema } from "../types/ArraySchema";
7
- import { CollectionSchema } from "../types/CollectionSchema";
8
- import { SetSchema } from "../types/SetSchema";
9
- import { ReferenceTracker } from "./ReferenceTracker";
10
-
11
- export type Ref = Schema
12
- | ArraySchema
13
- | MapSchema
14
- | CollectionSchema
15
- | SetSchema;
16
-
17
- export interface ChangeOperation {
18
- op: OPERATION,
19
- index: number,
20
- }
21
-
22
- //
23
- // FieldCache is used for @filter()
24
- //
25
- export interface FieldCache {
26
- beginIndex: number;
27
- endIndex: number;
28
- }
29
-
30
-
31
- export class ChangeTree {
32
- ref: Ref;
33
- refId: number;
34
-
35
- root?: ReferenceTracker;
36
-
37
- parent?: Ref;
38
- parentIndex?: number;
39
-
40
- indexes: {[index: string]: any};
41
-
42
- changed: boolean = false;
43
- changes = new Map<number, ChangeOperation>();
44
- allChanges = new Set<number>();
45
-
46
- // cached indexes for filtering
47
- caches: {[field: number]: number[]} = {};
48
-
49
- currentCustomOperation: number = 0;
50
-
51
- constructor(ref: Ref, parent?: Ref, root?: ReferenceTracker) {
52
- this.ref = ref;
53
- this.setParent(parent, root);
54
- }
55
-
56
- setParent(
57
- parent: Ref,
58
- root?: ReferenceTracker,
59
- parentIndex?: number,
60
- ) {
61
- if (!this.indexes) {
62
- this.indexes = (this.ref instanceof Schema)
63
- ? this.ref['_definition'].indexes
64
- : {};
65
- }
66
-
67
- this.parent = parent;
68
- this.parentIndex = parentIndex;
69
-
70
- // avoid setting parents with empty `root`
71
- if (!root) { return; }
72
- this.root = root;
73
-
74
- //
75
- // assign same parent on child structures
76
- //
77
- if (this.ref instanceof Schema) {
78
- const definition: SchemaDefinition = this.ref['_definition'];
79
-
80
- for (let field in definition.schema) {
81
- const value = this.ref[field];
82
-
83
- if (value && value['$changes']) {
84
- const parentIndex = definition.indexes[field];
85
-
86
- (value['$changes'] as ChangeTree).setParent(
87
- this.ref,
88
- root,
89
- parentIndex,
90
- );
91
- }
92
- }
93
-
94
- } else if (typeof (this.ref) === "object") {
95
- this.ref.forEach((value, key) => {
96
- if (value instanceof Schema) {
97
- const changeTreee = value['$changes'];
98
- const parentIndex = this.ref['$changes'].indexes[key];
99
-
100
- changeTreee.setParent(
101
- this.ref,
102
- this.root,
103
- parentIndex,
104
- );
105
- }
106
- });
107
- }
108
- }
109
-
110
- operation(op: ChangeOperation) {
111
- this.changes.set(--this.currentCustomOperation, op);
112
- }
113
-
114
- change(fieldName: string | number, operation: OPERATION = OPERATION.ADD) {
115
- const index = (typeof (fieldName) === "number")
116
- ? fieldName
117
- : this.indexes[fieldName];
118
-
119
- this.assertValidIndex(index, fieldName);
120
-
121
- const previousChange = this.changes.get(index);
122
-
123
- if (
124
- !previousChange ||
125
- previousChange.op === OPERATION.DELETE ||
126
- previousChange.op === OPERATION.TOUCH // (mazmorra.io's BattleAction issue)
127
- ) {
128
- this.changes.set(index, {
129
- op: (!previousChange)
130
- ? operation
131
- : (previousChange.op === OPERATION.DELETE)
132
- ? OPERATION.DELETE_AND_ADD
133
- : operation,
134
- // : OPERATION.REPLACE,
135
- index
136
- });
137
- }
138
-
139
- this.allChanges.add(index);
140
-
141
- this.changed = true;
142
- this.touchParents();
143
- }
144
-
145
- touch(fieldName: string | number) {
146
- const index = (typeof (fieldName) === "number")
147
- ? fieldName
148
- : this.indexes[fieldName];
149
-
150
- this.assertValidIndex(index, fieldName);
151
-
152
- if (!this.changes.has(index)) {
153
- this.changes.set(index, { op: OPERATION.TOUCH, index });
154
- }
155
-
156
- this.allChanges.add(index);
157
-
158
- // ensure touch is placed until the $root is found.
159
- this.touchParents();
160
- }
161
-
162
- touchParents() {
163
- if (this.parent) {
164
- (this.parent['$changes'] as ChangeTree).touch(this.parentIndex);
165
- }
166
- }
167
-
168
- getType(index?: number) {
169
- if (this.ref['_definition']) {
170
- const definition = (this.ref as Schema)['_definition'];
171
- return definition.schema[ definition.fieldsByIndex[index] ];
172
-
173
- } else {
174
- const definition = (this.parent as Schema)['_definition'];
175
- const parentType = definition.schema[ definition.fieldsByIndex[this.parentIndex] ];
176
-
177
- //
178
- // Get the child type from parent structure.
179
- // - ["string"] => "string"
180
- // - { map: "string" } => "string"
181
- // - { set: "string" } => "string"
182
- //
183
- return Object.values(parentType)[0];
184
- }
185
- }
186
-
187
- getChildrenFilter(): FilterChildrenCallback {
188
- const childFilters = (this.parent as Schema)['_definition'].childFilters;
189
- return childFilters && childFilters[this.parentIndex];
190
- }
191
-
192
- //
193
- // used during `.encode()`
194
- //
195
- getValue(index: number) {
196
- return this.ref['getByIndex'](index);
197
- }
198
-
199
- delete(fieldName: string | number) {
200
- const index = (typeof (fieldName) === "number")
201
- ? fieldName
202
- : this.indexes[fieldName];
203
-
204
- if (index === undefined) {
205
- console.warn(`@colyseus/schema ${this.ref.constructor.name}: trying to delete non-existing index: ${fieldName} (${index})`);
206
- return;
207
- }
208
-
209
- const previousValue = this.getValue(index);
210
- // console.log("$changes.delete =>", { fieldName, index, previousValue });
211
-
212
- this.changes.set(index, { op: OPERATION.DELETE, index });
213
-
214
- this.allChanges.delete(index);
215
-
216
- // delete cache
217
- delete this.caches[index];
218
-
219
- // remove `root` reference
220
- if (previousValue && previousValue['$changes']) {
221
- previousValue['$changes'].parent = undefined;
222
- }
223
-
224
- this.changed = true;
225
- this.touchParents();
226
- }
227
-
228
- discard(changed: boolean = false, discardAll: boolean = false) {
229
- //
230
- // Map, Array, etc:
231
- // Remove cached key to ensure ADD operations is unsed instead of
232
- // REPLACE in case same key is used on next patches.
233
- //
234
- // TODO: refactor this. this is not relevant for Collection and Set.
235
- //
236
- if (!(this.ref instanceof Schema)) {
237
- this.changes.forEach((change) => {
238
- if (change.op === OPERATION.DELETE) {
239
- const index = this.ref['getIndex'](change.index)
240
- delete this.indexes[index];
241
- }
242
- });
243
- }
244
-
245
- this.changes.clear();
246
- this.changed = changed;
247
-
248
- if (discardAll) {
249
- this.allChanges.clear();
250
- }
251
-
252
- // re-set `currentCustomOperation`
253
- this.currentCustomOperation = 0;
254
- }
255
-
256
- /**
257
- * Recursively discard all changes from this, and child structures.
258
- */
259
- discardAll() {
260
- this.changes.forEach((change) => {
261
- const value = this.getValue(change.index);
262
-
263
- if (value && value['$changes']) {
264
- value['$changes'].discardAll();
265
- }
266
- });
267
-
268
- this.discard();
269
- }
270
-
271
- // cache(field: number, beginIndex: number, endIndex: number) {
272
- cache(field: number, cachedBytes: number[]) {
273
- this.caches[field] = cachedBytes;
274
- }
275
-
276
- clone() {
277
- return new ChangeTree(this.ref, this.parent, this.root);
278
- }
279
-
280
- ensureRefId() {
281
- // skip if refId is already set.
282
- if (this.refId !== undefined) {
283
- return;
284
- }
285
-
286
- this.refId = this.root.getNextUniqueId();
287
- }
288
-
289
- protected assertValidIndex(index: number, fieldName: string | number) {
290
- if (index === undefined) {
291
- throw new Error(`ChangeTree: missing index for field "${fieldName}"`);
292
- }
293
- }
294
-
295
- }
@@ -1,91 +0,0 @@
1
- import { Schema } from "../Schema";
2
- import { Ref } from "./ChangeTree";
3
- import type { SchemaDefinition } from "../annotations";
4
-
5
- export class ReferenceTracker {
6
- //
7
- // Relation of refId => Schema structure
8
- // For direct access of structures during decoding time.
9
- //
10
- public refs = new Map<number, Ref>();
11
- public refCounts: { [refId: number]: number; } = {};
12
- public deletedRefs = new Set<number>();
13
-
14
- protected nextUniqueId: number = 0;
15
-
16
- getNextUniqueId() {
17
- return this.nextUniqueId++;
18
- }
19
-
20
- // for decoding
21
- addRef(refId: number, ref: Ref, incrementCount: boolean = true) {
22
- this.refs.set(refId, ref);
23
-
24
- if (incrementCount) {
25
- this.refCounts[refId] = (this.refCounts[refId] || 0) + 1;
26
- }
27
- }
28
-
29
- // for decoding
30
- removeRef(refId: number) {
31
- const refCount = this.refCounts[refId];
32
- if (refCount === undefined) {
33
- console.warn(`trying to remove reference ${refId} that doesn't exist`);
34
- return;
35
- }
36
- if (refCount === 0) {
37
- console.warn(`trying to remove reference ${refId} with 0 refCount`);
38
- return;
39
- }
40
-
41
- this.refCounts[refId] = refCount - 1;
42
- this.deletedRefs.add(refId);
43
- }
44
-
45
- clearRefs() {
46
- this.refs.clear();
47
- this.deletedRefs.clear();
48
- this.refCounts = {};
49
- }
50
-
51
- // for decoding
52
- garbageCollectDeletedRefs() {
53
- this.deletedRefs.forEach((refId) => {
54
- //
55
- // Skip active references.
56
- //
57
- if (this.refCounts[refId] > 0) { return; }
58
-
59
- const ref = this.refs.get(refId);
60
-
61
- //
62
- // Ensure child schema instances have their references removed as well.
63
- //
64
- if (ref instanceof Schema) {
65
- for (const fieldName in ref['_definition'].schema) {
66
- if (typeof (ref['_definition'].schema[fieldName]) !== "string" &&
67
- ref[fieldName] &&
68
- ref[fieldName]['$changes']) {
69
- this.removeRef(ref[fieldName]['$changes'].refId);
70
- }
71
- }
72
-
73
- } else {
74
- const definition: SchemaDefinition = ref['$changes'].parent._definition;
75
- const type = definition.schema[definition.fieldsByIndex[ref['$changes'].parentIndex]];
76
-
77
- if (typeof (Object.values(type)[0]) === "function") {
78
- Array.from(ref.values())
79
- .forEach((child) => this.removeRef(child['$changes'].refId));
80
- }
81
- }
82
-
83
- this.refs.delete(refId);
84
- delete this.refCounts[refId];
85
- });
86
-
87
- // clear deleted refs.
88
- this.deletedRefs.clear();
89
- }
90
-
91
- }
@@ -1,23 +0,0 @@
1
- import { ChangeTree } from "../changes/ChangeTree";
2
- import { ClientWithSessionId } from "../annotations";
3
-
4
- export class ClientState {
5
- refIds = new WeakSet<ChangeTree>();
6
- containerIndexes = new WeakMap<ChangeTree, Set<number>>();
7
- // containerIndexes = new Map<ChangeTree, Set<number>>();
8
-
9
- addRefId(changeTree: ChangeTree) {
10
- if (!this.refIds.has(changeTree)) {
11
- this.refIds.add(changeTree);
12
- this.containerIndexes.set(changeTree, new Set());
13
- }
14
- }
15
-
16
- static get(client: ClientWithSessionId) {
17
- if (client.$filterState === undefined) {
18
- client.$filterState = new ClientState();
19
- }
20
-
21
- return client.$filterState as ClientState;
22
- }
23
- }
package/src/spec.ts DELETED
@@ -1,49 +0,0 @@
1
- // export const SWITCH_TO_STRUCTURE = 193; (easily collides with DELETE_AND_ADD + fieldIndex = 2)
2
- export const SWITCH_TO_STRUCTURE = 255; // (decoding collides with DELETE_AND_ADD + fieldIndex = 63)
3
- export const TYPE_ID = 213;
4
-
5
- /**
6
- * Encoding Schema field operations.
7
- */
8
- export enum OPERATION {
9
- // add new structure/primitive
10
- ADD = 128,
11
-
12
- // replace structure/primitive
13
- REPLACE = 0,
14
-
15
- // delete field
16
- DELETE = 64,
17
-
18
- // DELETE field, followed by an ADD
19
- DELETE_AND_ADD = 192, // 11100000
20
-
21
- // TOUCH is used to determine hierarchy of nested Schema structures during serialization.
22
- // touches are NOT encoded.
23
- TOUCH = 1, // 00000000
24
-
25
- // MapSchema Operations
26
- CLEAR = 10,
27
- }
28
-
29
- // export enum OPERATION {
30
- // // add new structure/primitive
31
- // // (128)
32
- // ADD = 128, // 10000000,
33
-
34
- // // replace structure/primitive
35
- // REPLACE = 1,// 00000001
36
-
37
- // // delete field
38
- // DELETE = 192, // 11000000
39
-
40
- // // DELETE field, followed by an ADD
41
- // DELETE_AND_ADD = 224, // 11100000
42
-
43
- // // TOUCH is used to determine hierarchy of nested Schema structures during serialization.
44
- // // touches are NOT encoded.
45
- // TOUCH = 0, // 00000000
46
-
47
- // // MapSchema Operations
48
- // CLEAR = 10,
49
- // }