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