@colyseus/schema 1.0.33 → 1.0.36

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 (76) hide show
  1. package/README.md +0 -4
  2. package/build/cjs/index.js +36 -35
  3. package/build/cjs/index.js.map +1 -1
  4. package/build/esm/index.mjs +114 -85
  5. package/build/esm/index.mjs.map +1 -1
  6. package/build/umd/index.js +38 -37
  7. package/lib/Reflection.js +11 -11
  8. package/lib/Reflection.js.map +1 -1
  9. package/lib/Schema.js +14 -13
  10. package/lib/Schema.js.map +1 -1
  11. package/lib/annotations.js +17 -13
  12. package/lib/annotations.js.map +1 -1
  13. package/lib/changes/ChangeTree.js +2 -2
  14. package/lib/changes/ChangeTree.js.map +1 -1
  15. package/lib/codegen/api.js +1 -1
  16. package/lib/codegen/api.js.map +1 -1
  17. package/lib/codegen/argv.d.ts +1 -1
  18. package/lib/codegen/cli.js +5 -5
  19. package/lib/codegen/cli.js.map +1 -1
  20. package/lib/codegen/languages/cpp.js +38 -38
  21. package/lib/codegen/languages/cpp.js.map +1 -1
  22. package/lib/codegen/languages/csharp.js +25 -21
  23. package/lib/codegen/languages/csharp.js.map +1 -1
  24. package/lib/codegen/languages/haxe.js +13 -13
  25. package/lib/codegen/languages/haxe.js.map +1 -1
  26. package/lib/codegen/languages/java.js +11 -11
  27. package/lib/codegen/languages/java.js.map +1 -1
  28. package/lib/codegen/languages/js.js +16 -16
  29. package/lib/codegen/languages/js.js.map +1 -1
  30. package/lib/codegen/languages/lua.js +12 -12
  31. package/lib/codegen/languages/lua.js.map +1 -1
  32. package/lib/codegen/languages/ts.js +37 -33
  33. package/lib/codegen/languages/ts.js.map +1 -1
  34. package/lib/codegen/parser.js +3 -3
  35. package/lib/codegen/parser.js.map +1 -1
  36. package/lib/codegen/types.js +2 -2
  37. package/lib/codegen/types.js.map +1 -1
  38. package/lib/events/EventEmitter.js +10 -6
  39. package/lib/events/EventEmitter.js.map +1 -1
  40. package/lib/index.js +4 -4
  41. package/lib/index.js.map +1 -1
  42. package/lib/types/ArraySchema.js +14 -10
  43. package/lib/types/ArraySchema.js.map +1 -1
  44. package/lib/types/MapSchema.d.ts +19 -19
  45. package/lib/types/MapSchema.js +2 -2
  46. package/lib/types/MapSchema.js.map +1 -1
  47. package/package.json +2 -1
  48. package/src/Reflection.ts +159 -0
  49. package/src/Schema.ts +1084 -0
  50. package/src/annotations.ts +357 -0
  51. package/src/changes/ChangeTree.ts +373 -0
  52. package/src/codegen/api.ts +46 -0
  53. package/src/codegen/argv.ts +40 -0
  54. package/src/codegen/cli.ts +65 -0
  55. package/src/codegen/languages/cpp.ts +297 -0
  56. package/src/codegen/languages/csharp.ts +119 -0
  57. package/src/codegen/languages/haxe.ts +110 -0
  58. package/src/codegen/languages/java.ts +115 -0
  59. package/src/codegen/languages/js.ts +115 -0
  60. package/src/codegen/languages/lua.ts +125 -0
  61. package/src/codegen/languages/ts.ts +129 -0
  62. package/src/codegen/parser.ts +251 -0
  63. package/src/codegen/types.ts +164 -0
  64. package/src/encoding/decode.ts +278 -0
  65. package/src/encoding/encode.ts +283 -0
  66. package/src/events/EventEmitter.ts +32 -0
  67. package/src/filters/index.ts +23 -0
  68. package/src/index.ts +59 -0
  69. package/src/spec.ts +49 -0
  70. package/src/types/ArraySchema.ts +608 -0
  71. package/src/types/CollectionSchema.ts +188 -0
  72. package/src/types/HelperTypes.ts +34 -0
  73. package/src/types/MapSchema.ts +255 -0
  74. package/src/types/SetSchema.ts +197 -0
  75. package/src/types/index.ts +19 -0
  76. 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)
@@ -282,7 +286,7 @@
282
286
  ? fieldName
283
287
  : this.indexes[fieldName];
284
288
  if (index === undefined) {
285
- console.warn("@colyseus/schema " + this.ref.constructor.name + ": trying to delete non-existing index: " + fieldName + " (" + index + ")");
289
+ console.warn("@colyseus/schema ".concat(this.ref.constructor.name, ": trying to delete non-existing index: ").concat(fieldName, " (").concat(index, ")"));
286
290
  return;
287
291
  }
288
292
  var previousValue = this.getValue(index);
@@ -354,7 +358,7 @@
354
358
  };
355
359
  ChangeTree.prototype.assertValidIndex = function (index, fieldName) {
356
360
  if (index === undefined) {
357
- throw new Error("ChangeTree: missing index for field \"" + fieldName + "\"");
361
+ throw new Error("ChangeTree: missing index for field \"".concat(fieldName, "\""));
358
362
  }
359
363
  };
360
364
  return ChangeTree;
@@ -547,7 +551,7 @@
547
551
  for (var _i = 0; _i < arguments.length; _i++) {
548
552
  items[_i] = arguments[_i];
549
553
  }
550
- return new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], (_a = Array.from(this.$items.values())).concat.apply(_a, items))))();
554
+ return new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], (_a = Array.from(this.$items.values())).concat.apply(_a, items), false)))();
551
555
  };
552
556
  /**
553
557
  * Adds all the elements of an array separated by the specified separator string.
@@ -587,7 +591,7 @@
587
591
  * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'.
588
592
  */
589
593
  ArraySchema.prototype.slice = function (start, end) {
590
- return new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], Array.from(this.$items.values()).slice(start, end))))();
594
+ return new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], Array.from(this.$items.values()).slice(start, end), false)))();
591
595
  };
592
596
  /**
593
597
  * Sorts an array.
@@ -616,10 +620,6 @@
616
620
  */
617
621
  ArraySchema.prototype.splice = function (start, deleteCount) {
618
622
  if (deleteCount === void 0) { deleteCount = this.length - start; }
619
- var items = [];
620
- for (var _i = 2; _i < arguments.length; _i++) {
621
- items[_i - 2] = arguments[_i];
622
- }
623
623
  var indexes = Array.from(this.$items.keys());
624
624
  var removedItems = [];
625
625
  for (var i = start; i < start + deleteCount; i++) {
@@ -714,7 +714,7 @@
714
714
  * @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.
715
715
  */
716
716
  ArraySchema.prototype.reduce = function (callbackfn, initialValue) {
717
- return Array.from(this.$items.values()).reduce.apply(this, arguments);
717
+ return Array.prototype.reduce.apply(Array.from(this.$items.values()), arguments);
718
718
  };
719
719
  /**
720
720
  * 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.
@@ -722,7 +722,7 @@
722
722
  * @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.
723
723
  */
724
724
  ArraySchema.prototype.reduceRight = function (callbackfn, initialValue) {
725
- return Array.from(this.$items.values()).reduceRight.apply(this, arguments);
725
+ return Array.prototype.reduceRight.apply(Array.from(this.$items.values()), arguments);
726
726
  };
727
727
  /**
728
728
  * Returns the value of the first element in the array where predicate is true, and undefined
@@ -871,12 +871,12 @@
871
871
  ArraySchema.prototype.clone = function (isDecoding) {
872
872
  var cloned;
873
873
  if (isDecoding) {
874
- cloned = new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], Array.from(this.$items.values()))))();
874
+ cloned = new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], Array.from(this.$items.values()), false)))();
875
875
  }
876
876
  else {
877
877
  cloned = new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], this.map(function (item) { return ((item['$changes'])
878
878
  ? item.clone()
879
- : item); }))))();
879
+ : item); }), false)))();
880
880
  }
881
881
  return cloned;
882
882
  };
@@ -948,7 +948,7 @@
948
948
  });
949
949
  MapSchema.prototype.set = function (key, value) {
950
950
  if (value === undefined || value === null) {
951
- throw new Error("MapSchema#set('" + key + "', " + value + "): trying to set " + value + " value on '" + key + "'.");
951
+ throw new Error("MapSchema#set('".concat(key, "', ").concat(value, "): trying to set ").concat(value, " value on '").concat(key, "'."));
952
952
  }
953
953
  // get "index" for this value.
954
954
  var hasIndex = typeof (this.$changes.indexes[key]) !== "undefined";
@@ -1142,7 +1142,7 @@
1142
1142
  return true;
1143
1143
  }
1144
1144
  else {
1145
- console.warn("@filterChildren: field '" + field + "' can't have children. Ignoring filter.");
1145
+ console.warn("@filterChildren: field '".concat(field, "' can't have children. Ignoring filter."));
1146
1146
  }
1147
1147
  };
1148
1148
  SchemaDefinition.prototype.getChildrenFilter = function (field) {
@@ -1217,11 +1217,11 @@
1217
1217
  // trying to define same property multiple times across inheritance.
1218
1218
  // https://github.com/colyseus/colyseus-unity3d/issues/131#issuecomment-814308572
1219
1219
  try {
1220
- throw new Error("@colyseus/schema: Duplicate '" + field + "' definition on '" + constructor.name + "'.\nCheck @type() annotation");
1220
+ throw new Error("@colyseus/schema: Duplicate '".concat(field, "' definition on '").concat(constructor.name, "'.\nCheck @type() annotation"));
1221
1221
  }
1222
1222
  catch (e) {
1223
1223
  var definitionAtLine = e.stack.split("\n")[4].trim();
1224
- throw new Error(e.message + " " + definitionAtLine);
1224
+ throw new Error("".concat(e.message, " ").concat(definitionAtLine));
1225
1225
  }
1226
1226
  }
1227
1227
  }
@@ -1236,7 +1236,7 @@
1236
1236
  context.add(childType);
1237
1237
  }
1238
1238
  }
1239
- var fieldCached = "_" + field;
1239
+ var fieldCached = "_".concat(field);
1240
1240
  definition.descriptors[fieldCached] = {
1241
1241
  enumerable: false,
1242
1242
  configurable: false,
@@ -1258,7 +1258,7 @@
1258
1258
  value !== null) {
1259
1259
  // automaticallty transform Array into ArraySchema
1260
1260
  if (isArray && !(value instanceof ArraySchema)) {
1261
- value = new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], value)))();
1261
+ value = new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], value, false)))();
1262
1262
  }
1263
1263
  // automaticallty transform Map into MapSchema
1264
1264
  if (isMap && !(value instanceof MapSchema)) {
@@ -1329,7 +1329,7 @@
1329
1329
  definition.deprecated[field] = true;
1330
1330
  if (throws) {
1331
1331
  definition.descriptors[field] = {
1332
- get: function () { throw new Error(field + " is deprecated."); },
1332
+ get: function () { throw new Error("".concat(field, " is deprecated.")); },
1333
1333
  set: function (value) { },
1334
1334
  enumerable: false,
1335
1335
  configurable: true
@@ -2251,7 +2251,7 @@
2251
2251
  case "float64":
2252
2252
  typeofTarget = "number";
2253
2253
  if (isNaN(value)) {
2254
- console.log("trying to encode \"NaN\" in " + klass.constructor.name + "#" + field);
2254
+ console.log("trying to encode \"NaN\" in ".concat(klass.constructor.name, "#").concat(field));
2255
2255
  }
2256
2256
  break;
2257
2257
  case "string":
@@ -2263,13 +2263,13 @@
2263
2263
  return;
2264
2264
  }
2265
2265
  if (typeof (value) !== typeofTarget && (!allowNull || (allowNull && value !== null))) {
2266
- var foundValue = "'" + JSON.stringify(value) + "'" + ((value && value.constructor && " (" + value.constructor.name + ")") || '');
2267
- throw new EncodeSchemaError("a '" + typeofTarget + "' was expected, but " + foundValue + " was provided in " + klass.constructor.name + "#" + field);
2266
+ var foundValue = "'".concat(JSON.stringify(value), "'").concat((value && value.constructor && " (".concat(value.constructor.name, ")")) || '');
2267
+ throw new EncodeSchemaError("a '".concat(typeofTarget, "' was expected, but ").concat(foundValue, " was provided in ").concat(klass.constructor.name, "#").concat(field));
2268
2268
  }
2269
2269
  }
2270
2270
  function assertInstanceType(value, type, klass, field) {
2271
2271
  if (!(value instanceof type)) {
2272
- throw new EncodeSchemaError("a '" + type.name + "' was expected, but '" + value.constructor.name + "' was provided in " + klass.constructor.name + "#" + field);
2272
+ throw new EncodeSchemaError("a '".concat(type.name, "' was expected, but '").concat(value.constructor.name, "' was provided in ").concat(klass.constructor.name, "#").concat(field));
2273
2273
  }
2274
2274
  }
2275
2275
  function encodePrimitiveType(type, bytes, value, klass, field) {
@@ -2279,7 +2279,7 @@
2279
2279
  encodeFunc(bytes, value);
2280
2280
  }
2281
2281
  else {
2282
- throw new EncodeSchemaError("a '" + type + "' was expected, but " + value + " was provided in " + klass.constructor.name + "#" + field);
2282
+ throw new EncodeSchemaError("a '".concat(type, "' was expected, but ").concat(value, " was provided in ").concat(klass.constructor.name, "#").concat(field));
2283
2283
  }
2284
2284
  }
2285
2285
  function decodePrimitiveType(type, bytes, it) {
@@ -2365,7 +2365,7 @@
2365
2365
  // Trying to access a reference that haven't been decoded yet.
2366
2366
  //
2367
2367
  if (!nextRef) {
2368
- throw new Error("\"refId\" not found: " + refId);
2368
+ throw new Error("\"refId\" not found: ".concat(refId));
2369
2369
  }
2370
2370
  ref = nextRef;
2371
2371
  // create empty list of changes for this refId.
@@ -2411,7 +2411,7 @@
2411
2411
  }
2412
2412
  }
2413
2413
  else {
2414
- previousValue = ref["_" + fieldName];
2414
+ previousValue = ref["_".concat(fieldName)];
2415
2415
  }
2416
2416
  //
2417
2417
  // Delete operations
@@ -2693,7 +2693,7 @@
2693
2693
  //
2694
2694
  // ensure a ArraySchema has been provided
2695
2695
  //
2696
- assertInstanceType(ref["_" + field], definition.constructor, ref, field);
2696
+ assertInstanceType(ref["_".concat(field)], definition.constructor, ref, field);
2697
2697
  //
2698
2698
  // Encode refId for this instance.
2699
2699
  // The actual instance is going to be encoded on next `changeTree` iteration.
@@ -2895,11 +2895,12 @@
2895
2895
  return filteredBytes;
2896
2896
  };
2897
2897
  Schema.prototype.clone = function () {
2898
+ var _a;
2898
2899
  var cloned = new (this.constructor);
2899
2900
  var schema = this._definition.schema;
2900
2901
  for (var field in schema) {
2901
2902
  if (typeof (this[field]) === "object" &&
2902
- typeof (this[field].clone) === "function") {
2903
+ typeof ((_a = this[field]) === null || _a === void 0 ? void 0 : _a.clone) === "function") {
2903
2904
  // deep clone
2904
2905
  cloned[field] = this[field].clone();
2905
2906
  }
@@ -2932,7 +2933,7 @@
2932
2933
  if (!deprecated[field] && this[field] !== null && typeof (this[field]) !== "undefined") {
2933
2934
  obj[field] = (typeof (this[field]['toJSON']) === "function")
2934
2935
  ? this[field]['toJSON']()
2935
- : this["_" + field];
2936
+ : this["_".concat(field)];
2936
2937
  }
2937
2938
  }
2938
2939
  return obj;
@@ -2975,7 +2976,7 @@
2975
2976
  if (ref instanceof Schema) {
2976
2977
  var schema = ref._definition.schema;
2977
2978
  for (var fieldName in schema) {
2978
- var _field = "_" + fieldName;
2979
+ var _field = "_".concat(fieldName);
2979
2980
  var value = ref[_field];
2980
2981
  if (value !== undefined) {
2981
2982
  changes.push({
@@ -3283,4 +3284,4 @@
3283
3284
 
3284
3285
  Object.defineProperty(exports, '__esModule', { value: true });
3285
3286
 
3286
- })));
3287
+ }));
package/lib/Reflection.js CHANGED
@@ -36,13 +36,13 @@ var ReflectionField = /** @class */ (function (_super) {
36
36
  return _super !== null && _super.apply(this, arguments) || this;
37
37
  }
38
38
  __decorate([
39
- annotations_1.type("string", reflectionContext)
39
+ (0, annotations_1.type)("string", reflectionContext)
40
40
  ], ReflectionField.prototype, "name", void 0);
41
41
  __decorate([
42
- annotations_1.type("string", reflectionContext)
42
+ (0, annotations_1.type)("string", reflectionContext)
43
43
  ], ReflectionField.prototype, "type", void 0);
44
44
  __decorate([
45
- annotations_1.type("number", reflectionContext)
45
+ (0, annotations_1.type)("number", reflectionContext)
46
46
  ], ReflectionField.prototype, "referencedType", void 0);
47
47
  return ReflectionField;
48
48
  }(Schema_1.Schema));
@@ -55,10 +55,10 @@ var ReflectionType = /** @class */ (function (_super) {
55
55
  return _this;
56
56
  }
57
57
  __decorate([
58
- annotations_1.type("number", reflectionContext)
58
+ (0, annotations_1.type)("number", reflectionContext)
59
59
  ], ReflectionType.prototype, "id", void 0);
60
60
  __decorate([
61
- annotations_1.type([ReflectionField], reflectionContext)
61
+ (0, annotations_1.type)([ReflectionField], reflectionContext)
62
62
  ], ReflectionType.prototype, "fields", void 0);
63
63
  return ReflectionType;
64
64
  }(Schema_1.Schema));
@@ -149,14 +149,14 @@ var Reflection = /** @class */ (function (_super) {
149
149
  refType = typeInfo[1];
150
150
  }
151
151
  if (fieldType === "ref") {
152
- annotations_1.type(refType, context)(schemaType.prototype, field.name);
152
+ (0, annotations_1.type)(refType, context)(schemaType.prototype, field.name);
153
153
  }
154
154
  else {
155
- annotations_1.type((_a = {}, _a[fieldType] = refType, _a), context)(schemaType.prototype, field.name);
155
+ (0, annotations_1.type)((_a = {}, _a[fieldType] = refType, _a), context)(schemaType.prototype, field.name);
156
156
  }
157
157
  }
158
158
  else {
159
- annotations_1.type(field.type, context)(schemaType.prototype, field.name);
159
+ (0, annotations_1.type)(field.type, context)(schemaType.prototype, field.name);
160
160
  }
161
161
  });
162
162
  });
@@ -171,16 +171,16 @@ var Reflection = /** @class */ (function (_super) {
171
171
  if (typeof (fieldType) !== "string") {
172
172
  rootInstance[fieldName] = (typeof (fieldType) === "function")
173
173
  ? new fieldType() // is a schema reference
174
- : new (types_1.getType(Object.keys(fieldType)[0])).constructor(); // is a "collection"
174
+ : new ((0, types_1.getType)(Object.keys(fieldType)[0])).constructor(); // is a "collection"
175
175
  }
176
176
  }
177
177
  return rootInstance;
178
178
  };
179
179
  __decorate([
180
- annotations_1.type([ReflectionType], reflectionContext)
180
+ (0, annotations_1.type)([ReflectionType], reflectionContext)
181
181
  ], Reflection.prototype, "types", void 0);
182
182
  __decorate([
183
- annotations_1.type("number", reflectionContext)
183
+ (0, annotations_1.type)("number", reflectionContext)
184
184
  ], Reflection.prototype, "rootType", void 0);
185
185
  return Reflection;
186
186
  }(Schema_1.Schema));
@@ -1 +1 @@
1
- {"version":3,"file":"Reflection.js","sourceRoot":"","sources":["../src/Reflection.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA,6CAA6E;AAC7E,mCAAkC;AAClC,mDAAkD;AAClD,iCAAkC;AAGlC,IAAM,iBAAiB,GAAG,IAAI,qBAAO,EAAE,CAAC;AAExC;;GAEG;AACH;IAAqC,mCAAM;IAA3C;;IASA,CAAC;IAPG;QADC,kBAAI,CAAC,QAAQ,EAAE,iBAAiB,CAAC;iDACrB;IAGb;QADC,kBAAI,CAAC,QAAQ,EAAE,iBAAiB,CAAC;iDACrB;IAGb;QADC,kBAAI,CAAC,QAAQ,EAAE,iBAAiB,CAAC;2DACX;IAC3B,sBAAC;CAAA,AATD,CAAqC,eAAM,GAS1C;AATY,0CAAe;AAW5B;IAAoC,kCAAM;IAA1C;QAAA,qEAMC;QADG,YAAM,GAAiC,IAAI,yBAAW,EAAmB,CAAC;;IAC9E,CAAC;IAJG;QADC,kBAAI,CAAC,QAAQ,EAAE,iBAAiB,CAAC;8CACvB;IAGX;QADC,kBAAI,CAAC,CAAE,eAAe,CAAE,EAAE,iBAAiB,CAAC;kDAC6B;IAC9E,qBAAC;CAAA,AAND,CAAoC,eAAM,GAMzC;AANY,wCAAc;AAQ3B;IAAgC,8BAAM;IAAtC;QAAA,qEAgIC;QA9HG,WAAK,GAAgC,IAAI,yBAAW,EAAkB,CAAC;;IA8H3E,CAAC;IAzHU,iBAAM,GAAb,UAAe,QAAgB;QAC3B,IAAM,cAAc,GAAG,QAAQ,CAAC,WAA4B,CAAC;QAE7D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,CAAC;YAEzD,IAAI,OAAM,CAAC,SAAS,CAAC,KAAK,QAAQ,EAAE;gBAChC,YAAY,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,UAAU,CAAC;oBACzD,CAAC,CAAC,IAAK,SAAiB,EAAE,CAAC,wBAAwB;oBACnD,CAAC,CAAC,IAAI,CAAC,eAAO,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC,oBAAoB;aACrF;SACJ;QAED,OAAO,YAAY,CAAC;IACxB,CAAC;IA7HD;QADC,kBAAI,CAAC,CAAE,cAAc,CAAE,EAAE,iBAAiB,CAAC;6CAC2B;IAGvE;QADC,kBAAI,CAAC,QAAQ,EAAE,iBAAiB,CAAC;gDACjB;IA2HrB,iBAAC;CAAA,AAhID,CAAgC,eAAM,GAgIrC;AAhIY,gCAAU","sourcesContent":["import { type, PrimitiveType, Context, DefinitionType } from \"./annotations\";\nimport { Schema } from \"./Schema\";\nimport { ArraySchema } from \"./types/ArraySchema\";\nimport { getType } from \"./types\";\nimport { Iterator } from \"./encoding/decode\";\n\nconst reflectionContext = new Context();\n\n/**\n * Reflection\n */\nexport class ReflectionField extends Schema {\n @type(\"string\", reflectionContext)\n name: string;\n\n @type(\"string\", reflectionContext)\n type: string;\n\n @type(\"number\", reflectionContext)\n referencedType: number;\n}\n\nexport class ReflectionType extends Schema {\n @type(\"number\", reflectionContext)\n id: number;\n\n @type([ ReflectionField ], reflectionContext)\n fields: ArraySchema<ReflectionField> = new ArraySchema<ReflectionField>();\n}\n\nexport class Reflection extends Schema {\n @type([ ReflectionType ], reflectionContext)\n types: ArraySchema<ReflectionType> = new ArraySchema<ReflectionType>();\n\n @type(\"number\", reflectionContext)\n rootType: number;\n\n static encode (instance: Schema) {\n const rootSchemaType = instance.constructor as typeof Schema;\n\n const reflection = new Reflection();\n reflection.rootType = rootSchemaType._typeid;\n\n const buildType = (currentType: ReflectionType, schema: any) => {\n for (let fieldName in schema) {\n const field = new ReflectionField();\n field.name = fieldName;\n\n let fieldType: string;\n\n if (typeof (schema[fieldName]) === \"string\") {\n fieldType = schema[fieldName];\n\n } else {\n const type = schema[fieldName];\n let childTypeSchema: typeof Schema;\n\n //\n // TODO: refactor below.\n //\n if (Schema.is(type)) {\n fieldType = \"ref\";\n childTypeSchema = schema[fieldName];\n\n } else {\n fieldType = Object.keys(type)[0];\n\n if (typeof(type[fieldType]) === \"string\") {\n fieldType += \":\" + type[fieldType]; // array:string\n\n } else {\n childTypeSchema = type[fieldType];\n }\n }\n\n field.referencedType = (childTypeSchema)\n ? childTypeSchema._typeid\n : -1;\n }\n\n field.type = fieldType;\n currentType.fields.push(field);\n }\n\n reflection.types.push(currentType);\n }\n\n const types = rootSchemaType._context.types;\n for (let typeid in types) {\n const type = new ReflectionType();\n type.id = Number(typeid);\n buildType(type, types[typeid]._definition.schema);\n }\n\n return reflection.encodeAll();\n }\n\n static decode<T extends Schema = Schema>(bytes: number[], it?: Iterator): T {\n const context = new Context();\n\n const reflection = new Reflection();\n reflection.decode(bytes, it);\n\n const schemaTypes = reflection.types.reduce((types, reflectionType) => {\n const schema: typeof Schema = class _ extends Schema {};\n const typeid = reflectionType.id;\n types[typeid] = schema\n context.add(schema, typeid);\n return types;\n }, {});\n\n reflection.types.forEach((reflectionType) => {\n const schemaType = schemaTypes[reflectionType.id];\n\n reflectionType.fields.forEach(field => {\n if (field.referencedType !== undefined) {\n let fieldType = field.type;\n let refType = schemaTypes[field.referencedType];\n\n // map or array of primitive type (-1)\n if (!refType) {\n const typeInfo = field.type.split(\":\");\n fieldType = typeInfo[0];\n refType = typeInfo[1];\n }\n\n if (fieldType === \"ref\") {\n type(refType, context)(schemaType.prototype, field.name);\n\n } else {\n type({ [fieldType]: refType } as DefinitionType, context)(schemaType.prototype, field.name);\n }\n\n } else {\n type(field.type as PrimitiveType, context)(schemaType.prototype, field.name);\n }\n });\n })\n\n const rootType: any = schemaTypes[reflection.rootType];\n const rootInstance = new rootType();\n\n /**\n * auto-initialize referenced types on root type\n * to allow registering listeners immediatelly on client-side\n */\n for (let fieldName in rootType._definition.schema) {\n const fieldType = rootType._definition.schema[fieldName];\n\n if (typeof(fieldType) !== \"string\") {\n rootInstance[fieldName] = (typeof (fieldType) === \"function\")\n ? new (fieldType as any)() // is a schema reference\n : new (getType(Object.keys(fieldType)[0])).constructor(); // is a \"collection\"\n }\n }\n\n return rootInstance;\n }\n}"]}
1
+ {"version":3,"file":"Reflection.js","sourceRoot":"","sources":["../src/Reflection.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA,6CAA6E;AAC7E,mCAAkC;AAClC,mDAAkD;AAClD,iCAAkC;AAGlC,IAAM,iBAAiB,GAAG,IAAI,qBAAO,EAAE,CAAC;AAExC;;GAEG;AACH;IAAqC,mCAAM;IAA3C;;IASA,CAAC;IAPG;QADC,IAAA,kBAAI,EAAC,QAAQ,EAAE,iBAAiB,CAAC;iDACrB;IAGb;QADC,IAAA,kBAAI,EAAC,QAAQ,EAAE,iBAAiB,CAAC;iDACrB;IAGb;QADC,IAAA,kBAAI,EAAC,QAAQ,EAAE,iBAAiB,CAAC;2DACX;IAC3B,sBAAC;CAAA,AATD,CAAqC,eAAM,GAS1C;AATY,0CAAe;AAW5B;IAAoC,kCAAM;IAA1C;QAAA,qEAMC;QADG,YAAM,GAAiC,IAAI,yBAAW,EAAmB,CAAC;;IAC9E,CAAC;IAJG;QADC,IAAA,kBAAI,EAAC,QAAQ,EAAE,iBAAiB,CAAC;8CACvB;IAGX;QADC,IAAA,kBAAI,EAAC,CAAE,eAAe,CAAE,EAAE,iBAAiB,CAAC;kDAC6B;IAC9E,qBAAC;CAAA,AAND,CAAoC,eAAM,GAMzC;AANY,wCAAc;AAQ3B;IAAgC,8BAAM;IAAtC;QAAA,qEAgIC;QA9HG,WAAK,GAAgC,IAAI,yBAAW,EAAkB,CAAC;;IA8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AS,IAAI,QAAQ,CAAC,WAAW,CAAC,MAAM,EAAE;YAC/C,IAAM,SAAS,GAAG,QAAQ,CAAC,WAAW,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;YAEzD,IAAI,OAAM,CAAC,SAAS,CAAC,KAAK,QAAQ,EAAE;gBAChC,YAAY,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,UAAU,CAAC;oBACzD,CAAC,CAAC,IAAK,SAAiB,EAAE,CAAC,wBAAwB;oBACnD,CAAC,CAAC,IAAI,CAAC,IAAA,eAAO,EAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC,oBAAoB;aACrF;SACJ;QAED,OAAO,YAAY,CAAC;IACxB,CAAC;IA7HD;QADC,IAAA,kBAAI,EAAC,CAAE,cAAc,CAAE,EAAE,iBAAiB,CAAC;6CAC2B;IAGvE;QADC,IAAA,kBAAI,EAAC,QAAQ,EAAE,iBAAiB,CAAC;gDACjB;IA2HrB,iBAAC;CAAA,AAhID,CAAgC,eAAM,GAgIrC;AAhIY,gCAAU","sourcesContent":["import { type, PrimitiveType, Context, DefinitionType } from \"./annotations\";\nimport { Schema } from \"./Schema\";\nimport { ArraySchema } from \"./types/ArraySchema\";\nimport { getType } from \"./types\";\nimport { Iterator } from \"./encoding/decode\";\n\nconst reflectionContext = new Context();\n\n/**\n * Reflection\n */\nexport class ReflectionField extends Schema {\n @type(\"string\", reflectionContext)\n name: string;\n\n @type(\"string\", reflectionContext)\n type: string;\n\n @type(\"number\", reflectionContext)\n referencedType: number;\n}\n\nexport class ReflectionType extends Schema {\n @type(\"number\", reflectionContext)\n id: number;\n\n @type([ ReflectionField ], reflectionContext)\n fields: ArraySchema<ReflectionField> = new ArraySchema<ReflectionField>();\n}\n\nexport class Reflection extends Schema {\n @type([ ReflectionType ], reflectionContext)\n types: ArraySchema<ReflectionType> = new ArraySchema<ReflectionType>();\n\n @type(\"number\", reflectionContext)\n rootType: number;\n\n static encode (instance: Schema) {\n const rootSchemaType = instance.constructor as typeof Schema;\n\n const reflection = new Reflection();\n reflection.rootType = rootSchemaType._typeid;\n\n const buildType = (currentType: ReflectionType, schema: any) => {\n for (let fieldName in schema) {\n const field = new ReflectionField();\n field.name = fieldName;\n\n let fieldType: string;\n\n if (typeof (schema[fieldName]) === \"string\") {\n fieldType = schema[fieldName];\n\n } else {\n const type = schema[fieldName];\n let childTypeSchema: typeof Schema;\n\n //\n // TODO: refactor below.\n //\n if (Schema.is(type)) {\n fieldType = \"ref\";\n childTypeSchema = schema[fieldName];\n\n } else {\n fieldType = Object.keys(type)[0];\n\n if (typeof(type[fieldType]) === \"string\") {\n fieldType += \":\" + type[fieldType]; // array:string\n\n } else {\n childTypeSchema = type[fieldType];\n }\n }\n\n field.referencedType = (childTypeSchema)\n ? childTypeSchema._typeid\n : -1;\n }\n\n field.type = fieldType;\n currentType.fields.push(field);\n }\n\n reflection.types.push(currentType);\n }\n\n const types = rootSchemaType._context.types;\n for (let typeid in types) {\n const type = new ReflectionType();\n type.id = Number(typeid);\n buildType(type, types[typeid]._definition.schema);\n }\n\n return reflection.encodeAll();\n }\n\n static decode<T extends Schema = Schema>(bytes: number[], it?: Iterator): T {\n const context = new Context();\n\n const reflection = new Reflection();\n reflection.decode(bytes, it);\n\n const schemaTypes = reflection.types.reduce((types, reflectionType) => {\n const schema: typeof Schema = class _ extends Schema {};\n const typeid = reflectionType.id;\n types[typeid] = schema\n context.add(schema, typeid);\n return types;\n }, {});\n\n reflection.types.forEach((reflectionType) => {\n const schemaType = schemaTypes[reflectionType.id];\n\n reflectionType.fields.forEach(field => {\n if (field.referencedType !== undefined) {\n let fieldType = field.type;\n let refType = schemaTypes[field.referencedType];\n\n // map or array of primitive type (-1)\n if (!refType) {\n const typeInfo = field.type.split(\":\");\n fieldType = typeInfo[0];\n refType = typeInfo[1];\n }\n\n if (fieldType === \"ref\") {\n type(refType, context)(schemaType.prototype, field.name);\n\n } else {\n type({ [fieldType]: refType } as DefinitionType, context)(schemaType.prototype, field.name);\n }\n\n } else {\n type(field.type as PrimitiveType, context)(schemaType.prototype, field.name);\n }\n });\n })\n\n const rootType: any = schemaTypes[reflection.rootType];\n const rootInstance = new rootType();\n\n /**\n * auto-initialize referenced types on root type\n * to allow registering listeners immediatelly on client-side\n */\n for (let fieldName in rootType._definition.schema) {\n const fieldType = rootType._definition.schema[fieldName];\n\n if (typeof(fieldType) !== \"string\") {\n rootInstance[fieldName] = (typeof (fieldType) === \"function\")\n ? new (fieldType as any)() // is a schema reference\n : new (getType(Object.keys(fieldType)[0])).constructor(); // is a \"collection\"\n }\n }\n\n return rootInstance;\n }\n}"]}
package/lib/Schema.js CHANGED
@@ -68,7 +68,7 @@ function assertType(value, type, klass, field) {
68
68
  case "float64":
69
69
  typeofTarget = "number";
70
70
  if (isNaN(value)) {
71
- console.log("trying to encode \"NaN\" in " + klass.constructor.name + "#" + field);
71
+ console.log("trying to encode \"NaN\" in ".concat(klass.constructor.name, "#").concat(field));
72
72
  }
73
73
  break;
74
74
  case "string":
@@ -80,13 +80,13 @@ function assertType(value, type, klass, field) {
80
80
  return;
81
81
  }
82
82
  if (typeof (value) !== typeofTarget && (!allowNull || (allowNull && value !== null))) {
83
- var foundValue = "'" + JSON.stringify(value) + "'" + ((value && value.constructor && " (" + value.constructor.name + ")") || '');
84
- throw new EncodeSchemaError("a '" + typeofTarget + "' was expected, but " + foundValue + " was provided in " + klass.constructor.name + "#" + field);
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));
85
85
  }
86
86
  }
87
87
  function assertInstanceType(value, type, klass, field) {
88
88
  if (!(value instanceof type)) {
89
- throw new EncodeSchemaError("a '" + type.name + "' was expected, but '" + value.constructor.name + "' was provided in " + klass.constructor.name + "#" + field);
89
+ throw new EncodeSchemaError("a '".concat(type.name, "' was expected, but '").concat(value.constructor.name, "' was provided in ").concat(klass.constructor.name, "#").concat(field));
90
90
  }
91
91
  }
92
92
  function encodePrimitiveType(type, bytes, value, klass, field) {
@@ -96,7 +96,7 @@ function encodePrimitiveType(type, bytes, value, klass, field) {
96
96
  encodeFunc(bytes, value);
97
97
  }
98
98
  else {
99
- throw new EncodeSchemaError("a '" + type + "' was expected, but " + value + " was provided in " + klass.constructor.name + "#" + field);
99
+ throw new EncodeSchemaError("a '".concat(type, "' was expected, but ").concat(value, " was provided in ").concat(klass.constructor.name, "#").concat(field));
100
100
  }
101
101
  }
102
102
  function decodePrimitiveType(type, bytes, it) {
@@ -182,7 +182,7 @@ var Schema = /** @class */ (function () {
182
182
  // Trying to access a reference that haven't been decoded yet.
183
183
  //
184
184
  if (!nextRef) {
185
- throw new Error("\"refId\" not found: " + refId);
185
+ throw new Error("\"refId\" not found: ".concat(refId));
186
186
  }
187
187
  ref = nextRef;
188
188
  // create empty list of changes for this refId.
@@ -228,7 +228,7 @@ var Schema = /** @class */ (function () {
228
228
  }
229
229
  }
230
230
  else {
231
- previousValue = ref["_" + fieldName];
231
+ previousValue = ref["_".concat(fieldName)];
232
232
  }
233
233
  //
234
234
  // Delete operations
@@ -295,7 +295,7 @@ var Schema = /** @class */ (function () {
295
295
  value = decodePrimitiveType(type, bytes, it);
296
296
  }
297
297
  else {
298
- var typeDef = types_1.getType(Object.keys(type)[0]);
298
+ var typeDef = (0, types_1.getType)(Object.keys(type)[0]);
299
299
  var refId_2 = decode.number(bytes, it);
300
300
  var valueRef = ($root.refs.has(refId_2))
301
301
  ? previousValue || $root.refs.get(refId_2)
@@ -511,11 +511,11 @@ var Schema = /** @class */ (function () {
511
511
  //
512
512
  // Custom type (MapSchema, ArraySchema, etc)
513
513
  //
514
- var definition = types_1.getType(Object.keys(type)[0]);
514
+ var definition = (0, types_1.getType)(Object.keys(type)[0]);
515
515
  //
516
516
  // ensure a ArraySchema has been provided
517
517
  //
518
- assertInstanceType(ref["_" + field], definition.constructor, ref, field);
518
+ assertInstanceType(ref["_".concat(field)], definition.constructor, ref, field);
519
519
  //
520
520
  // Encode refId for this instance.
521
521
  // The actual instance is going to be encoded on next `changeTree` iteration.
@@ -719,11 +719,12 @@ var Schema = /** @class */ (function () {
719
719
  return filteredBytes;
720
720
  };
721
721
  Schema.prototype.clone = function () {
722
+ var _a;
722
723
  var cloned = new (this.constructor);
723
724
  var schema = this._definition.schema;
724
725
  for (var field in schema) {
725
726
  if (typeof (this[field]) === "object" &&
726
- typeof (this[field].clone) === "function") {
727
+ typeof ((_a = this[field]) === null || _a === void 0 ? void 0 : _a.clone) === "function") {
727
728
  // deep clone
728
729
  cloned[field] = this[field].clone();
729
730
  }
@@ -756,7 +757,7 @@ var Schema = /** @class */ (function () {
756
757
  if (!deprecated[field] && this[field] !== null && typeof (this[field]) !== "undefined") {
757
758
  obj[field] = (typeof (this[field]['toJSON']) === "function")
758
759
  ? this[field]['toJSON']()
759
- : this["_" + field];
760
+ : this["_".concat(field)];
760
761
  }
761
762
  }
762
763
  return obj;
@@ -799,7 +800,7 @@ var Schema = /** @class */ (function () {
799
800
  if (ref instanceof Schema) {
800
801
  var schema = ref._definition.schema;
801
802
  for (var fieldName in schema) {
802
- var _field = "_" + fieldName;
803
+ var _field = "_".concat(fieldName);
803
804
  var value = ref[_field];
804
805
  if (value !== undefined) {
805
806
  changes.push({