@colyseus/schema 1.0.45 → 1.0.46

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 (67) hide show
  1. package/build/cjs/index.js +0 -3
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/index.mjs +4 -3
  4. package/build/esm/index.mjs.map +1 -1
  5. package/build/umd/index.js +0 -3
  6. package/lib/Reflection.js +77 -109
  7. package/lib/Reflection.js.map +1 -1
  8. package/lib/Schema.js +194 -255
  9. package/lib/Schema.js.map +1 -1
  10. package/lib/annotations.d.ts +6 -6
  11. package/lib/annotations.js +56 -89
  12. package/lib/annotations.js.map +1 -1
  13. package/lib/changes/ChangeTree.d.ts +1 -1
  14. package/lib/changes/ChangeTree.js +86 -96
  15. package/lib/changes/ChangeTree.js.map +1 -1
  16. package/lib/codegen/api.js +9 -9
  17. package/lib/codegen/api.js.map +1 -1
  18. package/lib/codegen/argv.js +11 -11
  19. package/lib/codegen/argv.js.map +1 -1
  20. package/lib/codegen/cli.js +20 -9
  21. package/lib/codegen/cli.js.map +1 -1
  22. package/lib/codegen/languages/cpp.js +126 -77
  23. package/lib/codegen/languages/cpp.js.map +1 -1
  24. package/lib/codegen/languages/csharp.js +68 -68
  25. package/lib/codegen/languages/csharp.js.map +1 -1
  26. package/lib/codegen/languages/haxe.js +34 -26
  27. package/lib/codegen/languages/haxe.js.map +1 -1
  28. package/lib/codegen/languages/java.js +39 -27
  29. package/lib/codegen/languages/java.js.map +1 -1
  30. package/lib/codegen/languages/js.js +48 -32
  31. package/lib/codegen/languages/js.js.map +1 -1
  32. package/lib/codegen/languages/lua.js +35 -24
  33. package/lib/codegen/languages/lua.js.map +1 -1
  34. package/lib/codegen/languages/ts.js +63 -72
  35. package/lib/codegen/languages/ts.js.map +1 -1
  36. package/lib/codegen/parser.js +47 -51
  37. package/lib/codegen/parser.js.map +1 -1
  38. package/lib/codegen/types.js +55 -60
  39. package/lib/codegen/types.js.map +1 -1
  40. package/lib/encoding/decode.js +15 -15
  41. package/lib/encoding/decode.js.map +1 -1
  42. package/lib/encoding/encode.js +14 -14
  43. package/lib/encoding/encode.js.map +1 -1
  44. package/lib/events/EventEmitter.d.ts +1 -1
  45. package/lib/events/EventEmitter.js +16 -51
  46. package/lib/events/EventEmitter.js.map +1 -1
  47. package/lib/filters/index.js +7 -8
  48. package/lib/filters/index.js.map +1 -1
  49. package/lib/index.js +7 -7
  50. package/lib/index.js.map +1 -1
  51. package/lib/types/ArraySchema.d.ts +1 -1
  52. package/lib/types/ArraySchema.js +157 -219
  53. package/lib/types/ArraySchema.js.map +1 -1
  54. package/lib/types/CollectionSchema.d.ts +1 -1
  55. package/lib/types/CollectionSchema.js +60 -68
  56. package/lib/types/CollectionSchema.js.map +1 -1
  57. package/lib/types/HelperTypes.d.ts +9 -9
  58. package/lib/types/MapSchema.js +63 -74
  59. package/lib/types/MapSchema.js.map +1 -1
  60. package/lib/types/SetSchema.js +59 -68
  61. package/lib/types/SetSchema.js.map +1 -1
  62. package/lib/types/index.js +1 -1
  63. package/lib/types/index.js.map +1 -1
  64. package/lib/utils.js +10 -13
  65. package/lib/utils.js.map +1 -1
  66. package/package.json +1 -1
  67. package/src/types/ArraySchema.ts +4 -3
package/lib/Schema.js CHANGED
@@ -1,59 +1,23 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __read = (this && this.__read) || function (o, n) {
18
- var m = typeof Symbol === "function" && o[Symbol.iterator];
19
- if (!m) return o;
20
- var i = m.call(o), r, ar = [], e;
21
- try {
22
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
23
- }
24
- catch (error) { e = { error: error }; }
25
- finally {
26
- try {
27
- if (r && !r.done && (m = i["return"])) m.call(i);
28
- }
29
- finally { if (e) throw e.error; }
30
- }
31
- return ar;
32
- };
33
2
  Object.defineProperty(exports, "__esModule", { value: true });
34
3
  exports.Schema = void 0;
35
- var spec_1 = require("./spec");
36
- var annotations_1 = require("./annotations");
37
- var encode = require("./encoding/encode");
38
- var decode = require("./encoding/decode");
39
- var ArraySchema_1 = require("./types/ArraySchema");
40
- var MapSchema_1 = require("./types/MapSchema");
41
- var CollectionSchema_1 = require("./types/CollectionSchema");
42
- var SetSchema_1 = require("./types/SetSchema");
43
- var ChangeTree_1 = require("./changes/ChangeTree");
44
- var EventEmitter_1 = require("./events/EventEmitter");
45
- var filters_1 = require("./filters");
46
- var types_1 = require("./types");
47
- var EncodeSchemaError = /** @class */ (function (_super) {
48
- __extends(EncodeSchemaError, _super);
49
- function EncodeSchemaError() {
50
- return _super !== null && _super.apply(this, arguments) || this;
51
- }
52
- return EncodeSchemaError;
53
- }(Error));
4
+ const spec_1 = require("./spec");
5
+ const annotations_1 = require("./annotations");
6
+ const encode = require("./encoding/encode");
7
+ const decode = require("./encoding/decode");
8
+ const ArraySchema_1 = require("./types/ArraySchema");
9
+ const MapSchema_1 = require("./types/MapSchema");
10
+ const CollectionSchema_1 = require("./types/CollectionSchema");
11
+ const SetSchema_1 = require("./types/SetSchema");
12
+ const ChangeTree_1 = require("./changes/ChangeTree");
13
+ const EventEmitter_1 = require("./events/EventEmitter");
14
+ const filters_1 = require("./filters");
15
+ const types_1 = require("./types");
16
+ class EncodeSchemaError extends Error {
17
+ }
54
18
  function assertType(value, type, klass, field) {
55
- var typeofTarget;
56
- var allowNull = false;
19
+ let typeofTarget;
20
+ let allowNull = false;
57
21
  switch (type) {
58
22
  case "number":
59
23
  case "int8":
@@ -68,7 +32,7 @@ function assertType(value, type, klass, field) {
68
32
  case "float64":
69
33
  typeofTarget = "number";
70
34
  if (isNaN(value)) {
71
- console.log("trying to encode \"NaN\" in ".concat(klass.constructor.name, "#").concat(field));
35
+ console.log(`trying to encode "NaN" in ${klass.constructor.name}#${field}`);
72
36
  }
73
37
  break;
74
38
  case "string":
@@ -80,23 +44,23 @@ function assertType(value, type, klass, field) {
80
44
  return;
81
45
  }
82
46
  if (typeof (value) !== typeofTarget && (!allowNull || (allowNull && value !== null))) {
83
- var foundValue = "'".concat(JSON.stringify(value), "'").concat((value && value.constructor && " (".concat(value.constructor.name, ")")) || '');
84
- throw new EncodeSchemaError("a '".concat(typeofTarget, "' was expected, but ").concat(foundValue, " was provided in ").concat(klass.constructor.name, "#").concat(field));
47
+ let foundValue = `'${JSON.stringify(value)}'${(value && value.constructor && ` (${value.constructor.name})`) || ''}`;
48
+ throw new EncodeSchemaError(`a '${typeofTarget}' was expected, but ${foundValue} was provided in ${klass.constructor.name}#${field}`);
85
49
  }
86
50
  }
87
51
  function assertInstanceType(value, type, klass, field) {
88
52
  if (!(value instanceof type)) {
89
- throw new EncodeSchemaError("a '".concat(type.name, "' was expected, but '").concat(value.constructor.name, "' was provided in ").concat(klass.constructor.name, "#").concat(field));
53
+ throw new EncodeSchemaError(`a '${type.name}' was expected, but '${value.constructor.name}' was provided in ${klass.constructor.name}#${field}`);
90
54
  }
91
55
  }
92
56
  function encodePrimitiveType(type, bytes, value, klass, field) {
93
57
  assertType(value, type, klass, field);
94
- var encodeFunc = encode[type];
58
+ const encodeFunc = encode[type];
95
59
  if (encodeFunc) {
96
60
  encodeFunc(bytes, value);
97
61
  }
98
62
  else {
99
- throw new EncodeSchemaError("a '".concat(type, "' was expected, but ").concat(value, " was provided in ").concat(klass.constructor.name, "#").concat(field));
63
+ throw new EncodeSchemaError(`a '${type}' was expected, but ${value} was provided in ${klass.constructor.name}#${field}`);
100
64
  }
101
65
  }
102
66
  function decodePrimitiveType(type, bytes, it) {
@@ -105,13 +69,17 @@ function decodePrimitiveType(type, bytes, it) {
105
69
  /**
106
70
  * Schema encoder / decoder
107
71
  */
108
- var Schema = /** @class */ (function () {
72
+ class Schema {
73
+ static { this._definition = annotations_1.SchemaDefinition.create(); }
74
+ static onError(e) {
75
+ console.error(e);
76
+ }
77
+ static is(type) {
78
+ return (type['_definition'] &&
79
+ type['_definition'].schema !== undefined);
80
+ }
109
81
  // allow inherited classes to have a constructor
110
- function Schema() {
111
- var args = [];
112
- for (var _i = 0; _i < arguments.length; _i++) {
113
- args[_i] = arguments[_i];
114
- }
82
+ constructor(...args) {
115
83
  // fix enumerability of fields for end-user
116
84
  Object.defineProperties(this, {
117
85
  $changes: {
@@ -125,7 +93,7 @@ var Schema = /** @class */ (function () {
125
93
  writable: true
126
94
  },
127
95
  });
128
- var descriptors = this._definition.descriptors;
96
+ const descriptors = this._definition.descriptors;
129
97
  if (descriptors) {
130
98
  Object.defineProperties(this, descriptors);
131
99
  }
@@ -136,53 +104,36 @@ var Schema = /** @class */ (function () {
136
104
  this.assign(args[0]);
137
105
  }
138
106
  }
139
- Schema.onError = function (e) {
140
- console.error(e);
141
- };
142
- Schema.is = function (type) {
143
- return (type['_definition'] &&
144
- type['_definition'].schema !== undefined);
145
- };
146
- Schema.prototype.assign = function (props) {
107
+ assign(props) {
147
108
  Object.assign(this, props);
148
109
  return this;
149
- };
150
- Object.defineProperty(Schema.prototype, "_definition", {
151
- get: function () { return this.constructor._definition; },
152
- enumerable: false,
153
- configurable: true
154
- });
155
- Schema.prototype.listen = function (attr, callback) {
156
- var _this = this;
110
+ }
111
+ get _definition() { return this.constructor._definition; }
112
+ listen(attr, callback) {
157
113
  if (!this.$listeners[attr]) {
158
114
  this.$listeners[attr] = new EventEmitter_1.EventEmitter_();
159
115
  }
160
116
  this.$listeners[attr].register(callback);
161
117
  // return un-register callback.
162
- return function () {
163
- return _this.$listeners[attr].remove(callback);
164
- };
165
- };
166
- Schema.prototype.decode = function (bytes, it, ref, allChanges) {
167
- if (it === void 0) { it = { offset: 0 }; }
168
- if (ref === void 0) { ref = this; }
169
- if (allChanges === void 0) { allChanges = new Map(); }
170
- var $root = this.$changes.root;
171
- var totalBytes = bytes.length;
172
- var refId = 0;
173
- var changes = [];
118
+ return () => this.$listeners[attr].remove(callback);
119
+ }
120
+ decode(bytes, it = { offset: 0 }, ref = this, allChanges = new Map()) {
121
+ const $root = this.$changes.root;
122
+ const totalBytes = bytes.length;
123
+ let refId = 0;
124
+ let changes = [];
174
125
  $root.refs.set(refId, this);
175
126
  allChanges.set(refId, changes);
176
127
  while (it.offset < totalBytes) {
177
- var byte = bytes[it.offset++];
128
+ let byte = bytes[it.offset++];
178
129
  if (byte == spec_1.SWITCH_TO_STRUCTURE) {
179
130
  refId = decode.number(bytes, it);
180
- var nextRef = $root.refs.get(refId);
131
+ const nextRef = $root.refs.get(refId);
181
132
  //
182
133
  // Trying to access a reference that haven't been decoded yet.
183
134
  //
184
135
  if (!nextRef) {
185
- throw new Error("\"refId\" not found: ".concat(refId));
136
+ throw new Error(`"refId" not found: ${refId}`);
186
137
  }
187
138
  ref = nextRef;
188
139
  // create empty list of changes for this refId.
@@ -190,9 +141,9 @@ var Schema = /** @class */ (function () {
190
141
  allChanges.set(refId, changes);
191
142
  continue;
192
143
  }
193
- var changeTree = ref['$changes'];
194
- var isSchema = (ref['_definition'] !== undefined);
195
- var operation = (isSchema)
144
+ const changeTree = ref['$changes'];
145
+ const isSchema = (ref['_definition'] !== undefined);
146
+ const operation = (isSchema)
196
147
  ? (byte >> 6) << 6 // "compressed" index + operation
197
148
  : byte; // "uncompressed" index + operation (array/map items)
198
149
  if (operation === spec_1.OPERATION.CLEAR) {
@@ -204,16 +155,16 @@ var Schema = /** @class */ (function () {
204
155
  ref.clear(true);
205
156
  continue;
206
157
  }
207
- var fieldIndex = (isSchema)
158
+ const fieldIndex = (isSchema)
208
159
  ? byte % (operation || 255) // if "REPLACE" operation (0), use 255
209
160
  : decode.number(bytes, it);
210
- var fieldName = (isSchema)
161
+ const fieldName = (isSchema)
211
162
  ? (ref['_definition'].fieldsByIndex[fieldIndex])
212
163
  : "";
213
- var type = changeTree.getType(fieldIndex);
214
- var value = void 0;
215
- var previousValue = void 0;
216
- var dynamicIndex = void 0;
164
+ let type = changeTree.getType(fieldIndex);
165
+ let value;
166
+ let previousValue;
167
+ let dynamicIndex;
217
168
  if (!isSchema) {
218
169
  previousValue = ref['getByIndex'](fieldIndex);
219
170
  if ((operation & spec_1.OPERATION.ADD) === spec_1.OPERATION.ADD) { // ADD or DELETE_AND_ADD
@@ -228,7 +179,7 @@ var Schema = /** @class */ (function () {
228
179
  }
229
180
  }
230
181
  else {
231
- previousValue = ref["_".concat(fieldName)];
182
+ previousValue = ref[`_${fieldName}`];
232
183
  }
233
184
  //
234
185
  // Delete operations
@@ -249,7 +200,7 @@ var Schema = /** @class */ (function () {
249
200
  // keep skipping next bytes until reaches a known structure
250
201
  // by local decoder.
251
202
  //
252
- var nextIterator = { offset: it.offset };
203
+ const nextIterator = { offset: it.offset };
253
204
  while (it.offset < totalBytes) {
254
205
  if (decode.switchStructureCheck(bytes, it)) {
255
206
  nextIterator.offset = it.offset + 1;
@@ -268,24 +219,24 @@ var Schema = /** @class */ (function () {
268
219
  //
269
220
  }
270
221
  else if (Schema.is(type)) {
271
- var refId_1 = decode.number(bytes, it);
272
- value = $root.refs.get(refId_1);
222
+ const refId = decode.number(bytes, it);
223
+ value = $root.refs.get(refId);
273
224
  if (operation !== spec_1.OPERATION.REPLACE) {
274
- var childType = this.getSchemaType(bytes, it, type);
225
+ const childType = this.getSchemaType(bytes, it, type);
275
226
  if (!value) {
276
227
  value = this.createTypeInstance(childType);
277
- value.$changes.refId = refId_1;
228
+ value.$changes.refId = refId;
278
229
  if (previousValue) {
279
230
  value.onChange = previousValue.onChange;
280
231
  value.onRemove = previousValue.onRemove;
281
232
  value.$listeners = previousValue.$listeners;
282
233
  if (previousValue['$changes'].refId &&
283
- refId_1 !== previousValue['$changes'].refId) {
234
+ refId !== previousValue['$changes'].refId) {
284
235
  $root.removeRef(previousValue['$changes'].refId);
285
236
  }
286
237
  }
287
238
  }
288
- $root.addRef(refId_1, value, (value !== previousValue));
239
+ $root.addRef(refId, value, (value !== previousValue));
289
240
  }
290
241
  }
291
242
  else if (typeof (type) === "string") {
@@ -295,40 +246,40 @@ var Schema = /** @class */ (function () {
295
246
  value = decodePrimitiveType(type, bytes, it);
296
247
  }
297
248
  else {
298
- var typeDef = (0, types_1.getType)(Object.keys(type)[0]);
299
- var refId_2 = decode.number(bytes, it);
300
- var valueRef = ($root.refs.has(refId_2))
301
- ? previousValue || $root.refs.get(refId_2)
249
+ const typeDef = (0, types_1.getType)(Object.keys(type)[0]);
250
+ const refId = decode.number(bytes, it);
251
+ const valueRef = ($root.refs.has(refId))
252
+ ? previousValue || $root.refs.get(refId)
302
253
  : new typeDef.constructor();
303
254
  value = valueRef.clone(true);
304
- value.$changes.refId = refId_2;
255
+ value.$changes.refId = refId;
305
256
  // preserve schema callbacks
306
257
  if (previousValue) {
307
258
  value.onAdd = previousValue.onAdd;
308
259
  value.onRemove = previousValue.onRemove;
309
260
  value.onChange = previousValue.onChange;
310
261
  if (previousValue['$changes'].refId &&
311
- refId_2 !== previousValue['$changes'].refId) {
262
+ refId !== previousValue['$changes'].refId) {
312
263
  $root.removeRef(previousValue['$changes'].refId);
313
264
  //
314
265
  // Trigger onRemove if structure has been replaced.
315
266
  //
316
- var deletes = [];
317
- var entries = previousValue.entries();
318
- var iter = void 0;
267
+ const deletes = [];
268
+ const entries = previousValue.entries();
269
+ let iter;
319
270
  while ((iter = entries.next()) && !iter.done) {
320
- var _a = __read(iter.value, 2), key = _a[0], value_1 = _a[1];
271
+ const [key, value] = iter.value;
321
272
  deletes.push({
322
273
  op: spec_1.OPERATION.DELETE,
323
274
  field: key,
324
275
  value: undefined,
325
- previousValue: value_1,
276
+ previousValue: value,
326
277
  });
327
278
  }
328
279
  allChanges.set(previousValue['$changes'].refId, deletes);
329
280
  }
330
281
  }
331
- $root.addRef(refId_2, value, (valueRef !== previousValue));
282
+ $root.addRef(refId, value, (valueRef !== previousValue));
332
283
  //
333
284
  // TODO: deprecate proxies on next version.
334
285
  // get proxy to target value.
@@ -337,7 +288,7 @@ var Schema = /** @class */ (function () {
337
288
  value = typeDef.getProxy(value);
338
289
  }
339
290
  }
340
- var hasChange = (previousValue !== value);
291
+ let hasChange = (previousValue !== value);
341
292
  if (value !== null &&
342
293
  value !== undefined) {
343
294
  if (value['$changes']) {
@@ -356,7 +307,7 @@ var Schema = /** @class */ (function () {
356
307
  }
357
308
  else if (ref instanceof MapSchema_1.MapSchema) {
358
309
  // const key = ref['$indexes'].get(field);
359
- var key = dynamicIndex;
310
+ const key = dynamicIndex;
360
311
  // ref.set(key, value);
361
312
  ref['$items'].set(key, value);
362
313
  ref['$changes'].allChanges.add(fieldIndex);
@@ -368,11 +319,11 @@ var Schema = /** @class */ (function () {
368
319
  ref.setAt(fieldIndex, value);
369
320
  }
370
321
  else if (ref instanceof CollectionSchema_1.CollectionSchema) {
371
- var index = ref.add(value);
322
+ const index = ref.add(value);
372
323
  ref['setIndex'](fieldIndex, index);
373
324
  }
374
325
  else if (ref instanceof SetSchema_1.SetSchema) {
375
- var index = ref.add(value);
326
+ const index = ref.add(value);
376
327
  if (index !== false) {
377
328
  ref['setIndex'](fieldIndex, index);
378
329
  }
@@ -387,9 +338,9 @@ var Schema = /** @class */ (function () {
387
338
  changes.push({
388
339
  op: operation,
389
340
  field: fieldName,
390
- dynamicIndex: dynamicIndex,
391
- value: value,
392
- previousValue: previousValue,
341
+ dynamicIndex,
342
+ value,
343
+ previousValue,
393
344
  });
394
345
  }
395
346
  }
@@ -397,19 +348,16 @@ var Schema = /** @class */ (function () {
397
348
  // drop references of unused schemas
398
349
  $root.garbageCollectDeletedRefs();
399
350
  return allChanges;
400
- };
401
- Schema.prototype.encode = function (encodeAll, bytes, useFilters) {
402
- if (encodeAll === void 0) { encodeAll = false; }
403
- if (bytes === void 0) { bytes = []; }
404
- if (useFilters === void 0) { useFilters = false; }
405
- var rootChangeTree = this.$changes;
406
- var refIdsVisited = new WeakSet();
407
- var changeTrees = [rootChangeTree];
408
- var numChangeTrees = 1;
409
- for (var i = 0; i < numChangeTrees; i++) {
410
- var changeTree = changeTrees[i];
411
- var ref = changeTree.ref;
412
- var isSchema = (ref instanceof Schema);
351
+ }
352
+ encode(encodeAll = false, bytes = [], useFilters = false) {
353
+ const rootChangeTree = this.$changes;
354
+ const refIdsVisited = new WeakSet();
355
+ const changeTrees = [rootChangeTree];
356
+ let numChangeTrees = 1;
357
+ for (let i = 0; i < numChangeTrees; i++) {
358
+ const changeTree = changeTrees[i];
359
+ const ref = changeTree.ref;
360
+ const isSchema = (ref instanceof Schema);
413
361
  // Generate unique refId for the ChangeTree.
414
362
  changeTree.ensureRefId();
415
363
  // mark this ChangeTree as visited.
@@ -420,19 +368,19 @@ var Schema = /** @class */ (function () {
420
368
  encode.uint8(bytes, spec_1.SWITCH_TO_STRUCTURE);
421
369
  encode.number(bytes, changeTree.refId);
422
370
  }
423
- var changes = (encodeAll)
371
+ const changes = (encodeAll)
424
372
  ? Array.from(changeTree.allChanges)
425
373
  : Array.from(changeTree.changes.values());
426
- for (var j = 0, cl = changes.length; j < cl; j++) {
427
- var operation = (encodeAll)
374
+ for (let j = 0, cl = changes.length; j < cl; j++) {
375
+ const operation = (encodeAll)
428
376
  ? { op: spec_1.OPERATION.ADD, index: changes[j] }
429
377
  : changes[j];
430
- var fieldIndex = operation.index;
431
- var field = (isSchema)
378
+ const fieldIndex = operation.index;
379
+ const field = (isSchema)
432
380
  ? ref['_definition'].fieldsByIndex && ref['_definition'].fieldsByIndex[fieldIndex]
433
381
  : fieldIndex;
434
382
  // cache begin index if `useFilters`
435
- var beginIndex = bytes.length;
383
+ const beginIndex = bytes.length;
436
384
  // encode field index + operation
437
385
  if (operation.op !== spec_1.OPERATION.TOUCH) {
438
386
  if (isSchema) {
@@ -462,7 +410,7 @@ var Schema = /** @class */ (function () {
462
410
  //
463
411
  // MapSchema dynamic key
464
412
  //
465
- var dynamicIndex = changeTree.ref['$indexes'].get(fieldIndex);
413
+ const dynamicIndex = changeTree.ref['$indexes'].get(fieldIndex);
466
414
  encode.string(bytes, dynamicIndex);
467
415
  }
468
416
  }
@@ -476,9 +424,9 @@ var Schema = /** @class */ (function () {
476
424
  continue;
477
425
  }
478
426
  // const type = changeTree.childType || ref._schema[field];
479
- var type = changeTree.getType(fieldIndex);
427
+ const type = changeTree.getType(fieldIndex);
480
428
  // const type = changeTree.getType(fieldIndex);
481
- var value = changeTree.getValue(fieldIndex);
429
+ const value = changeTree.getValue(fieldIndex);
482
430
  // Enqueue ChangeTree to be visited
483
431
  if (value &&
484
432
  value['$changes'] &&
@@ -512,11 +460,11 @@ var Schema = /** @class */ (function () {
512
460
  //
513
461
  // Custom type (MapSchema, ArraySchema, etc)
514
462
  //
515
- var definition = (0, types_1.getType)(Object.keys(type)[0]);
463
+ const definition = (0, types_1.getType)(Object.keys(type)[0]);
516
464
  //
517
465
  // ensure a ArraySchema has been provided
518
466
  //
519
- assertInstanceType(ref["_".concat(field)], definition.constructor, ref, field);
467
+ assertInstanceType(ref[`_${field}`], definition.constructor, ref, field);
520
468
  //
521
469
  // Encode refId for this instance.
522
470
  // The actual instance is going to be encoded on next `changeTree` iteration.
@@ -533,38 +481,37 @@ var Schema = /** @class */ (function () {
533
481
  }
534
482
  }
535
483
  return bytes;
536
- };
537
- Schema.prototype.encodeAll = function (useFilters) {
484
+ }
485
+ encodeAll(useFilters) {
538
486
  return this.encode(true, [], useFilters);
539
- };
540
- Schema.prototype.applyFilters = function (client, encodeAll) {
541
- var _a, _b;
542
- if (encodeAll === void 0) { encodeAll = false; }
543
- var root = this;
544
- var refIdsDissallowed = new Set();
545
- var $filterState = filters_1.ClientState.get(client);
546
- var changeTrees = [this.$changes];
547
- var numChangeTrees = 1;
548
- var filteredBytes = [];
549
- var _loop_1 = function (i) {
550
- var changeTree = changeTrees[i];
487
+ }
488
+ applyFilters(client, encodeAll = false) {
489
+ const root = this;
490
+ const refIdsDissallowed = new Set();
491
+ const $filterState = filters_1.ClientState.get(client);
492
+ const changeTrees = [this.$changes];
493
+ let numChangeTrees = 1;
494
+ let filteredBytes = [];
495
+ for (let i = 0; i < numChangeTrees; i++) {
496
+ const changeTree = changeTrees[i];
551
497
  if (refIdsDissallowed.has(changeTree.refId)) {
552
- return "continue";
498
+ // console.log("REFID IS NOT ALLOWED. SKIP.", { refId: changeTree.refId })
499
+ continue;
553
500
  }
554
- var ref = changeTree.ref;
555
- var isSchema = ref instanceof Schema;
501
+ const ref = changeTree.ref;
502
+ const isSchema = ref instanceof Schema;
556
503
  encode.uint8(filteredBytes, spec_1.SWITCH_TO_STRUCTURE);
557
504
  encode.number(filteredBytes, changeTree.refId);
558
- var clientHasRefId = $filterState.refIds.has(changeTree);
559
- var isEncodeAll = (encodeAll || !clientHasRefId);
505
+ const clientHasRefId = $filterState.refIds.has(changeTree);
506
+ const isEncodeAll = (encodeAll || !clientHasRefId);
560
507
  // console.log("REF:", ref.constructor.name);
561
508
  // console.log("Encode all?", isEncodeAll);
562
509
  //
563
510
  // include `changeTree` on list of known refIds by this client.
564
511
  //
565
512
  $filterState.addRefId(changeTree);
566
- var containerIndexes = $filterState.containerIndexes.get(changeTree);
567
- var changes = (isEncodeAll)
513
+ const containerIndexes = $filterState.containerIndexes.get(changeTree);
514
+ const changes = (isEncodeAll)
568
515
  ? Array.from(changeTree.allChanges)
569
516
  : Array.from(changeTree.changes.values());
570
517
  //
@@ -574,8 +521,8 @@ var Schema = /** @class */ (function () {
574
521
  if (!encodeAll &&
575
522
  isSchema &&
576
523
  ref._definition.indexesWithFilters) {
577
- var indexesWithFilters = ref._definition.indexesWithFilters;
578
- indexesWithFilters.forEach(function (indexWithFilter) {
524
+ const indexesWithFilters = ref._definition.indexesWithFilters;
525
+ indexesWithFilters.forEach(indexWithFilter => {
579
526
  if (!containerIndexes.has(indexWithFilter) &&
580
527
  changeTree.allChanges.has(indexWithFilter)) {
581
528
  if (isEncodeAll) {
@@ -587,8 +534,8 @@ var Schema = /** @class */ (function () {
587
534
  }
588
535
  });
589
536
  }
590
- for (var j = 0, cl = changes.length; j < cl; j++) {
591
- var change = (isEncodeAll)
537
+ for (let j = 0, cl = changes.length; j < cl; j++) {
538
+ const change = (isEncodeAll)
592
539
  ? { op: spec_1.OPERATION.ADD, index: changes[j] }
593
540
  : changes[j];
594
541
  // custom operations
@@ -596,7 +543,7 @@ var Schema = /** @class */ (function () {
596
543
  encode.uint8(filteredBytes, change.op);
597
544
  continue;
598
545
  }
599
- var fieldIndex = change.index;
546
+ const fieldIndex = change.index;
600
547
  //
601
548
  // Deleting fields: encode the operation + field index
602
549
  //
@@ -617,11 +564,11 @@ var Schema = /** @class */ (function () {
617
564
  continue;
618
565
  }
619
566
  // indexed operation
620
- var value = changeTree.getValue(fieldIndex);
621
- var type = changeTree.getType(fieldIndex);
567
+ const value = changeTree.getValue(fieldIndex);
568
+ const type = changeTree.getType(fieldIndex);
622
569
  if (isSchema) {
623
570
  // Is a Schema!
624
- var filter = (ref._definition.filters &&
571
+ const filter = (ref._definition.filters &&
625
572
  ref._definition.filters[fieldIndex]);
626
573
  if (filter && !filter.call(ref, client, value, root)) {
627
574
  if (value && value['$changes']) {
@@ -633,8 +580,8 @@ var Schema = /** @class */ (function () {
633
580
  }
634
581
  else {
635
582
  // Is a collection! (map, array, etc.)
636
- var parent = changeTree.parent;
637
- var filter = changeTree.getChildrenFilter();
583
+ const parent = changeTree.parent;
584
+ const filter = changeTree.getChildrenFilter();
638
585
  if (filter && !filter.call(parent, client, ref['$indexes'].get(fieldIndex), value, root)) {
639
586
  if (value && value['$changes']) {
640
587
  refIdsDissallowed.add(value['$changes'].refId);
@@ -658,7 +605,7 @@ var Schema = /** @class */ (function () {
658
605
  //
659
606
  // use cached bytes directly if is from Schema type.
660
607
  //
661
- filteredBytes.push.apply(filteredBytes, (_a = changeTree.caches[fieldIndex]) !== null && _a !== void 0 ? _a : []);
608
+ filteredBytes.push.apply(filteredBytes, changeTree.caches[fieldIndex] ?? []);
662
609
  containerIndexes.add(fieldIndex);
663
610
  }
664
611
  else {
@@ -666,7 +613,7 @@ var Schema = /** @class */ (function () {
666
613
  //
667
614
  // use cached bytes if already has the field
668
615
  //
669
- filteredBytes.push.apply(filteredBytes, (_b = changeTree.caches[fieldIndex]) !== null && _b !== void 0 ? _b : []);
616
+ filteredBytes.push.apply(filteredBytes, changeTree.caches[fieldIndex] ?? []);
670
617
  }
671
618
  else {
672
619
  //
@@ -679,7 +626,7 @@ var Schema = /** @class */ (function () {
679
626
  //
680
627
  // MapSchema dynamic key
681
628
  //
682
- var dynamicIndex = changeTree.ref['$indexes'].get(fieldIndex);
629
+ const dynamicIndex = changeTree.ref['$indexes'].get(fieldIndex);
683
630
  encode.string(filteredBytes, dynamicIndex);
684
631
  }
685
632
  if (value['$changes']) {
@@ -706,26 +653,22 @@ var Schema = /** @class */ (function () {
706
653
  //
707
654
  // MapSchema dynamic key
708
655
  //
709
- var dynamicIndex = changeTree.ref['$indexes'].get(fieldIndex);
656
+ const dynamicIndex = changeTree.ref['$indexes'].get(fieldIndex);
710
657
  encode.string(filteredBytes, dynamicIndex);
711
658
  }
712
659
  encode.number(filteredBytes, value['$changes'].refId);
713
660
  }
714
661
  }
715
662
  ;
716
- };
717
- for (var i = 0; i < numChangeTrees; i++) {
718
- _loop_1(i);
719
663
  }
720
664
  return filteredBytes;
721
- };
722
- Schema.prototype.clone = function () {
723
- var _a;
724
- var cloned = new (this.constructor);
725
- var schema = this._definition.schema;
726
- for (var field in schema) {
665
+ }
666
+ clone() {
667
+ const cloned = new (this.constructor);
668
+ const schema = this._definition.schema;
669
+ for (let field in schema) {
727
670
  if (typeof (this[field]) === "object" &&
728
- typeof ((_a = this[field]) === null || _a === void 0 ? void 0 : _a.clone) === "function") {
671
+ typeof (this[field]?.clone) === "function") {
729
672
  // deep clone
730
673
  cloned[field] = this[field].clone();
731
674
  }
@@ -735,13 +678,13 @@ var Schema = /** @class */ (function () {
735
678
  }
736
679
  }
737
680
  return cloned;
738
- };
739
- Schema.prototype.triggerAll = function () {
681
+ }
682
+ triggerAll() {
740
683
  // skip if haven't received any remote refs yet.
741
684
  if (this.$changes.root.refs.size === 0) {
742
685
  return;
743
686
  }
744
- var allChanges = new Map();
687
+ const allChanges = new Map();
745
688
  Schema.prototype._triggerAllFillChanges.call(this, this, allChanges);
746
689
  try {
747
690
  Schema.prototype._triggerChanges.call(this, allChanges);
@@ -749,65 +692,65 @@ var Schema = /** @class */ (function () {
749
692
  catch (e) {
750
693
  Schema.onError(e);
751
694
  }
752
- };
753
- Schema.prototype.toJSON = function () {
754
- var schema = this._definition.schema;
755
- var deprecated = this._definition.deprecated;
756
- var obj = {};
757
- for (var field in schema) {
695
+ }
696
+ toJSON() {
697
+ const schema = this._definition.schema;
698
+ const deprecated = this._definition.deprecated;
699
+ const obj = {};
700
+ for (let field in schema) {
758
701
  if (!deprecated[field] && this[field] !== null && typeof (this[field]) !== "undefined") {
759
702
  obj[field] = (typeof (this[field]['toJSON']) === "function")
760
703
  ? this[field]['toJSON']()
761
- : this["_".concat(field)];
704
+ : this[`_${field}`];
762
705
  }
763
706
  }
764
707
  return obj;
765
- };
766
- Schema.prototype.discardAllChanges = function () {
708
+ }
709
+ discardAllChanges() {
767
710
  this.$changes.discardAll();
768
- };
769
- Schema.prototype.getByIndex = function (index) {
711
+ }
712
+ getByIndex(index) {
770
713
  return this[this._definition.fieldsByIndex[index]];
771
- };
772
- Schema.prototype.deleteByIndex = function (index) {
714
+ }
715
+ deleteByIndex(index) {
773
716
  this[this._definition.fieldsByIndex[index]] = undefined;
774
- };
775
- Schema.prototype.tryEncodeTypeId = function (bytes, type, targetType) {
717
+ }
718
+ tryEncodeTypeId(bytes, type, targetType) {
776
719
  if (type._typeid !== targetType._typeid) {
777
720
  encode.uint8(bytes, spec_1.TYPE_ID);
778
721
  encode.number(bytes, targetType._typeid);
779
722
  }
780
- };
781
- Schema.prototype.getSchemaType = function (bytes, it, defaultType) {
782
- var type;
723
+ }
724
+ getSchemaType(bytes, it, defaultType) {
725
+ let type;
783
726
  if (bytes[it.offset] === spec_1.TYPE_ID) {
784
727
  it.offset++;
785
728
  type = this.constructor._context.get(decode.number(bytes, it));
786
729
  }
787
730
  return type || defaultType;
788
- };
789
- Schema.prototype.createTypeInstance = function (type) {
790
- var instance = new type();
731
+ }
732
+ createTypeInstance(type) {
733
+ let instance = new type();
791
734
  // assign root on $changes
792
735
  instance.$changes.root = this.$changes.root;
793
736
  return instance;
794
- };
795
- Schema.prototype._triggerAllFillChanges = function (ref, allChanges) {
737
+ }
738
+ _triggerAllFillChanges(ref, allChanges) {
796
739
  if (allChanges.has(ref['$changes'].refId)) {
797
740
  return;
798
741
  }
799
- var changes = [];
742
+ const changes = [];
800
743
  allChanges.set(ref['$changes'].refId || 0, changes);
801
744
  if (ref instanceof Schema) {
802
- var schema = ref._definition.schema;
803
- for (var fieldName in schema) {
804
- var _field = "_".concat(fieldName);
805
- var value = ref[_field];
745
+ const schema = ref._definition.schema;
746
+ for (let fieldName in schema) {
747
+ const _field = `_${fieldName}`;
748
+ const value = ref[_field];
806
749
  if (value !== undefined) {
807
750
  changes.push({
808
751
  op: spec_1.OPERATION.ADD,
809
752
  field: fieldName,
810
- value: value,
753
+ value,
811
754
  previousValue: undefined
812
755
  });
813
756
  if (value['$changes'] !== undefined) {
@@ -817,10 +760,10 @@ var Schema = /** @class */ (function () {
817
760
  }
818
761
  }
819
762
  else {
820
- var entries = ref.entries();
821
- var iter = void 0;
763
+ const entries = ref.entries();
764
+ let iter;
822
765
  while ((iter = entries.next()) && !iter.done) {
823
- var _a = __read(iter.value, 2), key = _a[0], value = _a[1];
766
+ const [key, value] = iter.value;
824
767
  changes.push({
825
768
  op: spec_1.OPERATION.ADD,
826
769
  field: key,
@@ -833,20 +776,18 @@ var Schema = /** @class */ (function () {
833
776
  }
834
777
  }
835
778
  }
836
- };
837
- Schema.prototype._triggerChanges = function (allChanges) {
838
- var _this = this;
839
- allChanges.forEach(function (changes, refId) {
840
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
779
+ }
780
+ _triggerChanges(allChanges) {
781
+ allChanges.forEach((changes, refId) => {
841
782
  if (changes.length > 0) {
842
- var ref = _this.$changes.root.refs.get(refId);
843
- var isSchema = ref instanceof Schema;
844
- for (var i = 0; i < changes.length; i++) {
845
- var change = changes[i];
846
- var listener = ref['$listeners'] && ref['$listeners'][change.field];
783
+ const ref = this.$changes.root.refs.get(refId);
784
+ const isSchema = ref instanceof Schema;
785
+ for (let i = 0; i < changes.length; i++) {
786
+ const change = changes[i];
787
+ const listener = ref['$listeners'] && ref['$listeners'][change.field];
847
788
  if (!isSchema) {
848
789
  if (change.op === spec_1.OPERATION.ADD && change.previousValue === undefined) {
849
- (_b = (_a = ref).onAdd) === null || _b === void 0 ? void 0 : _b.call(_a, change.value, (_c = change.dynamicIndex) !== null && _c !== void 0 ? _c : change.field);
790
+ ref.onAdd?.(change.value, change.dynamicIndex ?? change.field);
850
791
  }
851
792
  else if (change.op === spec_1.OPERATION.DELETE) {
852
793
  //
@@ -854,18 +795,18 @@ var Schema = /** @class */ (function () {
854
795
  // ADD + DELETE operations are still encoding DELETE operation.
855
796
  //
856
797
  if (change.previousValue !== undefined) {
857
- (_e = (_d = ref).onRemove) === null || _e === void 0 ? void 0 : _e.call(_d, change.previousValue, (_f = change.dynamicIndex) !== null && _f !== void 0 ? _f : change.field);
798
+ ref.onRemove?.(change.previousValue, change.dynamicIndex ?? change.field);
858
799
  }
859
800
  }
860
801
  else if (change.op === spec_1.OPERATION.DELETE_AND_ADD) {
861
802
  if (change.previousValue !== undefined) {
862
- (_h = (_g = ref).onRemove) === null || _h === void 0 ? void 0 : _h.call(_g, change.previousValue, change.dynamicIndex);
803
+ ref.onRemove?.(change.previousValue, change.dynamicIndex);
863
804
  }
864
- (_k = (_j = ref).onAdd) === null || _k === void 0 ? void 0 : _k.call(_j, change.value, change.dynamicIndex);
805
+ ref.onAdd?.(change.value, change.dynamicIndex);
865
806
  }
866
807
  else if (change.op === spec_1.OPERATION.REPLACE ||
867
808
  change.value !== change.previousValue) {
868
- (_m = (_l = ref).onChange) === null || _m === void 0 ? void 0 : _m.call(_l, change.value, change.dynamicIndex);
809
+ ref.onChange?.(change.value, change.dynamicIndex);
869
810
  }
870
811
  }
871
812
  //
@@ -897,9 +838,7 @@ var Schema = /** @class */ (function () {
897
838
  }
898
839
  }
899
840
  });
900
- };
901
- Schema._definition = annotations_1.SchemaDefinition.create();
902
- return Schema;
903
- }());
841
+ }
842
+ }
904
843
  exports.Schema = Schema;
905
844
  //# sourceMappingURL=Schema.js.map