@colyseus/schema 1.0.28 → 1.0.29

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 (52) hide show
  1. package/build/cjs/index.js +9 -13
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/index.mjs +81 -110
  4. package/build/esm/index.mjs.map +1 -1
  5. package/build/umd/index.js +9 -13
  6. package/lib/Reflection.js +11 -11
  7. package/lib/Reflection.js.map +1 -1
  8. package/lib/Schema.js +2 -2
  9. package/lib/Schema.js.map +1 -1
  10. package/lib/annotations.js +8 -12
  11. package/lib/annotations.js.map +1 -1
  12. package/lib/changes/ReferenceTracker.d.ts +14 -0
  13. package/lib/changes/ReferenceTracker.js +77 -0
  14. package/lib/changes/ReferenceTracker.js.map +1 -0
  15. package/lib/codegen/api.js +1 -1
  16. package/lib/codegen/api.js.map +1 -1
  17. package/lib/codegen/cli.js +2 -2
  18. package/lib/codegen/cli.js.map +1 -1
  19. package/lib/codegen/languages/cpp.js +3 -3
  20. package/lib/codegen/languages/cpp.js.map +1 -1
  21. package/lib/codegen/languages/csharp.js +8 -12
  22. package/lib/codegen/languages/csharp.js.map +1 -1
  23. package/lib/codegen/languages/haxe.js +1 -1
  24. package/lib/codegen/languages/haxe.js.map +1 -1
  25. package/lib/codegen/languages/java.js +1 -1
  26. package/lib/codegen/languages/java.js.map +1 -1
  27. package/lib/codegen/languages/js.js +2 -2
  28. package/lib/codegen/languages/js.js.map +1 -1
  29. package/lib/codegen/languages/lua.js +2 -2
  30. package/lib/codegen/languages/lua.js.map +1 -1
  31. package/lib/codegen/languages/ts.js +9 -13
  32. package/lib/codegen/languages/ts.js.map +1 -1
  33. package/lib/codegen/parser.js +2 -1
  34. package/lib/codegen/parser.js.map +1 -1
  35. package/lib/events/EventEmitter.js +6 -10
  36. package/lib/events/EventEmitter.js.map +1 -1
  37. package/lib/index.js +4 -4
  38. package/lib/index.js.map +1 -1
  39. package/lib/types/ArraySchema.js +8 -12
  40. package/lib/types/ArraySchema.js.map +1 -1
  41. package/lib/types/typeRegistry.d.ts +5 -0
  42. package/lib/types/typeRegistry.js +13 -0
  43. package/lib/types/typeRegistry.js.map +1 -0
  44. package/lib/types/utils.d.ts +9 -0
  45. package/lib/types/utils.js +50 -0
  46. package/lib/types/utils.js.map +1 -0
  47. package/package.json +4 -1
  48. package/decoders/cpp/usage_array_schema_types.cpp +0 -40
  49. package/decoders/cpp/usage_child_schema_types.cpp +0 -24
  50. package/decoders/cpp/usage_inherited_types.cpp +0 -34
  51. package/decoders/cpp/usage_map_schema_types.cpp +0 -35
  52. package/decoders/cpp/usage_primitive_types.cpp +0 -41
@@ -41,14 +41,16 @@ var OPERATION;
41
41
  // Root holds all schema references by unique id
42
42
  //
43
43
  class Root {
44
- //
45
- // Relation of refId => Schema structure
46
- // For direct access of structures during decoding time.
47
- //
48
- refs = new Map();
49
- refCounts = {};
50
- deletedRefs = new Set();
51
- nextUniqueId = 0;
44
+ constructor() {
45
+ //
46
+ // Relation of refId => Schema structure
47
+ // For direct access of structures during decoding time.
48
+ //
49
+ this.refs = new Map();
50
+ this.refCounts = {};
51
+ this.deletedRefs = new Set();
52
+ this.nextUniqueId = 0;
53
+ }
52
54
  getNextUniqueId() {
53
55
  return this.nextUniqueId++;
54
56
  }
@@ -103,19 +105,13 @@ class Root {
103
105
  }
104
106
  }
105
107
  class ChangeTree {
106
- ref;
107
- refId;
108
- root;
109
- parent;
110
- parentIndex;
111
- indexes;
112
- changed = false;
113
- changes = new Map();
114
- allChanges = new Set();
115
- // cached indexes for filtering
116
- caches = {};
117
- currentCustomOperation = 0;
118
108
  constructor(ref, parent, root) {
109
+ this.changed = false;
110
+ this.changes = new Map();
111
+ this.allChanges = new Set();
112
+ // cached indexes for filtering
113
+ this.caches = {};
114
+ this.currentCustomOperation = 0;
119
115
  this.ref = ref;
120
116
  this.setParent(parent, root);
121
117
  }
@@ -370,16 +366,13 @@ function getArrayProxy(value) {
370
366
  return value;
371
367
  }
372
368
  class ArraySchema {
373
- $changes = new ChangeTree(this);
374
- $items = new Map();
375
- $indexes = new Map();
376
- $refId = 0;
377
- //
378
- // Decoding callbacks
379
- //
380
- onAdd;
381
- onRemove;
382
- onChange;
369
+ constructor(...items) {
370
+ this.$changes = new ChangeTree(this);
371
+ this.$items = new Map();
372
+ this.$indexes = new Map();
373
+ this.$refId = 0;
374
+ this.push.apply(this, items);
375
+ }
383
376
  static is(type) {
384
377
  return (
385
378
  // type format: ["string"]
@@ -387,9 +380,6 @@ class ArraySchema {
387
380
  // type format: { array: "string" }
388
381
  (type['array'] !== undefined));
389
382
  }
390
- constructor(...items) {
391
- this.push.apply(this, items);
392
- }
393
383
  set length(value) {
394
384
  if (value === 0) {
395
385
  this.clear();
@@ -836,20 +826,11 @@ function getMapProxy(value) {
836
826
  return value;
837
827
  }
838
828
  class MapSchema {
839
- $changes = new ChangeTree(this);
840
- $items = new Map();
841
- $indexes = new Map();
842
- $refId = 0;
843
- //
844
- // Decoding callbacks
845
- //
846
- onAdd;
847
- onRemove;
848
- onChange;
849
- static is(type) {
850
- return type['map'] !== undefined;
851
- }
852
829
  constructor(initialValues) {
830
+ this.$changes = new ChangeTree(this);
831
+ this.$items = new Map();
832
+ this.$indexes = new Map();
833
+ this.$refId = 0;
853
834
  if (initialValues) {
854
835
  if (initialValues instanceof Map) {
855
836
  initialValues.forEach((v, k) => this.set(k, v));
@@ -861,6 +842,9 @@ class MapSchema {
861
842
  }
862
843
  }
863
844
  }
845
+ static is(type) {
846
+ return type['map'] !== undefined;
847
+ }
864
848
  /** Iterator */
865
849
  [Symbol.iterator]() { return this.$items[Symbol.iterator](); }
866
850
  get [Symbol.toStringTag]() { return this.$items[Symbol.toStringTag]; }
@@ -1007,17 +991,15 @@ function getType(identifier) {
1007
991
  }
1008
992
 
1009
993
  class SchemaDefinition {
1010
- schema;
1011
- //
1012
- // TODO: use a "field" structure combining all these properties per-field.
1013
- //
1014
- indexes = {};
1015
- fieldsByIndex = {};
1016
- filters;
1017
- indexesWithFilters;
1018
- childFilters; // childFilters are used on Map, Array, Set items.
1019
- deprecated = {};
1020
- descriptors = {};
994
+ constructor() {
995
+ //
996
+ // TODO: use a "field" structure combining all these properties per-field.
997
+ //
998
+ this.indexes = {};
999
+ this.fieldsByIndex = {};
1000
+ this.deprecated = {};
1001
+ this.descriptors = {};
1002
+ }
1021
1003
  static create(parent) {
1022
1004
  const definition = new SchemaDefinition();
1023
1005
  // support inheritance
@@ -1070,9 +1052,11 @@ function hasFilter(klass) {
1070
1052
  return klass._context && klass._context.useFilters;
1071
1053
  }
1072
1054
  class Context {
1073
- types = {};
1074
- schemas = new Map();
1075
- useFilters = false;
1055
+ constructor() {
1056
+ this.types = {};
1057
+ this.schemas = new Map();
1058
+ this.useFilters = false;
1059
+ }
1076
1060
  has(schema) {
1077
1061
  return this.schemas.has(schema);
1078
1062
  }
@@ -1773,24 +1757,18 @@ var decode = /*#__PURE__*/Object.freeze({
1773
1757
  });
1774
1758
 
1775
1759
  class CollectionSchema {
1776
- $changes = new ChangeTree(this);
1777
- $items = new Map();
1778
- $indexes = new Map();
1779
- $refId = 0;
1780
- //
1781
- // Decoding callbacks
1782
- //
1783
- onAdd;
1784
- onRemove;
1785
- onChange;
1786
- static is(type) {
1787
- return type['collection'] !== undefined;
1788
- }
1789
1760
  constructor(initialValues) {
1761
+ this.$changes = new ChangeTree(this);
1762
+ this.$items = new Map();
1763
+ this.$indexes = new Map();
1764
+ this.$refId = 0;
1790
1765
  if (initialValues) {
1791
1766
  initialValues.forEach((v) => this.add(v));
1792
1767
  }
1793
1768
  }
1769
+ static is(type) {
1770
+ return type['collection'] !== undefined;
1771
+ }
1794
1772
  add(value) {
1795
1773
  // set "index" for reference.
1796
1774
  const index = this.$refId++;
@@ -1916,24 +1894,18 @@ class CollectionSchema {
1916
1894
  }
1917
1895
 
1918
1896
  class SetSchema {
1919
- $changes = new ChangeTree(this);
1920
- $items = new Map();
1921
- $indexes = new Map();
1922
- $refId = 0;
1923
- //
1924
- // Decoding callbacks
1925
- //
1926
- onAdd;
1927
- onRemove;
1928
- onChange;
1929
- static is(type) {
1930
- return type['set'] !== undefined;
1931
- }
1932
1897
  constructor(initialValues) {
1898
+ this.$changes = new ChangeTree(this);
1899
+ this.$items = new Map();
1900
+ this.$indexes = new Map();
1901
+ this.$refId = 0;
1933
1902
  if (initialValues) {
1934
1903
  initialValues.forEach((v) => this.add(v));
1935
1904
  }
1936
1905
  }
1906
+ static is(type) {
1907
+ return type['set'] !== undefined;
1908
+ }
1937
1909
  add(value) {
1938
1910
  // immediatelly return false if value already added.
1939
1911
  if (this.has(value)) {
@@ -2074,7 +2046,9 @@ class SetSchema {
2074
2046
  * Extracted from https://www.npmjs.com/package/strong-events
2075
2047
  */
2076
2048
  class EventEmitter_ {
2077
- handlers = [];
2049
+ constructor() {
2050
+ this.handlers = [];
2051
+ }
2078
2052
  register(cb, once = false) {
2079
2053
  this.handlers.push(cb);
2080
2054
  return this;
@@ -2096,8 +2070,10 @@ class EventEmitter_ {
2096
2070
  }
2097
2071
 
2098
2072
  class ClientState {
2099
- refIds = new WeakSet();
2100
- containerIndexes = new WeakMap();
2073
+ constructor() {
2074
+ this.refIds = new WeakSet();
2075
+ this.containerIndexes = new WeakMap();
2076
+ }
2101
2077
  // containerIndexes = new Map<ChangeTree, Set<number>>();
2102
2078
  addRefId(changeTree) {
2103
2079
  if (!this.refIds.has(changeTree)) {
@@ -2170,20 +2146,6 @@ function decodePrimitiveType(type, bytes, it) {
2170
2146
  * Schema encoder / decoder
2171
2147
  */
2172
2148
  class Schema {
2173
- static _typeid;
2174
- static _context;
2175
- static _definition = SchemaDefinition.create();
2176
- static onError(e) {
2177
- console.error(e);
2178
- }
2179
- static is(type) {
2180
- return (type['_definition'] &&
2181
- type['_definition'].schema !== undefined);
2182
- }
2183
- $changes;
2184
- // protected $root: ChangeSet;
2185
- // TODO: refactor. this feature needs to be ported to other languages with potentially different API
2186
- $listeners;
2187
2149
  // allow inherited classes to have a constructor
2188
2150
  constructor(...args) {
2189
2151
  // fix enumerability of fields for end-user
@@ -2210,6 +2172,13 @@ class Schema {
2210
2172
  this.assign(args[0]);
2211
2173
  }
2212
2174
  }
2175
+ static onError(e) {
2176
+ console.error(e);
2177
+ }
2178
+ static is(type) {
2179
+ return (type['_definition'] &&
2180
+ type['_definition'].schema !== undefined);
2181
+ }
2213
2182
  assign(props) {
2214
2183
  Object.assign(this, props);
2215
2184
  return this;
@@ -2938,6 +2907,7 @@ class Schema {
2938
2907
  });
2939
2908
  }
2940
2909
  }
2910
+ Schema._definition = SchemaDefinition.create();
2941
2911
 
2942
2912
  function dumpChanges(schema) {
2943
2913
  const changeTrees = [schema['$changes']];
@@ -2985,9 +2955,6 @@ const reflectionContext = new Context();
2985
2955
  * Reflection
2986
2956
  */
2987
2957
  class ReflectionField extends Schema {
2988
- name;
2989
- type;
2990
- referencedType;
2991
2958
  }
2992
2959
  __decorate([
2993
2960
  type("string", reflectionContext)
@@ -2999,8 +2966,10 @@ __decorate([
2999
2966
  type("number", reflectionContext)
3000
2967
  ], ReflectionField.prototype, "referencedType", void 0);
3001
2968
  class ReflectionType extends Schema {
3002
- id;
3003
- fields = new ArraySchema();
2969
+ constructor() {
2970
+ super(...arguments);
2971
+ this.fields = new ArraySchema();
2972
+ }
3004
2973
  }
3005
2974
  __decorate([
3006
2975
  type("number", reflectionContext)
@@ -3009,8 +2978,10 @@ __decorate([
3009
2978
  type([ReflectionField], reflectionContext)
3010
2979
  ], ReflectionType.prototype, "fields", void 0);
3011
2980
  class Reflection extends Schema {
3012
- types = new ArraySchema();
3013
- rootType;
2981
+ constructor() {
2982
+ super(...arguments);
2983
+ this.types = new ArraySchema();
2984
+ }
3014
2985
  static encode(instance) {
3015
2986
  const rootSchemaType = instance.constructor;
3016
2987
  const reflection = new Reflection();