@colyseus/schema 3.0.0-alpha.49 → 3.0.0-alpha.50

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.
@@ -3796,20 +3796,12 @@ class Encoder {
3796
3796
  const changeTrees = this.root[changeSetName];
3797
3797
  for (let i = 0, numChangeTrees = changeTrees.length; i < numChangeTrees; i++) {
3798
3798
  const changeTree = changeTrees[i];
3799
- // // Root#removeChangeFromChangeSet() is now removing instead of setting to "undefined"
3800
- // if (changeTree === undefined) { continue; }
3801
3799
  const operations = changeTree[changeSetName];
3802
3800
  const ref = changeTree.ref;
3803
3801
  const ctor = ref.constructor;
3804
3802
  const encoder = ctor[$encoder];
3805
3803
  const filter = ctor[$filter];
3806
3804
  const metadata = ctor[Symbol.metadata];
3807
- // try { throw new Error(); } catch (e) {
3808
- // // only print if not coming from Reflection.ts
3809
- // if (!e.stack.includes("src/Reflection.ts")) {
3810
- // console.log("ChangeTree:", { refId: changeTree.refId, ref: ref.constructor.name });
3811
- // }
3812
- // }
3813
3805
  if (hasView) {
3814
3806
  if (!view.items.has(changeTree)) {
3815
3807
  view.invisible.add(changeTree);
@@ -3844,24 +3836,12 @@ class Encoder {
3844
3836
  // view?.invisible.add(changeTree);
3845
3837
  continue;
3846
3838
  }
3847
- // try { throw new Error(); } catch (e) {
3848
- // // only print if not coming from Reflection.ts
3849
- // if (!e.stack.includes("src/Reflection.ts")) {
3850
- // console.log("WILL ENCODE", {
3851
- // ref: changeTree.ref.constructor.name,
3852
- // fieldIndex,
3853
- // operation: OPERATION[operation],
3854
- // });
3855
- // }
3856
- // }
3857
- // console.log("encode...", { ref: changeTree.ref.constructor.name, refId: changeTree.refId, fieldIndex, operation });
3858
3839
  encoder(this, buffer, changeTree, fieldIndex, operation, it, isEncodeAll, hasView, metadata);
3859
3840
  }
3860
- if (shouldDiscardChanges) {
3861
- changeTree.discard();
3862
- // Not a new instance anymore
3863
- changeTree.isNew = false;
3864
- }
3841
+ // if (shouldDiscardChanges) {
3842
+ // changeTree.discard();
3843
+ // changeTree.isNew = false; // Not a new instance anymore
3844
+ // }
3865
3845
  }
3866
3846
  if (it.offset > buffer.byteLength) {
3867
3847
  const newSize = getNextPowerOf2(buffer.byteLength * 2);
@@ -3873,7 +3853,7 @@ class Encoder {
3873
3853
  //
3874
3854
  // resize buffer and re-encode (TODO: can we avoid re-encoding here?)
3875
3855
  //
3876
- buffer = Buffer.allocUnsafeSlow(newSize);
3856
+ buffer = Buffer.alloc(newSize);
3877
3857
  // assign resized buffer to local sharedBuffer
3878
3858
  if (buffer === this.sharedBuffer) {
3879
3859
  this.sharedBuffer = buffer;
@@ -3881,28 +3861,27 @@ class Encoder {
3881
3861
  return this.encode({ offset: initialOffset }, view, buffer, changeSetName, isEncodeAll);
3882
3862
  }
3883
3863
  else {
3884
- // //
3885
- // // only clear changes after making sure buffer resize is not required.
3886
- // //
3887
- // if (shouldClearChanges) {
3888
- // //
3889
- // // FIXME: avoid iterating over change trees twice.
3890
- // //
3891
- // this.onEndEncode(changeTrees);
3892
- // }
3864
+ //
3865
+ // only clear changes after making sure buffer resize is not required.
3866
+ //
3867
+ if (shouldDiscardChanges) {
3868
+ //
3869
+ // TODO: avoid iterating over change trees twice.
3870
+ //
3871
+ for (let i = 0, numChangeTrees = changeTrees.length; i < numChangeTrees; i++) {
3872
+ const changeTree = changeTrees[i];
3873
+ changeTree.discard();
3874
+ changeTree.isNew = false; // Not a new instance anymore
3875
+ }
3876
+ }
3893
3877
  return buffer.subarray(0, it.offset);
3894
3878
  }
3895
3879
  }
3896
3880
  encodeAll(it = { offset: 0 }, buffer = this.sharedBuffer) {
3897
- // console.log(`\nencodeAll(), this.root.allChanges (${(Object.keys(this.root.allChanges).length)})`);
3898
- // this.debugChanges("allChanges");
3899
3881
  return this.encode(it, undefined, buffer, "allChanges", true);
3900
3882
  }
3901
3883
  encodeAllView(view, sharedOffset, it, bytes = this.sharedBuffer) {
3902
3884
  const viewOffset = it.offset;
3903
- // console.log(`\nencodeAllView(), this.root.allFilteredChanges (${(Object.keys(this.root.allFilteredChanges).length)})`);
3904
- // this.debugChanges("allFilteredChanges");
3905
- // console.log("\n\nENCODE ALL FOR VIEW...\n\n")
3906
3885
  // try to encode "filtered" changes
3907
3886
  this.encode(it, view, bytes, "allFilteredChanges", true, viewOffset);
3908
3887
  return Buffer.concat([
@@ -3930,13 +3909,8 @@ class Encoder {
3930
3909
  }
3931
3910
  encodeView(view, sharedOffset, it, bytes = this.sharedBuffer) {
3932
3911
  const viewOffset = it.offset;
3933
- // console.log(`\nencodeView(), view.changes (${view.changes.size})`);
3934
- // this.debugChanges(view.changes);
3935
- // console.log(`\nencodeView(), this.root.filteredChanges (${this.root.filteredChanges.size})`);
3936
- // this.debugChanges("filteredChanges");
3937
3912
  // encode visibility changes (add/remove for this view)
3938
3913
  const refIds = Object.keys(view.changes);
3939
- // console.log("ENCODE VIEW:", refIds);
3940
3914
  for (let i = 0, numRefIds = refIds.length; i < numRefIds; i++) {
3941
3915
  const refId = refIds[i];
3942
3916
  const changes = view.changes[refId];
@@ -3968,7 +3942,6 @@ class Encoder {
3968
3942
  //
3969
3943
  // clear "view" changes after encoding
3970
3944
  view.changes = {};
3971
- // console.log("FILTERED CHANGES:", this.root.filteredChanges);
3972
3945
  // try to encode "filtered" changes
3973
3946
  this.encode(it, view, bytes, "filteredChanges", false, viewOffset);
3974
3947
  return Buffer.concat([
@@ -3991,7 +3964,6 @@ class Encoder {
3991
3964
  // }
3992
3965
  }
3993
3966
  discardChanges() {
3994
- // console.log("DISCARD CHANGES!");
3995
3967
  // discard shared changes
3996
3968
  let length = this.root.changes.length;
3997
3969
  if (length > 0) {