@colyseus/schema 1.0.35 → 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 (74) hide show
  1. package/build/cjs/index.js +34 -33
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/index.mjs +112 -83
  4. package/build/esm/index.mjs.map +1 -1
  5. package/build/umd/index.js +36 -35
  6. package/lib/Reflection.js +11 -11
  7. package/lib/Reflection.js.map +1 -1
  8. package/lib/Schema.js +14 -13
  9. package/lib/Schema.js.map +1 -1
  10. package/lib/annotations.js +17 -13
  11. package/lib/annotations.js.map +1 -1
  12. package/lib/changes/ChangeTree.js +2 -2
  13. package/lib/changes/ChangeTree.js.map +1 -1
  14. package/lib/codegen/api.js +1 -1
  15. package/lib/codegen/api.js.map +1 -1
  16. package/lib/codegen/argv.d.ts +1 -1
  17. package/lib/codegen/cli.js +5 -5
  18. package/lib/codegen/cli.js.map +1 -1
  19. package/lib/codegen/languages/cpp.js +38 -38
  20. package/lib/codegen/languages/cpp.js.map +1 -1
  21. package/lib/codegen/languages/csharp.js +25 -21
  22. package/lib/codegen/languages/csharp.js.map +1 -1
  23. package/lib/codegen/languages/haxe.js +13 -13
  24. package/lib/codegen/languages/haxe.js.map +1 -1
  25. package/lib/codegen/languages/java.js +11 -11
  26. package/lib/codegen/languages/java.js.map +1 -1
  27. package/lib/codegen/languages/js.js +16 -16
  28. package/lib/codegen/languages/js.js.map +1 -1
  29. package/lib/codegen/languages/lua.js +12 -12
  30. package/lib/codegen/languages/lua.js.map +1 -1
  31. package/lib/codegen/languages/ts.js +37 -33
  32. package/lib/codegen/languages/ts.js.map +1 -1
  33. package/lib/codegen/parser.js +3 -3
  34. package/lib/codegen/parser.js.map +1 -1
  35. package/lib/codegen/types.js +2 -2
  36. package/lib/codegen/types.js.map +1 -1
  37. package/lib/events/EventEmitter.js +10 -6
  38. package/lib/events/EventEmitter.js.map +1 -1
  39. package/lib/index.js +4 -4
  40. package/lib/index.js.map +1 -1
  41. package/lib/types/ArraySchema.js +12 -8
  42. package/lib/types/ArraySchema.js.map +1 -1
  43. package/lib/types/MapSchema.js +1 -1
  44. package/lib/types/MapSchema.js.map +1 -1
  45. package/package.json +2 -1
  46. package/src/Reflection.ts +159 -0
  47. package/src/Schema.ts +1084 -0
  48. package/src/annotations.ts +357 -0
  49. package/src/changes/ChangeTree.ts +373 -0
  50. package/src/codegen/api.ts +46 -0
  51. package/src/codegen/argv.ts +40 -0
  52. package/src/codegen/cli.ts +65 -0
  53. package/src/codegen/languages/cpp.ts +297 -0
  54. package/src/codegen/languages/csharp.ts +119 -0
  55. package/src/codegen/languages/haxe.ts +110 -0
  56. package/src/codegen/languages/java.ts +115 -0
  57. package/src/codegen/languages/js.ts +115 -0
  58. package/src/codegen/languages/lua.ts +125 -0
  59. package/src/codegen/languages/ts.ts +129 -0
  60. package/src/codegen/parser.ts +251 -0
  61. package/src/codegen/types.ts +164 -0
  62. package/src/encoding/decode.ts +278 -0
  63. package/src/encoding/encode.ts +283 -0
  64. package/src/events/EventEmitter.ts +32 -0
  65. package/src/filters/index.ts +23 -0
  66. package/src/index.ts +59 -0
  67. package/src/spec.ts +49 -0
  68. package/src/types/ArraySchema.ts +608 -0
  69. package/src/types/CollectionSchema.ts +188 -0
  70. package/src/types/HelperTypes.ts +34 -0
  71. package/src/types/MapSchema.ts +255 -0
  72. package/src/types/SetSchema.ts +197 -0
  73. package/src/types/index.ts +19 -0
  74. package/src/utils.ts +28 -0
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- /*! *****************************************************************************
5
+ /******************************************************************************
6
6
  Copyright (c) Microsoft Corporation.
7
7
 
8
8
  Permission to use, copy, modify, and/or distribute this software for any
@@ -40,10 +40,14 @@ function __decorate(decorators, target, key, desc) {
40
40
  return c > 3 && r && Object.defineProperty(target, key, r), r;
41
41
  }
42
42
 
43
- function __spreadArray(to, from) {
44
- for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
45
- to[j] = from[i];
46
- return to;
43
+ function __spreadArray(to, from, pack) {
44
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
45
+ if (ar || !(i in from)) {
46
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
47
+ ar[i] = from[i];
48
+ }
49
+ }
50
+ return to.concat(ar || Array.prototype.slice.call(from));
47
51
  }
48
52
 
49
53
  // export const SWITCH_TO_STRUCTURE = 193; (easily collides with DELETE_AND_ADD + fieldIndex = 2)
@@ -280,7 +284,7 @@ var ChangeTree = /** @class */ (function () {
280
284
  ? fieldName
281
285
  : this.indexes[fieldName];
282
286
  if (index === undefined) {
283
- console.warn("@colyseus/schema " + this.ref.constructor.name + ": trying to delete non-existing index: " + fieldName + " (" + index + ")");
287
+ console.warn("@colyseus/schema ".concat(this.ref.constructor.name, ": trying to delete non-existing index: ").concat(fieldName, " (").concat(index, ")"));
284
288
  return;
285
289
  }
286
290
  var previousValue = this.getValue(index);
@@ -352,7 +356,7 @@ var ChangeTree = /** @class */ (function () {
352
356
  };
353
357
  ChangeTree.prototype.assertValidIndex = function (index, fieldName) {
354
358
  if (index === undefined) {
355
- throw new Error("ChangeTree: missing index for field \"" + fieldName + "\"");
359
+ throw new Error("ChangeTree: missing index for field \"".concat(fieldName, "\""));
356
360
  }
357
361
  };
358
362
  return ChangeTree;
@@ -545,7 +549,7 @@ var ArraySchema = /** @class */ (function () {
545
549
  for (var _i = 0; _i < arguments.length; _i++) {
546
550
  items[_i] = arguments[_i];
547
551
  }
548
- return new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], (_a = Array.from(this.$items.values())).concat.apply(_a, items))))();
552
+ return new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], (_a = Array.from(this.$items.values())).concat.apply(_a, items), false)))();
549
553
  };
550
554
  /**
551
555
  * Adds all the elements of an array separated by the specified separator string.
@@ -585,7 +589,7 @@ var ArraySchema = /** @class */ (function () {
585
589
  * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'.
586
590
  */
587
591
  ArraySchema.prototype.slice = function (start, end) {
588
- return new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], Array.from(this.$items.values()).slice(start, end))))();
592
+ return new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], Array.from(this.$items.values()).slice(start, end), false)))();
589
593
  };
590
594
  /**
591
595
  * Sorts an array.
@@ -614,10 +618,6 @@ var ArraySchema = /** @class */ (function () {
614
618
  */
615
619
  ArraySchema.prototype.splice = function (start, deleteCount) {
616
620
  if (deleteCount === void 0) { deleteCount = this.length - start; }
617
- var items = [];
618
- for (var _i = 2; _i < arguments.length; _i++) {
619
- items[_i - 2] = arguments[_i];
620
- }
621
621
  var indexes = Array.from(this.$items.keys());
622
622
  var removedItems = [];
623
623
  for (var i = start; i < start + deleteCount; i++) {
@@ -869,12 +869,12 @@ var ArraySchema = /** @class */ (function () {
869
869
  ArraySchema.prototype.clone = function (isDecoding) {
870
870
  var cloned;
871
871
  if (isDecoding) {
872
- cloned = new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], Array.from(this.$items.values()))))();
872
+ cloned = new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], Array.from(this.$items.values()), false)))();
873
873
  }
874
874
  else {
875
875
  cloned = new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], this.map(function (item) { return ((item['$changes'])
876
876
  ? item.clone()
877
- : item); }))))();
877
+ : item); }), false)))();
878
878
  }
879
879
  return cloned;
880
880
  };
@@ -946,7 +946,7 @@ var MapSchema = /** @class */ (function () {
946
946
  });
947
947
  MapSchema.prototype.set = function (key, value) {
948
948
  if (value === undefined || value === null) {
949
- throw new Error("MapSchema#set('" + key + "', " + value + "): trying to set " + value + " value on '" + key + "'.");
949
+ throw new Error("MapSchema#set('".concat(key, "', ").concat(value, "): trying to set ").concat(value, " value on '").concat(key, "'."));
950
950
  }
951
951
  // get "index" for this value.
952
952
  var hasIndex = typeof (this.$changes.indexes[key]) !== "undefined";
@@ -1140,7 +1140,7 @@ var SchemaDefinition = /** @class */ (function () {
1140
1140
  return true;
1141
1141
  }
1142
1142
  else {
1143
- console.warn("@filterChildren: field '" + field + "' can't have children. Ignoring filter.");
1143
+ console.warn("@filterChildren: field '".concat(field, "' can't have children. Ignoring filter."));
1144
1144
  }
1145
1145
  };
1146
1146
  SchemaDefinition.prototype.getChildrenFilter = function (field) {
@@ -1215,11 +1215,11 @@ function type(type, context) {
1215
1215
  // trying to define same property multiple times across inheritance.
1216
1216
  // https://github.com/colyseus/colyseus-unity3d/issues/131#issuecomment-814308572
1217
1217
  try {
1218
- throw new Error("@colyseus/schema: Duplicate '" + field + "' definition on '" + constructor.name + "'.\nCheck @type() annotation");
1218
+ throw new Error("@colyseus/schema: Duplicate '".concat(field, "' definition on '").concat(constructor.name, "'.\nCheck @type() annotation"));
1219
1219
  }
1220
1220
  catch (e) {
1221
1221
  var definitionAtLine = e.stack.split("\n")[4].trim();
1222
- throw new Error(e.message + " " + definitionAtLine);
1222
+ throw new Error("".concat(e.message, " ").concat(definitionAtLine));
1223
1223
  }
1224
1224
  }
1225
1225
  }
@@ -1234,7 +1234,7 @@ function type(type, context) {
1234
1234
  context.add(childType);
1235
1235
  }
1236
1236
  }
1237
- var fieldCached = "_" + field;
1237
+ var fieldCached = "_".concat(field);
1238
1238
  definition.descriptors[fieldCached] = {
1239
1239
  enumerable: false,
1240
1240
  configurable: false,
@@ -1256,7 +1256,7 @@ function type(type, context) {
1256
1256
  value !== null) {
1257
1257
  // automaticallty transform Array into ArraySchema
1258
1258
  if (isArray && !(value instanceof ArraySchema)) {
1259
- value = new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], value)))();
1259
+ value = new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], value, false)))();
1260
1260
  }
1261
1261
  // automaticallty transform Map into MapSchema
1262
1262
  if (isMap && !(value instanceof MapSchema)) {
@@ -1327,7 +1327,7 @@ function deprecated(throws, context) {
1327
1327
  definition.deprecated[field] = true;
1328
1328
  if (throws) {
1329
1329
  definition.descriptors[field] = {
1330
- get: function () { throw new Error(field + " is deprecated."); },
1330
+ get: function () { throw new Error("".concat(field, " is deprecated.")); },
1331
1331
  set: function (value) { },
1332
1332
  enumerable: false,
1333
1333
  configurable: true
@@ -2249,7 +2249,7 @@ function assertType(value, type, klass, field) {
2249
2249
  case "float64":
2250
2250
  typeofTarget = "number";
2251
2251
  if (isNaN(value)) {
2252
- console.log("trying to encode \"NaN\" in " + klass.constructor.name + "#" + field);
2252
+ console.log("trying to encode \"NaN\" in ".concat(klass.constructor.name, "#").concat(field));
2253
2253
  }
2254
2254
  break;
2255
2255
  case "string":
@@ -2261,13 +2261,13 @@ function assertType(value, type, klass, field) {
2261
2261
  return;
2262
2262
  }
2263
2263
  if (typeof (value) !== typeofTarget && (!allowNull || (allowNull && value !== null))) {
2264
- var foundValue = "'" + JSON.stringify(value) + "'" + ((value && value.constructor && " (" + value.constructor.name + ")") || '');
2265
- throw new EncodeSchemaError("a '" + typeofTarget + "' was expected, but " + foundValue + " was provided in " + klass.constructor.name + "#" + field);
2264
+ var foundValue = "'".concat(JSON.stringify(value), "'").concat((value && value.constructor && " (".concat(value.constructor.name, ")")) || '');
2265
+ throw new EncodeSchemaError("a '".concat(typeofTarget, "' was expected, but ").concat(foundValue, " was provided in ").concat(klass.constructor.name, "#").concat(field));
2266
2266
  }
2267
2267
  }
2268
2268
  function assertInstanceType(value, type, klass, field) {
2269
2269
  if (!(value instanceof type)) {
2270
- throw new EncodeSchemaError("a '" + type.name + "' was expected, but '" + value.constructor.name + "' was provided in " + klass.constructor.name + "#" + field);
2270
+ throw new EncodeSchemaError("a '".concat(type.name, "' was expected, but '").concat(value.constructor.name, "' was provided in ").concat(klass.constructor.name, "#").concat(field));
2271
2271
  }
2272
2272
  }
2273
2273
  function encodePrimitiveType(type, bytes, value, klass, field) {
@@ -2277,7 +2277,7 @@ function encodePrimitiveType(type, bytes, value, klass, field) {
2277
2277
  encodeFunc(bytes, value);
2278
2278
  }
2279
2279
  else {
2280
- throw new EncodeSchemaError("a '" + type + "' was expected, but " + value + " was provided in " + klass.constructor.name + "#" + field);
2280
+ throw new EncodeSchemaError("a '".concat(type, "' was expected, but ").concat(value, " was provided in ").concat(klass.constructor.name, "#").concat(field));
2281
2281
  }
2282
2282
  }
2283
2283
  function decodePrimitiveType(type, bytes, it) {
@@ -2363,7 +2363,7 @@ var Schema = /** @class */ (function () {
2363
2363
  // Trying to access a reference that haven't been decoded yet.
2364
2364
  //
2365
2365
  if (!nextRef) {
2366
- throw new Error("\"refId\" not found: " + refId);
2366
+ throw new Error("\"refId\" not found: ".concat(refId));
2367
2367
  }
2368
2368
  ref = nextRef;
2369
2369
  // create empty list of changes for this refId.
@@ -2409,7 +2409,7 @@ var Schema = /** @class */ (function () {
2409
2409
  }
2410
2410
  }
2411
2411
  else {
2412
- previousValue = ref["_" + fieldName];
2412
+ previousValue = ref["_".concat(fieldName)];
2413
2413
  }
2414
2414
  //
2415
2415
  // Delete operations
@@ -2691,7 +2691,7 @@ var Schema = /** @class */ (function () {
2691
2691
  //
2692
2692
  // ensure a ArraySchema has been provided
2693
2693
  //
2694
- assertInstanceType(ref["_" + field], definition.constructor, ref, field);
2694
+ assertInstanceType(ref["_".concat(field)], definition.constructor, ref, field);
2695
2695
  //
2696
2696
  // Encode refId for this instance.
2697
2697
  // The actual instance is going to be encoded on next `changeTree` iteration.
@@ -2893,11 +2893,12 @@ var Schema = /** @class */ (function () {
2893
2893
  return filteredBytes;
2894
2894
  };
2895
2895
  Schema.prototype.clone = function () {
2896
+ var _a;
2896
2897
  var cloned = new (this.constructor);
2897
2898
  var schema = this._definition.schema;
2898
2899
  for (var field in schema) {
2899
2900
  if (typeof (this[field]) === "object" &&
2900
- typeof (this[field].clone) === "function") {
2901
+ typeof ((_a = this[field]) === null || _a === void 0 ? void 0 : _a.clone) === "function") {
2901
2902
  // deep clone
2902
2903
  cloned[field] = this[field].clone();
2903
2904
  }
@@ -2930,7 +2931,7 @@ var Schema = /** @class */ (function () {
2930
2931
  if (!deprecated[field] && this[field] !== null && typeof (this[field]) !== "undefined") {
2931
2932
  obj[field] = (typeof (this[field]['toJSON']) === "function")
2932
2933
  ? this[field]['toJSON']()
2933
- : this["_" + field];
2934
+ : this["_".concat(field)];
2934
2935
  }
2935
2936
  }
2936
2937
  return obj;
@@ -2973,7 +2974,7 @@ var Schema = /** @class */ (function () {
2973
2974
  if (ref instanceof Schema) {
2974
2975
  var schema = ref._definition.schema;
2975
2976
  for (var fieldName in schema) {
2976
- var _field = "_" + fieldName;
2977
+ var _field = "_".concat(fieldName);
2977
2978
  var value = ref[_field];
2978
2979
  if (value !== undefined) {
2979
2980
  changes.push({