@colyseus/schema 3.0.9 → 3.0.11

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.
@@ -1236,6 +1236,7 @@ class ChangeTree {
1236
1236
  //
1237
1237
  this.root?.remove(previousValue[$changes]);
1238
1238
  }
1239
+ deleteOperationAtIndex(this.allChanges, allChangesIndex);
1239
1240
  //
1240
1241
  // FIXME: this is looking a ugly and repeated
1241
1242
  //
@@ -1244,7 +1245,6 @@ class ChangeTree {
1244
1245
  enqueueChangeTree(this.root, this, 'filteredChanges');
1245
1246
  }
1246
1247
  else {
1247
- deleteOperationAtIndex(this.allChanges, allChangesIndex);
1248
1248
  enqueueChangeTree(this.root, this, 'changes');
1249
1249
  }
1250
1250
  }
@@ -1846,7 +1846,6 @@ class ArraySchema {
1846
1846
  * - Then, the encoder iterates over all "owned" properties per instance and encodes them.
1847
1847
  */
1848
1848
  static [(_a$4 = $encoder, _b$4 = $decoder, $filter)](ref, index, view) {
1849
- // console.log("ArraSchema[$filter] VIEW??", !view)
1850
1849
  return (!view ||
1851
1850
  typeof (ref[$childType]) === "string" ||
1852
1851
  // view.items.has(ref[$getByIndex](index)[$changes])
@@ -2446,6 +2445,7 @@ class ArraySchema {
2446
2445
  }
2447
2446
  [$deleteByIndex](index) {
2448
2447
  this.items[index] = undefined;
2448
+ this.tmpItems[index] = undefined; // TODO: do not try to get "tmpItems" at decoding time.
2449
2449
  }
2450
2450
  [$onEncodeEnd]() {
2451
2451
  this.tmpItems = this.items.slice();
@@ -2453,6 +2453,7 @@ class ArraySchema {
2453
2453
  }
2454
2454
  [$onDecodeEnd]() {
2455
2455
  this.items = this.items.filter((item) => item !== undefined);
2456
+ this.tmpItems = this.items.slice(); // TODO: do no use "tmpItems" at decoding time.
2456
2457
  }
2457
2458
  toArray() {
2458
2459
  return this.items.slice(0);
@@ -3079,7 +3080,6 @@ var _a$2, _b$2;
3079
3080
  class Schema {
3080
3081
  static { this[_a$2] = encodeSchemaOperation; }
3081
3082
  static { this[_b$2] = decodeSchemaOperation; }
3082
- // public [$changes]: ChangeTree;
3083
3083
  /**
3084
3084
  * Assign the property descriptors required to track changes on this instance.
3085
3085
  * @param instance
@@ -3211,16 +3211,16 @@ class Schema {
3211
3211
  /**
3212
3212
  * Inspect the `refId` of all Schema instances in the tree. Optionally display the contents of the instance.
3213
3213
  *
3214
- * @param instance Schema instance
3214
+ * @param ref Schema instance
3215
3215
  * @param showContents display JSON contents of the instance
3216
3216
  * @returns
3217
3217
  */
3218
- static debugRefIds(instance, showContents = false, level = 0) {
3219
- const ref = instance;
3220
- const changeTree = ref[$changes];
3218
+ static debugRefIds(ref, showContents = false, level = 0) {
3221
3219
  const contents = (showContents) ? ` - ${JSON.stringify(ref.toJSON())}` : "";
3220
+ const changeTree = ref[$changes];
3221
+ const refId = changeTree.refId;
3222
3222
  let output = "";
3223
- output += `${getIndent(level)}${ref.constructor.name} (refId: ${ref[$changes].refId})${contents}\n`;
3223
+ output += `${getIndent(level)}${ref.constructor.name} (refId: ${refId})${contents}\n`;
3224
3224
  changeTree.forEachChild((childChangeTree) => output += this.debugRefIds(childChangeTree.ref, showContents, level + 1));
3225
3225
  return output;
3226
3226
  }
@@ -3745,15 +3745,31 @@ class Root {
3745
3745
  }
3746
3746
  else {
3747
3747
  this.refCount[changeTree.refId] = refCount;
3748
+ //
3749
+ // When losing a reference to an instance, it is best to move the
3750
+ // ChangeTree to the end of the encoding queue.
3751
+ //
3752
+ // This way, at decoding time, the instance that contains the
3753
+ // ChangeTree will be available before the ChangeTree itself. If the
3754
+ // containing instance is not available, the Decoder will throw
3755
+ // "refId not found" error.
3756
+ //
3757
+ if (changeTree.filteredChanges !== undefined) {
3758
+ this.removeChangeFromChangeSet("filteredChanges", changeTree);
3759
+ enqueueChangeTree(this, changeTree, "filteredChanges");
3760
+ }
3761
+ else {
3762
+ this.removeChangeFromChangeSet("changes", changeTree);
3763
+ enqueueChangeTree(this, changeTree, "changes");
3764
+ }
3748
3765
  }
3749
3766
  changeTree.forEachChild((child, _) => this.remove(child));
3750
3767
  return refCount;
3751
3768
  }
3752
3769
  removeChangeFromChangeSet(changeSetName, changeTree) {
3753
3770
  const changeSet = this[changeSetName];
3754
- const index = changeSet.indexOf(changeTree);
3755
- if (index !== -1) {
3756
- spliceOne(changeSet, index);
3771
+ if (spliceOne(changeSet, changeSet.indexOf(changeTree))) {
3772
+ changeTree[changeSetName].queueRootIndex = -1;
3757
3773
  // changeSet[index] = undefined;
3758
3774
  }
3759
3775
  }
@@ -4035,7 +4051,7 @@ class ReferenceTracker {
4035
4051
  const refCount = this.refCounts[refId];
4036
4052
  if (refCount === undefined) {
4037
4053
  try {
4038
- throw new DecodingWarning("trying to remove refId that doesn't exist");
4054
+ throw new DecodingWarning("trying to remove refId that doesn't exist: " + refId);
4039
4055
  }
4040
4056
  catch (e) {
4041
4057
  console.warn(e);