@colyseus/schema 2.0.4 → 2.0.6

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 (107) hide show
  1. package/README.md +0 -4
  2. package/build/cjs/index.js +48 -48
  3. package/build/cjs/index.js.map +1 -1
  4. package/build/esm/index.mjs +130 -104
  5. package/build/esm/index.mjs.map +1 -1
  6. package/build/umd/index.js +50 -50
  7. package/lib/Reflection.js +87 -119
  8. package/lib/Reflection.js.map +1 -1
  9. package/lib/Schema.js +195 -257
  10. package/lib/Schema.js.map +1 -1
  11. package/lib/annotations.d.ts +6 -6
  12. package/lib/annotations.js +64 -92
  13. package/lib/annotations.js.map +1 -1
  14. package/lib/changes/ChangeTree.d.ts +1 -1
  15. package/lib/changes/ChangeTree.js +63 -70
  16. package/lib/changes/ChangeTree.js.map +1 -1
  17. package/lib/changes/ReferenceTracker.js +24 -27
  18. package/lib/changes/ReferenceTracker.js.map +1 -1
  19. package/lib/codegen/api.js +9 -9
  20. package/lib/codegen/api.js.map +1 -1
  21. package/lib/codegen/argv.d.ts +1 -1
  22. package/lib/codegen/argv.js +11 -11
  23. package/lib/codegen/argv.js.map +1 -1
  24. package/lib/codegen/cli.js +21 -10
  25. package/lib/codegen/cli.js.map +1 -1
  26. package/lib/codegen/languages/cpp.js +126 -77
  27. package/lib/codegen/languages/cpp.js.map +1 -1
  28. package/lib/codegen/languages/csharp.js +121 -62
  29. package/lib/codegen/languages/csharp.js.map +1 -1
  30. package/lib/codegen/languages/haxe.js +34 -26
  31. package/lib/codegen/languages/haxe.js.map +1 -1
  32. package/lib/codegen/languages/java.js +39 -27
  33. package/lib/codegen/languages/java.js.map +1 -1
  34. package/lib/codegen/languages/js.js +48 -32
  35. package/lib/codegen/languages/js.js.map +1 -1
  36. package/lib/codegen/languages/lua.js +35 -24
  37. package/lib/codegen/languages/lua.js.map +1 -1
  38. package/lib/codegen/languages/ts.js +63 -68
  39. package/lib/codegen/languages/ts.js.map +1 -1
  40. package/lib/codegen/parser.d.ts +9 -1
  41. package/lib/codegen/parser.js +88 -46
  42. package/lib/codegen/parser.js.map +1 -1
  43. package/lib/codegen/types.d.ts +8 -0
  44. package/lib/codegen/types.js +64 -54
  45. package/lib/codegen/types.js.map +1 -1
  46. package/lib/encoding/decode.js +15 -15
  47. package/lib/encoding/decode.js.map +1 -1
  48. package/lib/encoding/encode.js +14 -14
  49. package/lib/encoding/encode.js.map +1 -1
  50. package/lib/events/EventEmitter.d.ts +1 -1
  51. package/lib/events/EventEmitter.js +16 -47
  52. package/lib/events/EventEmitter.js.map +1 -1
  53. package/lib/filters/index.js +7 -8
  54. package/lib/filters/index.js.map +1 -1
  55. package/lib/index.js +11 -11
  56. package/lib/index.js.map +1 -1
  57. package/lib/types/ArraySchema.d.ts +1 -1
  58. package/lib/types/ArraySchema.js +161 -219
  59. package/lib/types/ArraySchema.js.map +1 -1
  60. package/lib/types/CollectionSchema.d.ts +1 -1
  61. package/lib/types/CollectionSchema.js +63 -71
  62. package/lib/types/CollectionSchema.js.map +1 -1
  63. package/lib/types/HelperTypes.d.ts +9 -9
  64. package/lib/types/MapSchema.d.ts +16 -16
  65. package/lib/types/MapSchema.js +68 -78
  66. package/lib/types/MapSchema.js.map +1 -1
  67. package/lib/types/SetSchema.js +62 -71
  68. package/lib/types/SetSchema.js.map +1 -1
  69. package/lib/types/index.js +1 -1
  70. package/lib/types/index.js.map +1 -1
  71. package/lib/types/typeRegistry.js +1 -1
  72. package/lib/types/typeRegistry.js.map +1 -1
  73. package/lib/types/utils.js +9 -10
  74. package/lib/types/utils.js.map +1 -1
  75. package/lib/utils.js +10 -13
  76. package/lib/utils.js.map +1 -1
  77. package/package.json +18 -15
  78. package/src/Reflection.ts +159 -0
  79. package/src/Schema.ts +1024 -0
  80. package/src/annotations.ts +400 -0
  81. package/src/changes/ChangeTree.ts +295 -0
  82. package/src/changes/ReferenceTracker.ts +81 -0
  83. package/src/codegen/api.ts +46 -0
  84. package/src/codegen/argv.ts +40 -0
  85. package/src/codegen/cli.ts +65 -0
  86. package/src/codegen/languages/cpp.ts +297 -0
  87. package/src/codegen/languages/csharp.ts +208 -0
  88. package/src/codegen/languages/haxe.ts +110 -0
  89. package/src/codegen/languages/java.ts +115 -0
  90. package/src/codegen/languages/js.ts +115 -0
  91. package/src/codegen/languages/lua.ts +125 -0
  92. package/src/codegen/languages/ts.ts +129 -0
  93. package/src/codegen/parser.ts +299 -0
  94. package/src/codegen/types.ts +177 -0
  95. package/src/encoding/decode.ts +278 -0
  96. package/src/encoding/encode.ts +283 -0
  97. package/src/filters/index.ts +23 -0
  98. package/src/index.ts +59 -0
  99. package/src/spec.ts +49 -0
  100. package/src/types/ArraySchema.ts +612 -0
  101. package/src/types/CollectionSchema.ts +199 -0
  102. package/src/types/HelperTypes.ts +34 -0
  103. package/src/types/MapSchema.ts +268 -0
  104. package/src/types/SetSchema.ts +208 -0
  105. package/src/types/typeRegistry.ts +19 -0
  106. package/src/types/utils.ts +62 -0
  107. package/src/utils.ts +28 -0
@@ -2,9 +2,9 @@
2
2
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
3
  typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.schema = {}));
5
- }(this, (function (exports) { 'use strict';
5
+ })(this, (function (exports) { 'use strict';
6
6
 
7
- /*! *****************************************************************************
7
+ /******************************************************************************
8
8
  Copyright (c) Microsoft Corporation.
9
9
 
10
10
  Permission to use, copy, modify, and/or distribute this software for any
@@ -42,10 +42,14 @@
42
42
  return c > 3 && r && Object.defineProperty(target, key, r), r;
43
43
  }
44
44
 
45
- function __spreadArray(to, from) {
46
- for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
47
- to[j] = from[i];
48
- return to;
45
+ function __spreadArray(to, from, pack) {
46
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
47
+ if (ar || !(i in from)) {
48
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
49
+ ar[i] = from[i];
50
+ }
51
+ }
52
+ return to.concat(ar || Array.prototype.slice.call(from));
49
53
  }
50
54
 
51
55
  // export const SWITCH_TO_STRUCTURE = 193; (easily collides with DELETE_AND_ADD + fieldIndex = 2)
@@ -212,7 +216,7 @@
212
216
  ? fieldName
213
217
  : this.indexes[fieldName];
214
218
  if (index === undefined) {
215
- console.warn("@colyseus/schema " + this.ref.constructor.name + ": trying to delete non-existing index: " + fieldName + " (" + index + ")");
219
+ console.warn("@colyseus/schema ".concat(this.ref.constructor.name, ": trying to delete non-existing index: ").concat(fieldName, " (").concat(index, ")"));
216
220
  return;
217
221
  }
218
222
  var previousValue = this.getValue(index);
@@ -284,7 +288,7 @@
284
288
  };
285
289
  ChangeTree.prototype.assertValidIndex = function (index, fieldName) {
286
290
  if (index === undefined) {
287
- throw new Error("ChangeTree: missing index for field \"" + fieldName + "\"");
291
+ throw new Error("ChangeTree: missing index for field \"".concat(fieldName, "\""));
288
292
  }
289
293
  };
290
294
  return ChangeTree;
@@ -522,7 +526,7 @@
522
526
  for (var _i = 0; _i < arguments.length; _i++) {
523
527
  items[_i] = arguments[_i];
524
528
  }
525
- return new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], (_a = Array.from(this.$items.values())).concat.apply(_a, items))))();
529
+ return new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], (_a = Array.from(this.$items.values())).concat.apply(_a, items), false)))();
526
530
  };
527
531
  /**
528
532
  * Adds all the elements of an array separated by the specified separator string.
@@ -562,7 +566,7 @@
562
566
  * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'.
563
567
  */
564
568
  ArraySchema.prototype.slice = function (start, end) {
565
- return new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], Array.from(this.$items.values()).slice(start, end))))();
569
+ return new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], Array.from(this.$items.values()).slice(start, end), false)))();
566
570
  };
567
571
  /**
568
572
  * Sorts an array.
@@ -591,10 +595,6 @@
591
595
  */
592
596
  ArraySchema.prototype.splice = function (start, deleteCount) {
593
597
  if (deleteCount === void 0) { deleteCount = this.length - start; }
594
- var items = [];
595
- for (var _i = 2; _i < arguments.length; _i++) {
596
- items[_i - 2] = arguments[_i];
597
- }
598
598
  var indexes = Array.from(this.$items.keys());
599
599
  var removedItems = [];
600
600
  for (var i = start; i < start + deleteCount; i++) {
@@ -689,7 +689,7 @@
689
689
  * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
690
690
  */
691
691
  ArraySchema.prototype.reduce = function (callbackfn, initialValue) {
692
- return Array.from(this.$items.values()).reduce(callbackfn, initialValue);
692
+ return Array.prototype.reduce.apply(Array.from(this.$items.values()), arguments);
693
693
  };
694
694
  /**
695
695
  * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
@@ -697,7 +697,7 @@
697
697
  * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
698
698
  */
699
699
  ArraySchema.prototype.reduceRight = function (callbackfn, initialValue) {
700
- return Array.from(this.$items.values()).reduceRight(callbackfn, initialValue);
700
+ return Array.prototype.reduceRight.apply(Array.from(this.$items.values()), arguments);
701
701
  };
702
702
  /**
703
703
  * Returns the value of the first element in the array where predicate is true, and undefined
@@ -764,9 +764,6 @@
764
764
  ArraySchema.prototype[Symbol.iterator] = function () {
765
765
  return Array.from(this.$items.values())[Symbol.iterator]();
766
766
  };
767
- ArraySchema.prototype[Symbol.unscopables] = function () {
768
- return this.$items[Symbol.unscopables]();
769
- };
770
767
  /**
771
768
  * Returns an iterable of key, value pairs for every entry in the array
772
769
  */
@@ -846,12 +843,12 @@
846
843
  ArraySchema.prototype.clone = function (isDecoding) {
847
844
  var cloned;
848
845
  if (isDecoding) {
849
- cloned = new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], Array.from(this.$items.values()))))();
846
+ cloned = new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], Array.from(this.$items.values()), false)))();
850
847
  }
851
848
  else {
852
849
  cloned = new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], this.map(function (item) { return ((item['$changes'])
853
850
  ? item.clone()
854
- : item); }))))();
851
+ : item); }), false)))();
855
852
  }
856
853
  return cloned;
857
854
  };
@@ -898,7 +895,8 @@
898
895
  this.$indexes = new Map();
899
896
  this.$refId = 0;
900
897
  if (initialValues) {
901
- if (initialValues instanceof Map) {
898
+ if (initialValues instanceof Map ||
899
+ initialValues instanceof MapSchema) {
902
900
  initialValues.forEach(function (v, k) { return _this.set(k, v); });
903
901
  }
904
902
  else {
@@ -928,7 +926,7 @@
928
926
  });
929
927
  MapSchema.prototype.set = function (key, value) {
930
928
  if (value === undefined || value === null) {
931
- throw new Error("MapSchema#set('" + key + "', " + value + "): trying to set " + value + " value on '" + key + "'.");
929
+ throw new Error("MapSchema#set('".concat(key, "', ").concat(value, "): trying to set ").concat(value, " value on '").concat(key, "'."));
932
930
  }
933
931
  // get "index" for this value.
934
932
  var hasIndex = typeof (this.$changes.indexes[key]) !== "undefined";
@@ -1123,7 +1121,7 @@
1123
1121
  return true;
1124
1122
  }
1125
1123
  else {
1126
- console.warn("@filterChildren: field '" + field + "' can't have children. Ignoring filter.");
1124
+ console.warn("@filterChildren: field '".concat(field, "' can't have children. Ignoring filter."));
1127
1125
  }
1128
1126
  };
1129
1127
  SchemaDefinition.prototype.getChildrenFilter = function (field) {
@@ -1193,7 +1191,7 @@
1193
1191
  var constructor = target.constructor;
1194
1192
  constructor._context = context;
1195
1193
  if (!type) {
1196
- throw new Error(constructor.name + ": @type() reference provided for \"" + field + "\" is undefined. Make sure you don't have any circular dependencies.");
1194
+ throw new Error("".concat(constructor.name, ": @type() reference provided for \"").concat(field, "\" is undefined. Make sure you don't have any circular dependencies."));
1197
1195
  }
1198
1196
  /*
1199
1197
  * static schema
@@ -1215,11 +1213,11 @@
1215
1213
  // trying to define same property multiple times across inheritance.
1216
1214
  // https://github.com/colyseus/colyseus-unity3d/issues/131#issuecomment-814308572
1217
1215
  try {
1218
- throw new Error("@colyseus/schema: Duplicate '" + field + "' definition on '" + constructor.name + "'.\nCheck @type() annotation");
1216
+ throw new Error("@colyseus/schema: Duplicate '".concat(field, "' definition on '").concat(constructor.name, "'.\nCheck @type() annotation"));
1219
1217
  }
1220
1218
  catch (e) {
1221
1219
  var definitionAtLine = e.stack.split("\n")[4].trim();
1222
- throw new Error(e.message + " " + definitionAtLine);
1220
+ throw new Error("".concat(e.message, " ").concat(definitionAtLine));
1223
1221
  }
1224
1222
  }
1225
1223
  }
@@ -1243,7 +1241,7 @@
1243
1241
  };
1244
1242
  return;
1245
1243
  }
1246
- var fieldCached = "_" + field;
1244
+ var fieldCached = "_".concat(field);
1247
1245
  definition.descriptors[fieldCached] = {
1248
1246
  enumerable: false,
1249
1247
  configurable: false,
@@ -1265,7 +1263,7 @@
1265
1263
  value !== null) {
1266
1264
  // automaticallty transform Array into ArraySchema
1267
1265
  if (isArray && !(value instanceof ArraySchema)) {
1268
- value = new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], value)))();
1266
+ value = new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], value, false)))();
1269
1267
  }
1270
1268
  // automaticallty transform Map into MapSchema
1271
1269
  if (isMap && !(value instanceof MapSchema)) {
@@ -1290,7 +1288,7 @@
1290
1288
  value['$changes'].setParent(this, this.$changes.root, this._definition.indexes[field]);
1291
1289
  }
1292
1290
  }
1293
- else {
1291
+ else if (this[fieldCached]) {
1294
1292
  //
1295
1293
  // Setting a field to `null` or `undefined` will delete it.
1296
1294
  //
@@ -1336,7 +1334,7 @@
1336
1334
  definition.deprecated[field] = true;
1337
1335
  if (throws) {
1338
1336
  definition.descriptors[field] = {
1339
- get: function () { throw new Error(field + " is deprecated."); },
1337
+ get: function () { throw new Error("".concat(field, " is deprecated.")); },
1340
1338
  set: function (value) { },
1341
1339
  enumerable: false,
1342
1340
  configurable: true
@@ -2309,7 +2307,7 @@
2309
2307
  case "float64":
2310
2308
  typeofTarget = "number";
2311
2309
  if (isNaN(value)) {
2312
- console.log("trying to encode \"NaN\" in " + klass.constructor.name + "#" + field);
2310
+ console.log("trying to encode \"NaN\" in ".concat(klass.constructor.name, "#").concat(field));
2313
2311
  }
2314
2312
  break;
2315
2313
  case "string":
@@ -2321,13 +2319,13 @@
2321
2319
  return;
2322
2320
  }
2323
2321
  if (typeof (value) !== typeofTarget && (!allowNull || (allowNull && value !== null))) {
2324
- var foundValue = "'" + JSON.stringify(value) + "'" + ((value && value.constructor && " (" + value.constructor.name + ")") || '');
2325
- throw new EncodeSchemaError("a '" + typeofTarget + "' was expected, but " + foundValue + " was provided in " + klass.constructor.name + "#" + field);
2322
+ var foundValue = "'".concat(JSON.stringify(value), "'").concat((value && value.constructor && " (".concat(value.constructor.name, ")")) || '');
2323
+ throw new EncodeSchemaError("a '".concat(typeofTarget, "' was expected, but ").concat(foundValue, " was provided in ").concat(klass.constructor.name, "#").concat(field));
2326
2324
  }
2327
2325
  }
2328
2326
  function assertInstanceType(value, type, klass, field) {
2329
2327
  if (!(value instanceof type)) {
2330
- throw new EncodeSchemaError("a '" + type.name + "' was expected, but '" + value.constructor.name + "' was provided in " + klass.constructor.name + "#" + field);
2328
+ throw new EncodeSchemaError("a '".concat(type.name, "' was expected, but '").concat(value.constructor.name, "' was provided in ").concat(klass.constructor.name, "#").concat(field));
2331
2329
  }
2332
2330
  }
2333
2331
  function encodePrimitiveType(type, bytes, value, klass, field) {
@@ -2337,7 +2335,7 @@
2337
2335
  encodeFunc(bytes, value);
2338
2336
  }
2339
2337
  else {
2340
- throw new EncodeSchemaError("a '" + type + "' was expected, but " + value + " was provided in " + klass.constructor.name + "#" + field);
2338
+ throw new EncodeSchemaError("a '".concat(type, "' was expected, but ").concat(value, " was provided in ").concat(klass.constructor.name, "#").concat(field));
2341
2339
  }
2342
2340
  }
2343
2341
  function decodePrimitiveType(type, bytes, it) {
@@ -2442,7 +2440,7 @@
2442
2440
  // Trying to access a reference that haven't been decoded yet.
2443
2441
  //
2444
2442
  if (!nextRef) {
2445
- throw new Error("\"refId\" not found: " + refId);
2443
+ throw new Error("\"refId\" not found: ".concat(refId));
2446
2444
  }
2447
2445
  ref = nextRef;
2448
2446
  continue;
@@ -2485,7 +2483,7 @@
2485
2483
  }
2486
2484
  }
2487
2485
  else {
2488
- previousValue = ref["_" + fieldName];
2486
+ previousValue = ref["_".concat(fieldName)];
2489
2487
  }
2490
2488
  //
2491
2489
  // Delete operations
@@ -2592,6 +2590,7 @@
2592
2590
  var key = dynamicIndex;
2593
2591
  // ref.set(key, value);
2594
2592
  ref['$items'].set(key, value);
2593
+ ref['$changes'].allChanges.add(fieldIndex);
2595
2594
  }
2596
2595
  else if (ref instanceof ArraySchema) {
2597
2596
  // const key = ref['$indexes'][field];
@@ -2744,7 +2743,7 @@
2744
2743
  //
2745
2744
  // ensure a ArraySchema has been provided
2746
2745
  //
2747
- assertInstanceType(ref["_" + field], definition.constructor, ref, field);
2746
+ assertInstanceType(ref["_".concat(field)], definition.constructor, ref, field);
2748
2747
  //
2749
2748
  // Encode refId for this instance.
2750
2749
  // The actual instance is going to be encoded on next `changeTree` iteration.
@@ -2946,11 +2945,12 @@
2946
2945
  return filteredBytes;
2947
2946
  };
2948
2947
  Schema.prototype.clone = function () {
2948
+ var _a;
2949
2949
  var cloned = new (this.constructor);
2950
2950
  var schema = this._definition.schema;
2951
2951
  for (var field in schema) {
2952
2952
  if (typeof (this[field]) === "object" &&
2953
- typeof (this[field].clone) === "function") {
2953
+ typeof ((_a = this[field]) === null || _a === void 0 ? void 0 : _a.clone) === "function") {
2954
2954
  // deep clone
2955
2955
  cloned[field] = this[field].clone();
2956
2956
  }
@@ -2969,7 +2969,7 @@
2969
2969
  if (!deprecated[field] && this[field] !== null && typeof (this[field]) !== "undefined") {
2970
2970
  obj[field] = (typeof (this[field]['toJSON']) === "function")
2971
2971
  ? this[field]['toJSON']()
2972
- : this["_" + field];
2972
+ : this["_".concat(field)];
2973
2973
  }
2974
2974
  }
2975
2975
  return obj;
@@ -3004,7 +3004,7 @@
3004
3004
  return instance;
3005
3005
  };
3006
3006
  Schema.prototype._triggerChanges = function (changes) {
3007
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
3007
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
3008
3008
  var uniqueRefIds = new Set();
3009
3009
  var $refs = this.$changes.root.refs;
3010
3010
  var _loop_2 = function (i) {
@@ -3027,7 +3027,7 @@
3027
3027
  if (!uniqueRefIds.has(refId)) {
3028
3028
  try {
3029
3029
  // trigger onChange
3030
- (_d = (_c = $callbacks) === null || _c === void 0 ? void 0 : _c[exports.OPERATION.REPLACE]) === null || _d === void 0 ? void 0 : _d.forEach(function (callback) {
3030
+ (_c = $callbacks === null || $callbacks === void 0 ? void 0 : $callbacks[exports.OPERATION.REPLACE]) === null || _c === void 0 ? void 0 : _c.forEach(function (callback) {
3031
3031
  return callback(changes);
3032
3032
  });
3033
3033
  }
@@ -3037,7 +3037,7 @@
3037
3037
  }
3038
3038
  try {
3039
3039
  if ($callbacks.hasOwnProperty(change.field)) {
3040
- (_e = $callbacks[change.field]) === null || _e === void 0 ? void 0 : _e.forEach(function (callback) {
3040
+ (_d = $callbacks[change.field]) === null || _d === void 0 ? void 0 : _d.forEach(function (callback) {
3041
3041
  return callback(change.value, change.previousValue);
3042
3042
  });
3043
3043
  }
@@ -3050,7 +3050,7 @@
3050
3050
  // is a collection of items
3051
3051
  if (change.op === exports.OPERATION.ADD && change.previousValue === undefined) {
3052
3052
  // triger onAdd
3053
- (_f = $callbacks[exports.OPERATION.ADD]) === null || _f === void 0 ? void 0 : _f.forEach(function (callback) { var _a; return callback(change.value, (_a = change.dynamicIndex) !== null && _a !== void 0 ? _a : change.field); });
3053
+ (_e = $callbacks[exports.OPERATION.ADD]) === null || _e === void 0 ? void 0 : _e.forEach(function (callback) { var _a; return callback(change.value, (_a = change.dynamicIndex) !== null && _a !== void 0 ? _a : change.field); });
3054
3054
  }
3055
3055
  else if (change.op === exports.OPERATION.DELETE) {
3056
3056
  //
@@ -3059,20 +3059,20 @@
3059
3059
  //
3060
3060
  if (change.previousValue !== undefined) {
3061
3061
  // triger onRemove
3062
- (_g = $callbacks[exports.OPERATION.DELETE]) === null || _g === void 0 ? void 0 : _g.forEach(function (callback) { var _a; return callback(change.previousValue, (_a = change.dynamicIndex) !== null && _a !== void 0 ? _a : change.field); });
3062
+ (_f = $callbacks[exports.OPERATION.DELETE]) === null || _f === void 0 ? void 0 : _f.forEach(function (callback) { var _a; return callback(change.previousValue, (_a = change.dynamicIndex) !== null && _a !== void 0 ? _a : change.field); });
3063
3063
  }
3064
3064
  }
3065
3065
  else if (change.op === exports.OPERATION.DELETE_AND_ADD) {
3066
3066
  // triger onRemove
3067
3067
  if (change.previousValue !== undefined) {
3068
- (_h = $callbacks[exports.OPERATION.DELETE]) === null || _h === void 0 ? void 0 : _h.forEach(function (callback) { var _a; return callback(change.previousValue, (_a = change.dynamicIndex) !== null && _a !== void 0 ? _a : change.field); });
3068
+ (_g = $callbacks[exports.OPERATION.DELETE]) === null || _g === void 0 ? void 0 : _g.forEach(function (callback) { var _a; return callback(change.previousValue, (_a = change.dynamicIndex) !== null && _a !== void 0 ? _a : change.field); });
3069
3069
  }
3070
3070
  // triger onAdd
3071
- (_j = $callbacks[exports.OPERATION.ADD]) === null || _j === void 0 ? void 0 : _j.forEach(function (callback) { var _a; return callback(change.value, (_a = change.dynamicIndex) !== null && _a !== void 0 ? _a : change.field); });
3071
+ (_h = $callbacks[exports.OPERATION.ADD]) === null || _h === void 0 ? void 0 : _h.forEach(function (callback) { var _a; return callback(change.value, (_a = change.dynamicIndex) !== null && _a !== void 0 ? _a : change.field); });
3072
3072
  }
3073
3073
  // trigger onChange
3074
3074
  if (change.value !== change.previousValue) {
3075
- (_k = $callbacks[exports.OPERATION.REPLACE]) === null || _k === void 0 ? void 0 : _k.forEach(function (callback) { var _a; return callback(change.value, (_a = change.dynamicIndex) !== null && _a !== void 0 ? _a : change.field); });
3075
+ (_j = $callbacks[exports.OPERATION.REPLACE]) === null || _j === void 0 ? void 0 : _j.forEach(function (callback) { var _a; return callback(change.value, (_a = change.dynamicIndex) !== null && _a !== void 0 ? _a : change.field); });
3076
3076
  }
3077
3077
  }
3078
3078
  uniqueRefIds.add(refId);
@@ -3292,4 +3292,4 @@
3292
3292
 
3293
3293
  Object.defineProperty(exports, '__esModule', { value: true });
3294
3294
 
3295
- })));
3295
+ }));
package/lib/Reflection.js CHANGED
@@ -1,19 +1,4 @@
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
2
  var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18
3
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19
4
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -25,88 +10,78 @@ var __metadata = (this && this.__metadata) || function (k, v) {
25
10
  };
26
11
  Object.defineProperty(exports, "__esModule", { value: true });
27
12
  exports.Reflection = exports.ReflectionType = exports.ReflectionField = void 0;
28
- var annotations_1 = require("./annotations");
29
- var Schema_1 = require("./Schema");
30
- var ArraySchema_1 = require("./types/ArraySchema");
31
- var typeRegistry_1 = require("./types/typeRegistry");
32
- var reflectionContext = { context: new annotations_1.Context() };
13
+ const annotations_1 = require("./annotations");
14
+ const Schema_1 = require("./Schema");
15
+ const ArraySchema_1 = require("./types/ArraySchema");
16
+ const typeRegistry_1 = require("./types/typeRegistry");
17
+ const reflectionContext = { context: new annotations_1.Context() };
33
18
  /**
34
19
  * Reflection
35
20
  */
36
- var ReflectionField = /** @class */ (function (_super) {
37
- __extends(ReflectionField, _super);
38
- function ReflectionField() {
39
- return _super !== null && _super.apply(this, arguments) || this;
40
- }
41
- __decorate([
42
- annotations_1.type("string", reflectionContext),
43
- __metadata("design:type", String)
44
- ], ReflectionField.prototype, "name", void 0);
45
- __decorate([
46
- annotations_1.type("string", reflectionContext),
47
- __metadata("design:type", String)
48
- ], ReflectionField.prototype, "type", void 0);
49
- __decorate([
50
- annotations_1.type("number", reflectionContext),
51
- __metadata("design:type", Number)
52
- ], ReflectionField.prototype, "referencedType", void 0);
53
- return ReflectionField;
54
- }(Schema_1.Schema));
21
+ class ReflectionField extends Schema_1.Schema {
22
+ }
23
+ __decorate([
24
+ (0, annotations_1.type)("string", reflectionContext),
25
+ __metadata("design:type", String)
26
+ ], ReflectionField.prototype, "name", void 0);
27
+ __decorate([
28
+ (0, annotations_1.type)("string", reflectionContext),
29
+ __metadata("design:type", String)
30
+ ], ReflectionField.prototype, "type", void 0);
31
+ __decorate([
32
+ (0, annotations_1.type)("number", reflectionContext),
33
+ __metadata("design:type", Number)
34
+ ], ReflectionField.prototype, "referencedType", void 0);
55
35
  exports.ReflectionField = ReflectionField;
56
- var ReflectionType = /** @class */ (function (_super) {
57
- __extends(ReflectionType, _super);
58
- function ReflectionType() {
59
- var _this = _super !== null && _super.apply(this, arguments) || this;
60
- _this.fields = new ArraySchema_1.ArraySchema();
61
- return _this;
36
+ class ReflectionType extends Schema_1.Schema {
37
+ constructor() {
38
+ super(...arguments);
39
+ this.fields = new ArraySchema_1.ArraySchema();
62
40
  }
63
- __decorate([
64
- annotations_1.type("number", reflectionContext),
65
- __metadata("design:type", Number)
66
- ], ReflectionType.prototype, "id", void 0);
67
- __decorate([
68
- annotations_1.type([ReflectionField], reflectionContext),
69
- __metadata("design:type", ArraySchema_1.ArraySchema)
70
- ], ReflectionType.prototype, "fields", void 0);
71
- return ReflectionType;
72
- }(Schema_1.Schema));
41
+ }
42
+ __decorate([
43
+ (0, annotations_1.type)("number", reflectionContext),
44
+ __metadata("design:type", Number)
45
+ ], ReflectionType.prototype, "id", void 0);
46
+ __decorate([
47
+ (0, annotations_1.type)([ReflectionField], reflectionContext),
48
+ __metadata("design:type", ArraySchema_1.ArraySchema)
49
+ ], ReflectionType.prototype, "fields", void 0);
73
50
  exports.ReflectionType = ReflectionType;
74
- var Reflection = /** @class */ (function (_super) {
75
- __extends(Reflection, _super);
76
- function Reflection() {
77
- var _this = _super !== null && _super.apply(this, arguments) || this;
78
- _this.types = new ArraySchema_1.ArraySchema();
79
- return _this;
51
+ class Reflection extends Schema_1.Schema {
52
+ constructor() {
53
+ super(...arguments);
54
+ this.types = new ArraySchema_1.ArraySchema();
80
55
  }
81
- Reflection.encode = function (instance) {
82
- var rootSchemaType = instance.constructor;
83
- var reflection = new Reflection();
56
+ static encode(instance) {
57
+ const rootSchemaType = instance.constructor;
58
+ const reflection = new Reflection();
84
59
  reflection.rootType = rootSchemaType._typeid;
85
- var buildType = function (currentType, schema) {
86
- for (var fieldName in schema) {
87
- var field = new ReflectionField();
60
+ const buildType = (currentType, schema) => {
61
+ for (let fieldName in schema) {
62
+ const field = new ReflectionField();
88
63
  field.name = fieldName;
89
- var fieldType = void 0;
64
+ let fieldType;
90
65
  if (typeof (schema[fieldName]) === "string") {
91
66
  fieldType = schema[fieldName];
92
67
  }
93
68
  else {
94
- var type_1 = schema[fieldName];
95
- var childTypeSchema = void 0;
69
+ const type = schema[fieldName];
70
+ let childTypeSchema;
96
71
  //
97
72
  // TODO: refactor below.
98
73
  //
99
- if (Schema_1.Schema.is(type_1)) {
74
+ if (Schema_1.Schema.is(type)) {
100
75
  fieldType = "ref";
101
76
  childTypeSchema = schema[fieldName];
102
77
  }
103
78
  else {
104
- fieldType = Object.keys(type_1)[0];
105
- if (typeof (type_1[fieldType]) === "string") {
106
- fieldType += ":" + type_1[fieldType]; // array:string
79
+ fieldType = Object.keys(type)[0];
80
+ if (typeof (type[fieldType]) === "string") {
81
+ fieldType += ":" + type[fieldType]; // array:string
107
82
  }
108
83
  else {
109
- childTypeSchema = type_1[fieldType];
84
+ childTypeSchema = type[fieldType];
110
85
  }
111
86
  }
112
87
  field.referencedType = (childTypeSchema)
@@ -118,81 +93,74 @@ var Reflection = /** @class */ (function (_super) {
118
93
  }
119
94
  reflection.types.push(currentType);
120
95
  };
121
- var types = rootSchemaType._context.types;
122
- for (var typeid in types) {
123
- var type_2 = new ReflectionType();
124
- type_2.id = Number(typeid);
125
- buildType(type_2, types[typeid]._definition.schema);
96
+ const types = rootSchemaType._context.types;
97
+ for (let typeid in types) {
98
+ const type = new ReflectionType();
99
+ type.id = Number(typeid);
100
+ buildType(type, types[typeid]._definition.schema);
126
101
  }
127
102
  return reflection.encodeAll();
128
- };
129
- Reflection.decode = function (bytes, it) {
130
- var context = new annotations_1.Context();
131
- var reflection = new Reflection();
103
+ }
104
+ static decode(bytes, it) {
105
+ const context = new annotations_1.Context();
106
+ const reflection = new Reflection();
132
107
  reflection.decode(bytes, it);
133
- var schemaTypes = reflection.types.reduce(function (types, reflectionType) {
134
- var schema = /** @class */ (function (_super) {
135
- __extends(_, _super);
136
- function _() {
137
- return _super !== null && _super.apply(this, arguments) || this;
138
- }
139
- return _;
140
- }(Schema_1.Schema));
141
- var typeid = reflectionType.id;
108
+ const schemaTypes = reflection.types.reduce((types, reflectionType) => {
109
+ const schema = class _ extends Schema_1.Schema {
110
+ };
111
+ const typeid = reflectionType.id;
142
112
  types[typeid] = schema;
143
113
  context.add(schema, typeid);
144
114
  return types;
145
115
  }, {});
146
- reflection.types.forEach(function (reflectionType) {
147
- var schemaType = schemaTypes[reflectionType.id];
148
- reflectionType.fields.forEach(function (field) {
149
- var _a;
116
+ reflection.types.forEach((reflectionType) => {
117
+ const schemaType = schemaTypes[reflectionType.id];
118
+ reflectionType.fields.forEach(field => {
150
119
  if (field.referencedType !== undefined) {
151
- var fieldType = field.type;
152
- var refType = schemaTypes[field.referencedType];
120
+ let fieldType = field.type;
121
+ let refType = schemaTypes[field.referencedType];
153
122
  // map or array of primitive type (-1)
154
123
  if (!refType) {
155
- var typeInfo = field.type.split(":");
124
+ const typeInfo = field.type.split(":");
156
125
  fieldType = typeInfo[0];
157
126
  refType = typeInfo[1];
158
127
  }
159
128
  if (fieldType === "ref") {
160
- annotations_1.type(refType, { context: context })(schemaType.prototype, field.name);
129
+ (0, annotations_1.type)(refType, { context })(schemaType.prototype, field.name);
161
130
  }
162
131
  else {
163
- annotations_1.type((_a = {}, _a[fieldType] = refType, _a), { context: context })(schemaType.prototype, field.name);
132
+ (0, annotations_1.type)({ [fieldType]: refType }, { context })(schemaType.prototype, field.name);
164
133
  }
165
134
  }
166
135
  else {
167
- annotations_1.type(field.type, { context: context })(schemaType.prototype, field.name);
136
+ (0, annotations_1.type)(field.type, { context })(schemaType.prototype, field.name);
168
137
  }
169
138
  });
170
139
  });
171
- var rootType = schemaTypes[reflection.rootType];
172
- var rootInstance = new rootType();
140
+ const rootType = schemaTypes[reflection.rootType];
141
+ const rootInstance = new rootType();
173
142
  /**
174
143
  * auto-initialize referenced types on root type
175
144
  * to allow registering listeners immediatelly on client-side
176
145
  */
177
- for (var fieldName in rootType._definition.schema) {
178
- var fieldType = rootType._definition.schema[fieldName];
146
+ for (let fieldName in rootType._definition.schema) {
147
+ const fieldType = rootType._definition.schema[fieldName];
179
148
  if (typeof (fieldType) !== "string") {
180
149
  rootInstance[fieldName] = (typeof (fieldType) === "function")
181
150
  ? new fieldType() // is a schema reference
182
- : new (typeRegistry_1.getType(Object.keys(fieldType)[0])).constructor(); // is a "collection"
151
+ : new ((0, typeRegistry_1.getType)(Object.keys(fieldType)[0])).constructor(); // is a "collection"
183
152
  }
184
153
  }
185
154
  return rootInstance;
186
- };
187
- __decorate([
188
- annotations_1.type([ReflectionType], reflectionContext),
189
- __metadata("design:type", ArraySchema_1.ArraySchema)
190
- ], Reflection.prototype, "types", void 0);
191
- __decorate([
192
- annotations_1.type("number", reflectionContext),
193
- __metadata("design:type", Number)
194
- ], Reflection.prototype, "rootType", void 0);
195
- return Reflection;
196
- }(Schema_1.Schema));
155
+ }
156
+ }
157
+ __decorate([
158
+ (0, annotations_1.type)([ReflectionType], reflectionContext),
159
+ __metadata("design:type", ArraySchema_1.ArraySchema)
160
+ ], Reflection.prototype, "types", void 0);
161
+ __decorate([
162
+ (0, annotations_1.type)("number", reflectionContext),
163
+ __metadata("design:type", Number)
164
+ ], Reflection.prototype, "rootType", void 0);
197
165
  exports.Reflection = Reflection;
198
166
  //# sourceMappingURL=Reflection.js.map