@colyseus/schema 3.0.0-alpha.47 → 3.0.0-alpha.49

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.
package/src/Schema.ts CHANGED
@@ -174,17 +174,24 @@ export class Schema {
174
174
  this[metadata[index].name] = undefined;
175
175
  }
176
176
 
177
- static debugRefIds(instance: Ref, jsonContents: boolean = true, level: number = 0) {
177
+ /**
178
+ * Inspect the `refId` of all Schema instances in the tree. Optionally display the contents of the instance.
179
+ *
180
+ * @param instance Schema instance
181
+ * @param showContents display JSON contents of the instance
182
+ * @returns
183
+ */
184
+ static debugRefIds(instance: Ref, showContents: boolean = false, level: number = 0) {
178
185
  const ref = instance;
179
186
  const changeTree = ref[$changes];
180
187
 
181
- const contents = (jsonContents) ? ` - ${JSON.stringify(ref.toJSON())}` : "";
188
+ const contents = (showContents) ? ` - ${JSON.stringify(ref.toJSON())}` : "";
182
189
 
183
190
  let output = "";
184
191
  output += `${getIndent(level)}${ref.constructor.name} (refId: ${ref[$changes].refId})${contents}\n`;
185
192
 
186
193
  changeTree.forEachChild((childChangeTree) =>
187
- output += this.debugRefIds(childChangeTree.ref, jsonContents, level + 1));
194
+ output += this.debugRefIds(childChangeTree.ref, showContents, level + 1));
188
195
 
189
196
  return output;
190
197
  }
@@ -507,10 +507,13 @@ export class ChangeTree<T extends Ref=any> {
507
507
  // skip if parent is not set
508
508
  if (!parent) { return; }
509
509
 
510
+ //
510
511
  // ArraySchema | MapSchema - get the child type
512
+ // (if refType is typeof string, the parentFiltered[key] below will always be invalid)
513
+ //
511
514
  const refType = Metadata.isValidInstance(this.ref)
512
515
  ? this.ref.constructor
513
- : this.ref[$childType];
516
+ : this.ref[$childType];
514
517
 
515
518
  if (!Metadata.isValidInstance(parent)) {
516
519
  const parentChangeTree = parent[$changes];
@@ -521,10 +524,13 @@ export class ChangeTree<T extends Ref=any> {
521
524
  const parentConstructor = parent.constructor as typeof Schema;
522
525
 
523
526
  let key = `${this.root.types.getTypeId(refType as typeof Schema)}`;
524
- if (parentConstructor) { key += `-${this.root.types.schemas.get(parentConstructor)}`; }
527
+ if (parentConstructor) {
528
+ key += `-${this.root.types.schemas.get(parentConstructor)}`;
529
+ }
525
530
  key += `-${parentIndex}`;
526
531
 
527
- this.isFiltered = this.root.types.parentFiltered[key];
532
+ this.isFiltered = parent[$changes].isFiltered // in case parent is already filtered
533
+ || this.root.types.parentFiltered[key];
528
534
 
529
535
  // const parentMetadata = parentConstructor?.[Symbol.metadata];
530
536
  // this.isFiltered = parentMetadata?.[$viewFieldIndexes]?.includes(parentIndex) || this.root.types.parentFiltered[key];
@@ -9,11 +9,11 @@ import type { StateView } from "../../encoder/StateView";
9
9
  import type { Schema } from "../../Schema";
10
10
  import { assertInstanceType } from "../../encoding/assert";
11
11
 
12
- export class MapSchema<V=any, K extends string = string> implements Map<K, V>, Collection<K, V, [K, V]> {
12
+ export class MapSchema<V=any> implements Map<string, V>, Collection<string, V, [string, V]> {
13
13
  protected childType: new () => V;
14
14
 
15
- protected $items: Map<K, V> = new Map<K, V>();
16
- protected $indexes: Map<number, K> = new Map<number, K>();
15
+ protected $items: Map<string, V> = new Map<string, V>();
16
+ protected $indexes: Map<number, string> = new Map<number, string>();
17
17
 
18
18
  protected [$changes]: ChangeTree;
19
19
 
@@ -41,7 +41,7 @@ export class MapSchema<V=any, K extends string = string> implements Map<K, V>, C
41
41
  return type['map'] !== undefined;
42
42
  }
43
43
 
44
- constructor (initialValues?: Map<K, V> | Record<K, V>) {
44
+ constructor (initialValues?: Map<string, V> | Record<string, V>) {
45
45
  this[$changes] = new ChangeTree(this);
46
46
  this[$changes].indexes = {};
47
47
 
@@ -68,12 +68,12 @@ export class MapSchema<V=any, K extends string = string> implements Map<K, V>, C
68
68
  }
69
69
 
70
70
  /** Iterator */
71
- [Symbol.iterator](): IterableIterator<[K, V]> { return this.$items[Symbol.iterator](); }
71
+ [Symbol.iterator](): IterableIterator<[string, V]> { return this.$items[Symbol.iterator](); }
72
72
  get [Symbol.toStringTag]() { return this.$items[Symbol.toStringTag] }
73
73
 
74
74
  static get [Symbol.species]() { return MapSchema; }
75
75
 
76
- set(key: K, value: V) {
76
+ set(key: string, value: V) {
77
77
  if (value === undefined || value === null) {
78
78
  throw new Error(`MapSchema#set('${key}', ${value}): trying to set ${value} value on '${key}'.`);
79
79
 
@@ -83,7 +83,7 @@ export class MapSchema<V=any, K extends string = string> implements Map<K, V>, C
83
83
 
84
84
  // Force "key" as string
85
85
  // See: https://github.com/colyseus/colyseus/issues/561#issuecomment-1646733468
86
- key = key.toString() as K;
86
+ key = key.toString() as string;
87
87
 
88
88
  const changeTree = this[$changes];
89
89
 
@@ -138,11 +138,11 @@ export class MapSchema<V=any, K extends string = string> implements Map<K, V>, C
138
138
  return this;
139
139
  }
140
140
 
141
- get(key: K): V | undefined {
141
+ get(key: string): V | undefined {
142
142
  return this.$items.get(key);
143
143
  }
144
144
 
145
- delete(key: K) {
145
+ delete(key: string) {
146
146
  const index = this[$changes].indexes[key];
147
147
 
148
148
  this[$changes].delete(index);
@@ -166,11 +166,11 @@ export class MapSchema<V=any, K extends string = string> implements Map<K, V>, C
166
166
  changeTree.operation(OPERATION.CLEAR);
167
167
  }
168
168
 
169
- has (key: K) {
169
+ has (key: string) {
170
170
  return this.$items.has(key);
171
171
  }
172
172
 
173
- forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void) {
173
+ forEach(callbackfn: (value: V, key: string, map: Map<string, V>) => void) {
174
174
  this.$items.forEach(callbackfn);
175
175
  }
176
176
 
@@ -190,7 +190,7 @@ export class MapSchema<V=any, K extends string = string> implements Map<K, V>, C
190
190
  return this.$items.size;
191
191
  }
192
192
 
193
- protected setIndex(index: number, key: K) {
193
+ protected setIndex(index: number, key: string) {
194
194
  this.$indexes.set(index, key);
195
195
  }
196
196