@colyseus/schema 3.0.12 → 3.0.14

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
  //
@@ -4695,7 +4689,6 @@ class StateView {
4695
4689
  * Manual "ADD" operations for changes per ChangeTree, specific to this view.
4696
4690
  * (This is used to force encoding a property, even if it was not changed)
4697
4691
  */
4698
- // TODO: use map here!? may fix encode ordering issue
4699
4692
  this.changes = new Map();
4700
4693
  }
4701
4694
  // TODO: allow to set multiple tags at once
@@ -4780,19 +4773,18 @@ class StateView {
4780
4773
  addParentOf(childChangeTree, tag) {
4781
4774
  const changeTree = childChangeTree.parent[$changes];
4782
4775
  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;
4776
+ if (!this.items.has(changeTree)) {
4777
+ // view must have all "changeTree" parent tree
4778
+ this.items.add(changeTree);
4779
+ // add parent's parent
4780
+ const parentChangeTree = changeTree.parent?.[$changes];
4781
+ if (parentChangeTree && (parentChangeTree.filteredChanges !== undefined)) {
4782
+ this.addParentOf(changeTree, tag);
4783
+ }
4784
+ // parent is already available, no need to add it!
4785
+ if (!this.invisible.has(changeTree)) {
4786
+ return;
4787
+ }
4796
4788
  }
4797
4789
  // add parent's tag properties
4798
4790
  if (changeTree.getChange(parentIndex) !== exports.OPERATION.DELETE) {
@@ -4870,6 +4862,13 @@ class StateView {
4870
4862
  }
4871
4863
  return this;
4872
4864
  }
4865
+ has(obj) {
4866
+ return this.items.has(obj[$changes]);
4867
+ }
4868
+ hasTag(ob, tag = DEFAULT_VIEW_TAG) {
4869
+ const tags = this.tags?.get(ob[$changes]);
4870
+ return tags?.has(tag) ?? false;
4871
+ }
4873
4872
  }
4874
4873
 
4875
4874
  registerType("map", { constructor: MapSchema });