@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
@@ -38,13 +38,19 @@ var OPERATION;
38
38
  // }
39
39
 
40
40
  class ChangeTree {
41
+ ref;
42
+ refId;
43
+ root;
44
+ parent;
45
+ parentIndex;
46
+ indexes;
47
+ changed = false;
48
+ changes = new Map();
49
+ allChanges = new Set();
50
+ // cached indexes for filtering
51
+ caches = {};
52
+ currentCustomOperation = 0;
41
53
  constructor(ref, parent, root) {
42
- this.changed = false;
43
- this.changes = new Map();
44
- this.allChanges = new Set();
45
- // cached indexes for filtering
46
- this.caches = {};
47
- this.currentCustomOperation = 0;
48
54
  this.ref = ref;
49
55
  this.setParent(parent, root);
50
56
  }
@@ -334,13 +340,14 @@ function getArrayProxy(value) {
334
340
  return value;
335
341
  }
336
342
  class ArraySchema {
337
- constructor(...items) {
338
- this.$changes = new ChangeTree(this);
339
- this.$items = new Map();
340
- this.$indexes = new Map();
341
- this.$refId = 0;
342
- this.push.apply(this, items);
343
- }
343
+ $changes = new ChangeTree(this);
344
+ $items = new Map();
345
+ $indexes = new Map();
346
+ $refId = 0;
347
+ //
348
+ // Decoding callbacks
349
+ //
350
+ $callbacks;
344
351
  onAdd(callback, triggerAll = true) {
345
352
  return addCallback((this.$callbacks || (this.$callbacks = [])), OPERATION.ADD, callback, (triggerAll)
346
353
  ? this.$items
@@ -355,6 +362,9 @@ class ArraySchema {
355
362
  // type format: { array: "string" }
356
363
  (type['array'] !== undefined));
357
364
  }
365
+ constructor(...items) {
366
+ this.push.apply(this, items);
367
+ }
358
368
  set length(value) {
359
369
  if (value === 0) {
360
370
  this.clear();
@@ -597,7 +607,7 @@ class ArraySchema {
597
607
  * @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.
598
608
  */
599
609
  reduce(callbackfn, initialValue) {
600
- return Array.from(this.$items.values()).reduce(callbackfn, initialValue);
610
+ return Array.prototype.reduce.apply(Array.from(this.$items.values()), arguments);
601
611
  }
602
612
  /**
603
613
  * 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.
@@ -605,7 +615,7 @@ class ArraySchema {
605
615
  * @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.
606
616
  */
607
617
  reduceRight(callbackfn, initialValue) {
608
- return Array.from(this.$items.values()).reduceRight(callbackfn, initialValue);
618
+ return Array.prototype.reduceRight.apply(Array.from(this.$items.values()), arguments);
609
619
  }
610
620
  /**
611
621
  * Returns the value of the first element in the array where predicate is true, and undefined
@@ -673,9 +683,10 @@ class ArraySchema {
673
683
  [Symbol.iterator]() {
674
684
  return Array.from(this.$items.values())[Symbol.iterator]();
675
685
  }
676
- [Symbol.unscopables]() {
677
- return this.$items[Symbol.unscopables]();
678
- }
686
+ // WORKAROUND for compatibility
687
+ // - TypeScript 4 defines @@unscopables as a function
688
+ // - TypeScript 5 defines @@unscopables as an object
689
+ [Symbol.unscopables];
679
690
  /**
680
691
  * Returns an iterable of key, value pairs for every entry in the array
681
692
  */
@@ -800,13 +811,28 @@ function getMapProxy(value) {
800
811
  return value;
801
812
  }
802
813
  class MapSchema {
814
+ $changes = new ChangeTree(this);
815
+ $items = new Map();
816
+ $indexes = new Map();
817
+ $refId = 0;
818
+ //
819
+ // Decoding callbacks
820
+ //
821
+ $callbacks;
822
+ onAdd(callback, triggerAll = true) {
823
+ return addCallback((this.$callbacks || (this.$callbacks = [])), OPERATION.ADD, callback, (triggerAll)
824
+ ? this.$items
825
+ : undefined);
826
+ }
827
+ onRemove(callback) { return addCallback(this.$callbacks || (this.$callbacks = []), OPERATION.DELETE, callback); }
828
+ onChange(callback) { return addCallback(this.$callbacks || (this.$callbacks = []), OPERATION.REPLACE, callback); }
829
+ static is(type) {
830
+ return type['map'] !== undefined;
831
+ }
803
832
  constructor(initialValues) {
804
- this.$changes = new ChangeTree(this);
805
- this.$items = new Map();
806
- this.$indexes = new Map();
807
- this.$refId = 0;
808
833
  if (initialValues) {
809
- if (initialValues instanceof Map) {
834
+ if (initialValues instanceof Map ||
835
+ initialValues instanceof MapSchema) {
810
836
  initialValues.forEach((v, k) => this.set(k, v));
811
837
  }
812
838
  else {
@@ -816,16 +842,6 @@ class MapSchema {
816
842
  }
817
843
  }
818
844
  }
819
- onAdd(callback, triggerAll = true) {
820
- return addCallback((this.$callbacks || (this.$callbacks = [])), OPERATION.ADD, callback, (triggerAll)
821
- ? this.$items
822
- : undefined);
823
- }
824
- onRemove(callback) { return addCallback(this.$callbacks || (this.$callbacks = []), OPERATION.DELETE, callback); }
825
- onChange(callback) { return addCallback(this.$callbacks || (this.$callbacks = []), OPERATION.REPLACE, callback); }
826
- static is(type) {
827
- return type['map'] !== undefined;
828
- }
829
845
  /** Iterator */
830
846
  [Symbol.iterator]() { return this.$items[Symbol.iterator](); }
831
847
  get [Symbol.toStringTag]() { return this.$items[Symbol.toStringTag]; }
@@ -971,15 +987,17 @@ function getType(identifier) {
971
987
  }
972
988
 
973
989
  class SchemaDefinition {
974
- constructor() {
975
- //
976
- // TODO: use a "field" structure combining all these properties per-field.
977
- //
978
- this.indexes = {};
979
- this.fieldsByIndex = {};
980
- this.deprecated = {};
981
- this.descriptors = {};
982
- }
990
+ schema;
991
+ //
992
+ // TODO: use a "field" structure combining all these properties per-field.
993
+ //
994
+ indexes = {};
995
+ fieldsByIndex = {};
996
+ filters;
997
+ indexesWithFilters;
998
+ childFilters; // childFilters are used on Map, Array, Set items.
999
+ deprecated = {};
1000
+ descriptors = {};
983
1001
  static create(parent) {
984
1002
  const definition = new SchemaDefinition();
985
1003
  // support inheritance
@@ -1035,11 +1053,9 @@ function hasFilter(klass) {
1035
1053
  return klass._context && klass._context.useFilters;
1036
1054
  }
1037
1055
  class Context {
1038
- constructor() {
1039
- this.types = {};
1040
- this.schemas = new Map();
1041
- this.useFilters = false;
1042
- }
1056
+ types = {};
1057
+ schemas = new Map();
1058
+ useFilters = false;
1043
1059
  has(schema) {
1044
1060
  return this.schemas.has(schema);
1045
1061
  }
@@ -1183,7 +1199,7 @@ function type(type, options = {}) {
1183
1199
  value['$changes'].setParent(this, this.$changes.root, this._definition.indexes[field]);
1184
1200
  }
1185
1201
  }
1186
- else {
1202
+ else if (this[fieldCached]) {
1187
1203
  //
1188
1204
  // Setting a field to `null` or `undefined` will delete it.
1189
1205
  //
@@ -1769,15 +1785,14 @@ var decode = /*#__PURE__*/Object.freeze({
1769
1785
  });
1770
1786
 
1771
1787
  class CollectionSchema {
1772
- constructor(initialValues) {
1773
- this.$changes = new ChangeTree(this);
1774
- this.$items = new Map();
1775
- this.$indexes = new Map();
1776
- this.$refId = 0;
1777
- if (initialValues) {
1778
- initialValues.forEach((v) => this.add(v));
1779
- }
1780
- }
1788
+ $changes = new ChangeTree(this);
1789
+ $items = new Map();
1790
+ $indexes = new Map();
1791
+ $refId = 0;
1792
+ //
1793
+ // Decoding callbacks
1794
+ //
1795
+ $callbacks;
1781
1796
  onAdd(callback, triggerAll = true) {
1782
1797
  return addCallback((this.$callbacks || (this.$callbacks = [])), OPERATION.ADD, callback, (triggerAll)
1783
1798
  ? this.$items
@@ -1788,6 +1803,11 @@ class CollectionSchema {
1788
1803
  static is(type) {
1789
1804
  return type['collection'] !== undefined;
1790
1805
  }
1806
+ constructor(initialValues) {
1807
+ if (initialValues) {
1808
+ initialValues.forEach((v) => this.add(v));
1809
+ }
1810
+ }
1791
1811
  add(value) {
1792
1812
  // set "index" for reference.
1793
1813
  const index = this.$refId++;
@@ -1912,15 +1932,14 @@ class CollectionSchema {
1912
1932
  }
1913
1933
 
1914
1934
  class SetSchema {
1915
- constructor(initialValues) {
1916
- this.$changes = new ChangeTree(this);
1917
- this.$items = new Map();
1918
- this.$indexes = new Map();
1919
- this.$refId = 0;
1920
- if (initialValues) {
1921
- initialValues.forEach((v) => this.add(v));
1922
- }
1923
- }
1935
+ $changes = new ChangeTree(this);
1936
+ $items = new Map();
1937
+ $indexes = new Map();
1938
+ $refId = 0;
1939
+ //
1940
+ // Decoding callbacks
1941
+ //
1942
+ $callbacks;
1924
1943
  onAdd(callback, triggerAll = true) {
1925
1944
  return addCallback((this.$callbacks || (this.$callbacks = [])), OPERATION.ADD, callback, (triggerAll)
1926
1945
  ? this.$items
@@ -1931,6 +1950,11 @@ class SetSchema {
1931
1950
  static is(type) {
1932
1951
  return type['set'] !== undefined;
1933
1952
  }
1953
+ constructor(initialValues) {
1954
+ if (initialValues) {
1955
+ initialValues.forEach((v) => this.add(v));
1956
+ }
1957
+ }
1934
1958
  add(value) {
1935
1959
  // immediatelly return false if value already added.
1936
1960
  if (this.has(value)) {
@@ -2067,10 +2091,8 @@ class SetSchema {
2067
2091
  }
2068
2092
 
2069
2093
  class ClientState {
2070
- constructor() {
2071
- this.refIds = new WeakSet();
2072
- this.containerIndexes = new WeakMap();
2073
- }
2094
+ refIds = new WeakSet();
2095
+ containerIndexes = new WeakMap();
2074
2096
  // containerIndexes = new Map<ChangeTree, Set<number>>();
2075
2097
  addRefId(changeTree) {
2076
2098
  if (!this.refIds.has(changeTree)) {
@@ -2087,16 +2109,14 @@ class ClientState {
2087
2109
  }
2088
2110
 
2089
2111
  class ReferenceTracker {
2090
- constructor() {
2091
- //
2092
- // Relation of refId => Schema structure
2093
- // For direct access of structures during decoding time.
2094
- //
2095
- this.refs = new Map();
2096
- this.refCounts = {};
2097
- this.deletedRefs = new Set();
2098
- this.nextUniqueId = 0;
2099
- }
2112
+ //
2113
+ // Relation of refId => Schema structure
2114
+ // For direct access of structures during decoding time.
2115
+ //
2116
+ refs = new Map();
2117
+ refCounts = {};
2118
+ deletedRefs = new Set();
2119
+ nextUniqueId = 0;
2100
2120
  getNextUniqueId() {
2101
2121
  return this.nextUniqueId++;
2102
2122
  }
@@ -2212,6 +2232,26 @@ function decodePrimitiveType(type, bytes, it) {
2212
2232
  * Schema encoder / decoder
2213
2233
  */
2214
2234
  class Schema {
2235
+ static _typeid;
2236
+ static _context;
2237
+ static _definition = SchemaDefinition.create();
2238
+ static onError(e) {
2239
+ console.error(e);
2240
+ }
2241
+ static is(type) {
2242
+ return (type['_definition'] &&
2243
+ type['_definition'].schema !== undefined);
2244
+ }
2245
+ $changes;
2246
+ // TODO: refactor. this feature needs to be ported to other languages with potentially different API
2247
+ // protected $listeners: { [field: string]: Array<(value: any, previousValue: any) => void> };
2248
+ $callbacks;
2249
+ onChange(callback) {
2250
+ return addCallback((this.$callbacks || (this.$callbacks = [])), OPERATION.REPLACE, callback);
2251
+ }
2252
+ onRemove(callback) {
2253
+ return addCallback((this.$callbacks || (this.$callbacks = [])), OPERATION.DELETE, callback);
2254
+ }
2215
2255
  // allow inherited classes to have a constructor
2216
2256
  constructor(...args) {
2217
2257
  // fix enumerability of fields for end-user
@@ -2243,19 +2283,6 @@ class Schema {
2243
2283
  this.assign(args[0]);
2244
2284
  }
2245
2285
  }
2246
- static onError(e) {
2247
- console.error(e);
2248
- }
2249
- static is(type) {
2250
- return (type['_definition'] &&
2251
- type['_definition'].schema !== undefined);
2252
- }
2253
- onChange(callback) {
2254
- return addCallback((this.$callbacks || (this.$callbacks = [])), OPERATION.REPLACE, callback);
2255
- }
2256
- onRemove(callback) {
2257
- return addCallback((this.$callbacks || (this.$callbacks = [])), OPERATION.DELETE, callback);
2258
- }
2259
2286
  assign(props) {
2260
2287
  Object.assign(this, props);
2261
2288
  return this;
@@ -2446,6 +2473,7 @@ class Schema {
2446
2473
  const key = dynamicIndex;
2447
2474
  // ref.set(key, value);
2448
2475
  ref['$items'].set(key, value);
2476
+ ref['$changes'].allChanges.add(fieldIndex);
2449
2477
  }
2450
2478
  else if (ref instanceof ArraySchema) {
2451
2479
  // const key = ref['$indexes'][field];
@@ -2797,7 +2825,7 @@ class Schema {
2797
2825
  const schema = this._definition.schema;
2798
2826
  for (let field in schema) {
2799
2827
  if (typeof (this[field]) === "object" &&
2800
- typeof (this[field].clone) === "function") {
2828
+ typeof (this[field]?.clone) === "function") {
2801
2829
  // deep clone
2802
2830
  cloned[field] = this[field].clone();
2803
2831
  }
@@ -2921,7 +2949,6 @@ class Schema {
2921
2949
  }
2922
2950
  }
2923
2951
  }
2924
- Schema._definition = SchemaDefinition.create();
2925
2952
 
2926
2953
  function dumpChanges(schema) {
2927
2954
  const changeTrees = [schema['$changes']];
@@ -2942,7 +2969,7 @@ function dumpChanges(schema) {
2942
2969
  return dump;
2943
2970
  }
2944
2971
 
2945
- /*! *****************************************************************************
2972
+ /******************************************************************************
2946
2973
  Copyright (c) Microsoft Corporation.
2947
2974
 
2948
2975
  Permission to use, copy, modify, and/or distribute this software for any
@@ -2969,6 +2996,9 @@ const reflectionContext = { context: new Context() };
2969
2996
  * Reflection
2970
2997
  */
2971
2998
  class ReflectionField extends Schema {
2999
+ name;
3000
+ type;
3001
+ referencedType;
2972
3002
  }
2973
3003
  __decorate([
2974
3004
  type("string", reflectionContext)
@@ -2980,10 +3010,8 @@ __decorate([
2980
3010
  type("number", reflectionContext)
2981
3011
  ], ReflectionField.prototype, "referencedType", void 0);
2982
3012
  class ReflectionType extends Schema {
2983
- constructor() {
2984
- super(...arguments);
2985
- this.fields = new ArraySchema();
2986
- }
3013
+ id;
3014
+ fields = new ArraySchema();
2987
3015
  }
2988
3016
  __decorate([
2989
3017
  type("number", reflectionContext)
@@ -2992,10 +3020,8 @@ __decorate([
2992
3020
  type([ReflectionField], reflectionContext)
2993
3021
  ], ReflectionType.prototype, "fields", void 0);
2994
3022
  class Reflection extends Schema {
2995
- constructor() {
2996
- super(...arguments);
2997
- this.types = new ArraySchema();
2998
- }
3023
+ types = new ArraySchema();
3024
+ rootType;
2999
3025
  static encode(instance) {
3000
3026
  const rootSchemaType = instance.constructor;
3001
3027
  const reflection = new Reflection();