@colyseus/schema 3.0.24 → 3.0.25

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.
@@ -958,6 +958,9 @@ const Metadata = {
958
958
  }
959
959
  };
960
960
 
961
+ function createChangeSet() {
962
+ return { indexes: {}, operations: [] };
963
+ }
961
964
  function setOperationAtIndex(changeSet, index) {
962
965
  const operationsIndex = changeSet.indexes[index];
963
966
  if (operationsIndex === undefined) {
@@ -975,7 +978,11 @@ function deleteOperationAtIndex(changeSet, index) {
975
978
  delete changeSet.indexes[index];
976
979
  }
977
980
  function enqueueChangeTree(root, changeTree, changeSet, queueRootIndex = changeTree[changeSet].queueRootIndex) {
978
- if (root && root[changeSet][queueRootIndex] !== changeTree) {
981
+ if (!root) {
982
+ // skip
983
+ return;
984
+ }
985
+ else if (root[changeSet][queueRootIndex] !== changeTree) {
979
986
  changeTree[changeSet].queueRootIndex = root[changeSet].push(changeTree) - 1;
980
987
  }
981
988
  }
@@ -1252,11 +1259,12 @@ class ChangeTree {
1252
1259
  }
1253
1260
  return previousValue;
1254
1261
  }
1255
- endEncode() {
1262
+ endEncode(changeSetName) {
1256
1263
  this.indexedOperations = {};
1257
- // // clear changes
1258
- // this.changes.indexes = {};
1259
- // this.changes.operations.length = 0;
1264
+ // clear changeset
1265
+ this[changeSetName].indexes = {};
1266
+ this[changeSetName].operations.length = 0;
1267
+ this[changeSetName].queueRootIndex = undefined;
1260
1268
  // ArraySchema and MapSchema have a custom "encode end" method
1261
1269
  this.ref[$onEncodeEnd]?.();
1262
1270
  // Not a new instance anymore
@@ -1363,24 +1371,19 @@ class ChangeTree {
1363
1371
  || this.root.types.parentFiltered[key]
1364
1372
  || parentConstructor?.[Symbol.metadata]?.[$viewFieldIndexes]?.includes(parentIndex);
1365
1373
  //
1366
- // TODO: refactor this!
1367
- //
1368
- // swapping `changes` and `filteredChanges` is required here
1369
- // because "isFiltered" may not be imedialely available on `change()`
1370
- // (this happens when instance is detached from root or parent)
1374
+ // "isFiltered" may not be imedialely available during `change()` due to the instance not being attached to the root yet.
1375
+ // when it's available, we need to enqueue the "changes" changeset into the "filteredChanges" changeset.
1371
1376
  //
1372
1377
  if (this.isFiltered) {
1373
- this.filteredChanges = { indexes: {}, operations: [] };
1374
- this.allFilteredChanges = { indexes: {}, operations: [] };
1378
+ if (!this.filteredChanges) {
1379
+ this.filteredChanges = createChangeSet();
1380
+ this.allFilteredChanges = createChangeSet();
1381
+ }
1375
1382
  if (this.changes.operations.length > 0) {
1376
- // swap changes reference
1377
- const changes = this.changes;
1378
- this.changes = this.filteredChanges;
1379
- this.filteredChanges = changes;
1380
- // swap "all changes" reference
1381
- const allFilteredChanges = this.allFilteredChanges;
1382
- this.allFilteredChanges = this.allChanges;
1383
- this.allChanges = allFilteredChanges;
1383
+ this.changes.operations.forEach((index) => setOperationAtIndex(this.filteredChanges, index));
1384
+ this.allChanges.operations.forEach((index) => setOperationAtIndex(this.allFilteredChanges, index));
1385
+ this.changes = createChangeSet();
1386
+ this.allChanges = createChangeSet();
1384
1387
  }
1385
1388
  }
1386
1389
  }
@@ -3660,19 +3663,6 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
3660
3663
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
3661
3664
  };
3662
3665
 
3663
- function spliceOne(arr, index) {
3664
- // manually splice an array
3665
- if (index === -1 || index >= arr.length) {
3666
- return false;
3667
- }
3668
- const len = arr.length - 1;
3669
- for (let i = index; i < len; i++) {
3670
- arr[i] = arr[i + 1];
3671
- }
3672
- arr.length = len;
3673
- return true;
3674
- }
3675
-
3676
3666
  class Root {
3677
3667
  constructor(types) {
3678
3668
  this.types = types;
@@ -3753,11 +3743,16 @@ class Root {
3753
3743
  }
3754
3744
  removeChangeFromChangeSet(changeSetName, changeTree) {
3755
3745
  const changeSet = this[changeSetName];
3756
- if (spliceOne(changeSet, changeSet.indexOf(changeTree))) {
3746
+ const changeSetIndex = changeSet.indexOf(changeTree);
3747
+ if (changeSetIndex !== -1) {
3757
3748
  changeTree[changeSetName].queueRootIndex = -1;
3758
- // changeSet[index] = undefined;
3749
+ changeSet[changeSetIndex] = undefined;
3759
3750
  return true;
3760
3751
  }
3752
+ // if (spliceOne(changeSet, changeSet.indexOf(changeTree))) {
3753
+ // changeTree[changeSetName].queueRootIndex = -1;
3754
+ // return true;
3755
+ // }
3761
3756
  }
3762
3757
  clear() {
3763
3758
  this.changes.length = 0;
@@ -3790,10 +3785,12 @@ class Encoder {
3790
3785
  ) {
3791
3786
  const hasView = (view !== undefined);
3792
3787
  const rootChangeTree = this.state[$changes];
3793
- const shouldDiscardChanges = !isEncodeAll && !hasView;
3794
3788
  const changeTrees = this.root[changeSetName];
3795
3789
  for (let i = 0, numChangeTrees = changeTrees.length; i < numChangeTrees; i++) {
3796
3790
  const changeTree = changeTrees[i];
3791
+ if (!changeTree) {
3792
+ continue;
3793
+ }
3797
3794
  if (hasView) {
3798
3795
  if (!view.visible.has(changeTree)) {
3799
3796
  view.invisible.add(changeTree);
@@ -3841,10 +3838,6 @@ class Encoder {
3841
3838
  }
3842
3839
  encoder(this, buffer, changeTree, fieldIndex, operation, it, isEncodeAll, hasView, metadata);
3843
3840
  }
3844
- // if (shouldDiscardChanges) {
3845
- // changeTree.discard();
3846
- // changeTree.isNew = false; // Not a new instance anymore
3847
- // }
3848
3841
  }
3849
3842
  if (it.offset > buffer.byteLength) {
3850
3843
  // we can assume that n + 1 poolSize will suffice given that we are likely done with encoding at this point
@@ -3868,19 +3861,6 @@ class Encoder {
3868
3861
  return this.encode({ offset: initialOffset }, view, buffer, changeSetName, isEncodeAll);
3869
3862
  }
3870
3863
  else {
3871
- //
3872
- // only clear changes after making sure buffer resize is not required.
3873
- //
3874
- if (shouldDiscardChanges) {
3875
- //
3876
- // TODO: avoid iterating over change trees twice.
3877
- //
3878
- for (let i = 0, numChangeTrees = changeTrees.length; i < numChangeTrees; i++) {
3879
- const changeTree = changeTrees[i];
3880
- changeTree.discard();
3881
- changeTree.isNew = false; // Not a new instance anymore
3882
- }
3883
- }
3884
3864
  return buffer.subarray(0, it.offset);
3885
3865
  }
3886
3866
  }
@@ -3965,7 +3945,7 @@ class Encoder {
3965
3945
  let length = this.root.changes.length;
3966
3946
  if (length > 0) {
3967
3947
  while (length--) {
3968
- this.root.changes[length]?.endEncode();
3948
+ this.root.changes[length]?.endEncode('changes');
3969
3949
  }
3970
3950
  this.root.changes.length = 0;
3971
3951
  }
@@ -3973,7 +3953,7 @@ class Encoder {
3973
3953
  length = this.root.filteredChanges.length;
3974
3954
  if (length > 0) {
3975
3955
  while (length--) {
3976
- this.root.filteredChanges[length]?.endEncode();
3956
+ this.root.filteredChanges[length]?.endEncode('filteredChanges');
3977
3957
  }
3978
3958
  this.root.filteredChanges.length = 0;
3979
3959
  }
@@ -3996,6 +3976,19 @@ class Encoder {
3996
3976
  }
3997
3977
  }
3998
3978
 
3979
+ function spliceOne(arr, index) {
3980
+ // manually splice an array
3981
+ if (index === -1 || index >= arr.length) {
3982
+ return false;
3983
+ }
3984
+ const len = arr.length - 1;
3985
+ for (let i = index; i < len; i++) {
3986
+ arr[i] = arr[i + 1];
3987
+ }
3988
+ arr.length = len;
3989
+ return true;
3990
+ }
3991
+
3999
3992
  class DecodingWarning extends Error {
4000
3993
  constructor(message) {
4001
3994
  super(message);