@colyseus/schema 2.0.32 → 2.0.33

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.
@@ -38,19 +38,13 @@ 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;
53
41
  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;
54
48
  this.ref = ref;
55
49
  this.setParent(parent, root);
56
50
  }
@@ -347,14 +341,7 @@ function getArrayProxy(value) {
347
341
  return value;
348
342
  }
349
343
  class ArraySchema {
350
- $changes = new ChangeTree(this);
351
- $items = new Map();
352
- $indexes = new Map();
353
- $refId = 0;
354
- //
355
- // Decoding callbacks
356
- //
357
- $callbacks;
344
+ s }
358
345
  onAdd(callback, triggerAll = true) {
359
346
  return addCallback((this.$callbacks || (this.$callbacks = {})), OPERATION.ADD, callback, (triggerAll)
360
347
  ? this.$items
@@ -370,6 +357,10 @@ class ArraySchema {
370
357
  (type['array'] !== undefined));
371
358
  }
372
359
  constructor(...items) {
360
+ this.$changes = new ChangeTree(this);
361
+ this.$items = new Map();
362
+ this.$indexes = new Map();
363
+ this.$refId = 0;
373
364
  this.push.apply(this, items);
374
365
  }
375
366
  set length(value) {
@@ -715,10 +706,6 @@ class ArraySchema {
715
706
  static get [Symbol.species]() {
716
707
  return ArraySchema;
717
708
  }
718
- // WORKAROUND for compatibility
719
- // - TypeScript 4 defines @@unscopables as a function
720
- // - TypeScript 5 defines @@unscopables as an object
721
- [Symbol.unscopables];
722
709
  /**
723
710
  * Returns an iterable of key, value pairs for every entry in the array
724
711
  */
@@ -872,14 +859,6 @@ function getMapProxy(value) {
872
859
  return value;
873
860
  }
874
861
  class MapSchema {
875
- $changes = new ChangeTree(this);
876
- $items = new Map();
877
- $indexes = new Map();
878
- $refId = 0;
879
- //
880
- // Decoding callbacks
881
- //
882
- $callbacks;
883
862
  onAdd(callback, triggerAll = true) {
884
863
  return addCallback((this.$callbacks || (this.$callbacks = {})), OPERATION.ADD, callback, (triggerAll)
885
864
  ? this.$items
@@ -891,6 +870,10 @@ class MapSchema {
891
870
  return type['map'] !== undefined;
892
871
  }
893
872
  constructor(initialValues) {
873
+ this.$changes = new ChangeTree(this);
874
+ this.$items = new Map();
875
+ this.$indexes = new Map();
876
+ this.$refId = 0;
894
877
  if (initialValues) {
895
878
  if (initialValues instanceof Map ||
896
879
  initialValues instanceof MapSchema) {
@@ -1059,17 +1042,15 @@ function getType(identifier) {
1059
1042
  }
1060
1043
 
1061
1044
  class SchemaDefinition {
1062
- schema;
1063
- //
1064
- // TODO: use a "field" structure combining all these properties per-field.
1065
- //
1066
- indexes = {};
1067
- fieldsByIndex = {};
1068
- filters;
1069
- indexesWithFilters;
1070
- childFilters; // childFilters are used on Map, Array, Set items.
1071
- deprecated = {};
1072
- descriptors = {};
1045
+ constructor() {
1046
+ //
1047
+ // TODO: use a "field" structure combining all these properties per-field.
1048
+ //
1049
+ this.indexes = {};
1050
+ this.fieldsByIndex = {};
1051
+ this.deprecated = {};
1052
+ this.descriptors = {};
1053
+ }
1073
1054
  static create(parent) {
1074
1055
  const definition = new SchemaDefinition();
1075
1056
  // support inheritance
@@ -1125,9 +1106,11 @@ function hasFilter(klass) {
1125
1106
  return klass._context && klass._context.useFilters;
1126
1107
  }
1127
1108
  class Context {
1128
- types = {};
1129
- schemas = new Map();
1130
- useFilters = false;
1109
+ constructor() {
1110
+ this.types = {};
1111
+ this.schemas = new Map();
1112
+ this.useFilters = false;
1113
+ }
1131
1114
  has(schema) {
1132
1115
  return this.schemas.has(schema);
1133
1116
  }
@@ -1857,14 +1840,6 @@ var decode = /*#__PURE__*/Object.freeze({
1857
1840
  });
1858
1841
 
1859
1842
  class CollectionSchema {
1860
- $changes = new ChangeTree(this);
1861
- $items = new Map();
1862
- $indexes = new Map();
1863
- $refId = 0;
1864
- //
1865
- // Decoding callbacks
1866
- //
1867
- $callbacks;
1868
1843
  onAdd(callback, triggerAll = true) {
1869
1844
  return addCallback((this.$callbacks || (this.$callbacks = [])), OPERATION.ADD, callback, (triggerAll)
1870
1845
  ? this.$items
@@ -1876,6 +1851,10 @@ class CollectionSchema {
1876
1851
  return type['collection'] !== undefined;
1877
1852
  }
1878
1853
  constructor(initialValues) {
1854
+ this.$changes = new ChangeTree(this);
1855
+ this.$items = new Map();
1856
+ this.$indexes = new Map();
1857
+ this.$refId = 0;
1879
1858
  if (initialValues) {
1880
1859
  initialValues.forEach((v) => this.add(v));
1881
1860
  }
@@ -2004,14 +1983,6 @@ class CollectionSchema {
2004
1983
  }
2005
1984
 
2006
1985
  class SetSchema {
2007
- $changes = new ChangeTree(this);
2008
- $items = new Map();
2009
- $indexes = new Map();
2010
- $refId = 0;
2011
- //
2012
- // Decoding callbacks
2013
- //
2014
- $callbacks;
2015
1986
  onAdd(callback, triggerAll = true) {
2016
1987
  return addCallback((this.$callbacks || (this.$callbacks = [])), OPERATION.ADD, callback, (triggerAll)
2017
1988
  ? this.$items
@@ -2023,6 +1994,10 @@ class SetSchema {
2023
1994
  return type['set'] !== undefined;
2024
1995
  }
2025
1996
  constructor(initialValues) {
1997
+ this.$changes = new ChangeTree(this);
1998
+ this.$items = new Map();
1999
+ this.$indexes = new Map();
2000
+ this.$refId = 0;
2026
2001
  if (initialValues) {
2027
2002
  initialValues.forEach((v) => this.add(v));
2028
2003
  }
@@ -2163,8 +2138,10 @@ class SetSchema {
2163
2138
  }
2164
2139
 
2165
2140
  class ClientState {
2166
- refIds = new WeakSet();
2167
- containerIndexes = new WeakMap();
2141
+ constructor() {
2142
+ this.refIds = new WeakSet();
2143
+ this.containerIndexes = new WeakMap();
2144
+ }
2168
2145
  // containerIndexes = new Map<ChangeTree, Set<number>>();
2169
2146
  addRefId(changeTree) {
2170
2147
  if (!this.refIds.has(changeTree)) {
@@ -2181,14 +2158,16 @@ class ClientState {
2181
2158
  }
2182
2159
 
2183
2160
  class ReferenceTracker {
2184
- //
2185
- // Relation of refId => Schema structure
2186
- // For direct access of structures during decoding time.
2187
- //
2188
- refs = new Map();
2189
- refCounts = {};
2190
- deletedRefs = new Set();
2191
- nextUniqueId = 0;
2161
+ constructor() {
2162
+ //
2163
+ // Relation of refId => Schema structure
2164
+ // For direct access of structures during decoding time.
2165
+ //
2166
+ this.refs = new Map();
2167
+ this.refCounts = {};
2168
+ this.deletedRefs = new Set();
2169
+ this.nextUniqueId = 0;
2170
+ }
2192
2171
  getNextUniqueId() {
2193
2172
  return this.nextUniqueId++;
2194
2173
  }
@@ -2313,9 +2292,7 @@ function decodePrimitiveType(type, bytes, it) {
2313
2292
  * Schema encoder / decoder
2314
2293
  */
2315
2294
  class Schema {
2316
- static _typeid;
2317
- static _context;
2318
- static _definition = SchemaDefinition.create();
2295
+ static { this._definition = SchemaDefinition.create(); }
2319
2296
  static onError(e) {
2320
2297
  console.error(e);
2321
2298
  }
@@ -2323,10 +2300,6 @@ class Schema {
2323
2300
  return (type['_definition'] &&
2324
2301
  type['_definition'].schema !== undefined);
2325
2302
  }
2326
- $changes;
2327
- // TODO: refactor. this feature needs to be ported to other languages with potentially different API
2328
- // protected $listeners: { [field: string]: Array<(value: any, previousValue: any) => void> };
2329
- $callbacks;
2330
2303
  onChange(callback) {
2331
2304
  return addCallback((this.$callbacks || (this.$callbacks = {})), OPERATION.REPLACE, callback);
2332
2305
  }
@@ -3091,9 +3064,6 @@ const reflectionContext = { context: new Context() };
3091
3064
  * Reflection
3092
3065
  */
3093
3066
  class ReflectionField extends Schema {
3094
- name;
3095
- type;
3096
- referencedType;
3097
3067
  }
3098
3068
  __decorate([
3099
3069
  type("string", reflectionContext)
@@ -3105,8 +3075,10 @@ __decorate([
3105
3075
  type("number", reflectionContext)
3106
3076
  ], ReflectionField.prototype, "referencedType", void 0);
3107
3077
  class ReflectionType extends Schema {
3108
- id;
3109
- fields = new ArraySchema();
3078
+ constructor() {
3079
+ super(...arguments);
3080
+ this.fields = new ArraySchema();
3081
+ }
3110
3082
  }
3111
3083
  __decorate([
3112
3084
  type("number", reflectionContext)
@@ -3115,8 +3087,10 @@ __decorate([
3115
3087
  type([ReflectionField], reflectionContext)
3116
3088
  ], ReflectionType.prototype, "fields", void 0);
3117
3089
  class Reflection extends Schema {
3118
- types = new ArraySchema();
3119
- rootType;
3090
+ constructor() {
3091
+ super(...arguments);
3092
+ this.types = new ArraySchema();
3093
+ }
3120
3094
  static encode(instance) {
3121
3095
  const rootSchemaType = instance.constructor;
3122
3096
  const reflection = new Reflection();