@colyseus/schema 3.0.12 → 3.0.13

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.
@@ -1246,6 +1246,7 @@ class ChangeTree {
1246
1246
  else {
1247
1247
  enqueueChangeTree(this.root, this, 'changes');
1248
1248
  }
1249
+ return previousValue;
1249
1250
  }
1250
1251
  endEncode() {
1251
1252
  this.indexedOperations = {};
@@ -2491,7 +2492,7 @@ class MapSchema {
2491
2492
  static [(_a$3 = $encoder, _b$3 = $decoder, $filter)](ref, index, view) {
2492
2493
  return (!view ||
2493
2494
  typeof (ref[$childType]) === "string" ||
2494
- view.items.has(ref[$getByIndex](index)[$changes]));
2495
+ view.items.has((ref[$getByIndex](index) ?? ref.deletedItems[index])[$changes]));
2495
2496
  }
2496
2497
  static is(type) {
2497
2498
  return type['map'] !== undefined;
@@ -2499,6 +2500,7 @@ class MapSchema {
2499
2500
  constructor(initialValues) {
2500
2501
  this.$items = new Map();
2501
2502
  this.$indexes = new Map();
2503
+ this.deletedItems = {};
2502
2504
  this[$changes] = new ChangeTree(this);
2503
2505
  this[$changes].indexes = {};
2504
2506
  if (initialValues) {
@@ -2578,7 +2580,7 @@ class MapSchema {
2578
2580
  }
2579
2581
  delete(key) {
2580
2582
  const index = this[$changes].indexes[key];
2581
- this[$changes].delete(index);
2583
+ this.deletedItems[index] = this[$changes].delete(index);
2582
2584
  return this.$items.delete(key);
2583
2585
  }
2584
2586
  clear() {
@@ -2625,17 +2627,7 @@ class MapSchema {
2625
2627
  this.$indexes.delete(index);
2626
2628
  }
2627
2629
  [$onEncodeEnd]() {
2628
- const changeTree = this[$changes];
2629
- const keys = Object.keys(changeTree.indexedOperations);
2630
- for (let i = 0, len = keys.length; i < len; i++) {
2631
- const key = keys[i];
2632
- const fieldIndex = Number(key);
2633
- const operation = changeTree.indexedOperations[key];
2634
- if (operation === OPERATION.DELETE) {
2635
- const index = this[$getByIndex](fieldIndex);
2636
- delete changeTree.indexes[index];
2637
- }
2638
- }
2630
+ this.deletedItems = {};
2639
2631
  }
2640
2632
  toJSON() {
2641
2633
  const map = {};
@@ -3942,11 +3934,13 @@ class Encoder {
3942
3934
  bytes[it.offset++] = SWITCH_TO_STRUCTURE & 255;
3943
3935
  encode.number(bytes, changeTree.refId, it);
3944
3936
  for (let i = 0, numChanges = keys.length; i < numChanges; i++) {
3945
- const key = keys[i];
3946
- const operation = changes[key];
3937
+ const index = Number(keys[i]);
3938
+ // workaround when using view.add() on item that has been deleted from state (see test "adding to view item that has been removed from state")
3939
+ const value = changeTree.ref[$getByIndex](index);
3940
+ const operation = (value !== undefined && changes[index]) || OPERATION.DELETE;
3947
3941
  // isEncodeAll = false
3948
3942
  // hasView = true
3949
- encoder(this, bytes, changeTree, Number(key), operation, it, false, true, metadata);
3943
+ encoder(this, bytes, changeTree, index, operation, it, false, true, metadata);
3950
3944
  }
3951
3945
  }
3952
3946
  //
@@ -4778,19 +4772,18 @@ class StateView {
4778
4772
  addParentOf(childChangeTree, tag) {
4779
4773
  const changeTree = childChangeTree.parent[$changes];
4780
4774
  const parentIndex = childChangeTree.parentIndex;
4781
- // view must have all "changeTree" parent tree
4782
- this.items.add(changeTree);
4783
- // add parent's parent
4784
- const parentChangeTree = changeTree.parent?.[$changes];
4785
- if (parentChangeTree && (parentChangeTree.filteredChanges !== undefined)) {
4786
- this.addParentOf(changeTree, tag);
4787
- }
4788
- if (
4789
- // parent is already available, no need to add it!
4790
- !this.invisible.has(changeTree) &&
4791
- // item is being replaced, no need to add parent
4792
- changeTree.indexedOperations[parentIndex] !== OPERATION.DELETE_AND_ADD) {
4793
- return;
4775
+ if (!this.items.has(changeTree)) {
4776
+ // view must have all "changeTree" parent tree
4777
+ this.items.add(changeTree);
4778
+ // add parent's parent
4779
+ const parentChangeTree = changeTree.parent?.[$changes];
4780
+ if (parentChangeTree && (parentChangeTree.filteredChanges !== undefined)) {
4781
+ this.addParentOf(changeTree, tag);
4782
+ }
4783
+ // parent is already available, no need to add it!
4784
+ if (!this.invisible.has(changeTree)) {
4785
+ return;
4786
+ }
4794
4787
  }
4795
4788
  // add parent's tag properties
4796
4789
  if (changeTree.getChange(parentIndex) !== OPERATION.DELETE) {
@@ -4868,6 +4861,13 @@ class StateView {
4868
4861
  }
4869
4862
  return this;
4870
4863
  }
4864
+ has(obj) {
4865
+ return this.items.has(obj[$changes]);
4866
+ }
4867
+ hasTag(ob, tag = DEFAULT_VIEW_TAG) {
4868
+ const tags = this.tags?.get(ob[$changes]);
4869
+ return tags?.has(tag) ?? false;
4870
+ }
4871
4871
  }
4872
4872
 
4873
4873
  registerType("map", { constructor: MapSchema });