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