@colyseus/schema 3.0.18 → 3.0.20

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.
@@ -1358,9 +1358,8 @@ class ChangeTree {
1358
1358
  }
1359
1359
  key += `-${parentIndex}`;
1360
1360
  this.isFiltered = parent[$changes].isFiltered // in case parent is already filtered
1361
- || this.root.types.parentFiltered[key];
1362
- // const parentMetadata = parentConstructor?.[Symbol.metadata];
1363
- // this.isFiltered = parentMetadata?.[$viewFieldIndexes]?.includes(parentIndex) || this.root.types.parentFiltered[key];
1361
+ || this.root.types.parentFiltered[key]
1362
+ || parentConstructor?.[Symbol.metadata]?.[$viewFieldIndexes]?.includes(parentIndex);
1364
1363
  //
1365
1364
  // TODO: refactor this!
1366
1365
  //
@@ -1575,7 +1574,6 @@ function decodeValue(decoder, operation, ref, index, type, bytes, it, allChanges
1575
1574
  while ((iter = entries.next()) && !iter.done) {
1576
1575
  const [key, value] = iter.value;
1577
1576
  // if value is a schema, remove its reference
1578
- // FIXME: not sure if this is necessary, add more tests to confirm
1579
1577
  if (typeof (value) === "object") {
1580
1578
  previousRefId = $root.refIds.get(value);
1581
1579
  $root.removeRef(previousRefId);
@@ -3315,7 +3313,7 @@ class CollectionSchema {
3315
3313
  static [(_a$1 = $encoder, _b$1 = $decoder, $filter)](ref, index, view) {
3316
3314
  return (!view ||
3317
3315
  typeof (ref[$childType]) === "string" ||
3318
- view.items.has(ref[$getByIndex](index)[$changes]));
3316
+ view.items.has((ref[$getByIndex](index) ?? ref.deletedItems[index])[$changes]));
3319
3317
  }
3320
3318
  static is(type) {
3321
3319
  return type['collection'] !== undefined;
@@ -3323,6 +3321,7 @@ class CollectionSchema {
3323
3321
  constructor(initialValues) {
3324
3322
  this.$items = new Map();
3325
3323
  this.$indexes = new Map();
3324
+ this.deletedItems = {};
3326
3325
  this.$refId = 0;
3327
3326
  this[$changes] = new ChangeTree(this);
3328
3327
  this[$changes].indexes = {};
@@ -3372,7 +3371,7 @@ class CollectionSchema {
3372
3371
  if (index === undefined) {
3373
3372
  return false;
3374
3373
  }
3375
- this[$changes].delete(index);
3374
+ this.deletedItems[index] = this[$changes].delete(index);
3376
3375
  this.$indexes.delete(index);
3377
3376
  return this.$items.delete(index);
3378
3377
  }
@@ -3417,6 +3416,9 @@ class CollectionSchema {
3417
3416
  this.$items.delete(key);
3418
3417
  this.$indexes.delete(index);
3419
3418
  }
3419
+ [$onEncodeEnd]() {
3420
+ this.deletedItems = {};
3421
+ }
3420
3422
  toArray() {
3421
3423
  return Array.from(this.$items.values());
3422
3424
  }
@@ -3471,7 +3473,7 @@ class SetSchema {
3471
3473
  static [(_a = $encoder, _b = $decoder, $filter)](ref, index, view) {
3472
3474
  return (!view ||
3473
3475
  typeof (ref[$childType]) === "string" ||
3474
- view.items.has(ref[$getByIndex](index)[$changes]));
3476
+ view.items.has((ref[$getByIndex](index) ?? ref.deletedItems[index])[$changes]));
3475
3477
  }
3476
3478
  static is(type) {
3477
3479
  return type['set'] !== undefined;
@@ -3479,6 +3481,7 @@ class SetSchema {
3479
3481
  constructor(initialValues) {
3480
3482
  this.$items = new Map();
3481
3483
  this.$indexes = new Map();
3484
+ this.deletedItems = {};
3482
3485
  this.$refId = 0;
3483
3486
  this[$changes] = new ChangeTree(this);
3484
3487
  this[$changes].indexes = {};
@@ -3528,7 +3531,7 @@ class SetSchema {
3528
3531
  if (index === undefined) {
3529
3532
  return false;
3530
3533
  }
3531
- this[$changes].delete(index);
3534
+ this.deletedItems[index] = this[$changes].delete(index);
3532
3535
  this.$indexes.delete(index);
3533
3536
  return this.$items.delete(index);
3534
3537
  }
@@ -3585,6 +3588,9 @@ class SetSchema {
3585
3588
  this.$items.delete(key);
3586
3589
  this.$indexes.delete(index);
3587
3590
  }
3591
+ [$onEncodeEnd]() {
3592
+ this.deletedItems = {};
3593
+ }
3588
3594
  toArray() {
3589
3595
  return Array.from(this.$items.values());
3590
3596
  }
@@ -3789,7 +3795,7 @@ class Encoder {
3789
3795
  view.invisible.add(changeTree);
3790
3796
  continue; // skip this change tree
3791
3797
  }
3792
- else if (view.invisible.has(changeTree)) {
3798
+ else {
3793
3799
  view.invisible.delete(changeTree); // remove from invisible list
3794
3800
  }
3795
3801
  }
@@ -4745,10 +4751,11 @@ class StateView {
4745
4751
  } // skip "undefined" indexes
4746
4752
  const op = changeTree.indexedOperations[index] ?? exports.OPERATION.ADD;
4747
4753
  const tagAtIndex = metadata?.[index].tag;
4748
- if ((isInvisible || // if "invisible", include all
4749
- tagAtIndex === undefined || // "all change" with no tag
4750
- tagAtIndex === tag // tagged property
4751
- ) &&
4754
+ if (!changeTree.isNew && // new structures will be added as part of .encode() call, no need to force it to .encodeView()
4755
+ (isInvisible || // if "invisible", include all
4756
+ tagAtIndex === undefined || // "all change" with no tag
4757
+ tagAtIndex === tag // tagged property
4758
+ ) &&
4752
4759
  op !== exports.OPERATION.DELETE) {
4753
4760
  changes[index] = op;
4754
4761
  }
@@ -4777,10 +4784,8 @@ class StateView {
4777
4784
  if (parentChangeTree && (parentChangeTree.filteredChanges !== undefined)) {
4778
4785
  this.addParentOf(changeTree, tag);
4779
4786
  }
4780
- // parent is already available, no need to add it!
4781
- if (!this.invisible.has(changeTree)) {
4782
- return;
4783
- }
4787
+ // // parent is already available, no need to add it!
4788
+ // if (!this.invisible.has(changeTree)) { return; }
4784
4789
  }
4785
4790
  // add parent's tag properties
4786
4791
  if (changeTree.getChange(parentIndex) !== exports.OPERATION.DELETE) {