@colyseus/schema 3.0.0-alpha.31 → 3.0.0-alpha.33

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.
@@ -473,7 +473,6 @@ class ChangeTree {
473
473
  changeSet.set(index, operation ?? exports.OPERATION.DELETE);
474
474
  // remove `root` reference
475
475
  if (previousValue && previousValue[$changes]) {
476
- previousValue[$changes].root = undefined;
477
476
  //
478
477
  // FIXME: this.root is "undefined"
479
478
  //
@@ -863,21 +862,11 @@ var encode = /*#__PURE__*/Object.freeze({
863
862
  writeFloat64: writeFloat64
864
863
  });
865
864
 
866
- function encodeValue(encoder, bytes,
867
- // ref: Ref,
868
- type, value,
869
- // field: string | number,
870
- operation, it) {
865
+ function encodeValue(encoder, bytes, type, value, operation, it) {
871
866
  if (typeof (type) === "string") {
872
- //
873
- // Primitive values
874
- //
875
- // assertType(value, type as string, ref as Schema, field);
876
867
  encode[type]?.(bytes, value, it);
877
868
  }
878
869
  else if (type[Symbol.metadata] !== undefined) {
879
- // // TODO: move this to the `@type()` annotation
880
- // assertInstanceType(value, type as typeof Schema, ref as Schema, field);
881
870
  //
882
871
  // Encode refId for this instance.
883
872
  // The actual instance is going to be encoded on next `changeTree` iteration.
@@ -889,14 +878,6 @@ operation, it) {
889
878
  }
890
879
  }
891
880
  else {
892
- // //
893
- // // Custom type (MapSchema, ArraySchema, etc)
894
- // //
895
- // const definition = getType(Object.keys(type)[0]);
896
- // //
897
- // // ensure a ArraySchema has been provided
898
- // //
899
- // assertInstanceType(ref[field], definition.constructor, ref as Schema, field);
900
881
  //
901
882
  // Encode refId for this instance.
902
883
  // The actual instance is going to be encoded on next `changeTree` iteration.
@@ -916,14 +897,10 @@ const encodeSchemaOperation = function (encoder, bytes, changeTree, index, opera
916
897
  return;
917
898
  }
918
899
  const ref = changeTree.ref;
919
- const metadata = ref['constructor'][Symbol.metadata];
900
+ const metadata = ref.constructor[Symbol.metadata];
920
901
  const field = metadata[index];
921
902
  // TODO: inline this function call small performance gain
922
- encodeValue(encoder, bytes,
923
- // ref,
924
- metadata[index].type, ref[field.name],
925
- // index,
926
- operation, it);
903
+ encodeValue(encoder, bytes, metadata[index].type, ref[field.name], operation, it);
927
904
  };
928
905
  /**
929
906
  * Used for collections (MapSchema, CollectionSchema, SetSchema)
@@ -946,7 +923,7 @@ const encodeKeyValueOperation = function (encoder, bytes, changeTree, index, ope
946
923
  //
947
924
  // encode "alias" for dynamic fields (maps)
948
925
  //
949
- if ((operation & exports.OPERATION.ADD) == exports.OPERATION.ADD) { // ADD or DELETE_AND_ADD
926
+ if ((operation & exports.OPERATION.ADD) === exports.OPERATION.ADD) { // ADD or DELETE_AND_ADD
950
927
  if (typeof (ref['set']) === "function") {
951
928
  //
952
929
  // MapSchema dynamic key
@@ -955,8 +932,8 @@ const encodeKeyValueOperation = function (encoder, bytes, changeTree, index, ope
955
932
  string$1(bytes, dynamicIndex, it);
956
933
  }
957
934
  }
958
- const type = changeTree.getType(index);
959
- const value = changeTree.getValue(index);
935
+ const type = ref[$childType];
936
+ const value = ref[$getByIndex](index);
960
937
  // try { throw new Error(); } catch (e) {
961
938
  // // only print if not coming from Reflection.ts
962
939
  // if (!e.stack.includes("src/Reflection.ts")) {
@@ -970,11 +947,7 @@ const encodeKeyValueOperation = function (encoder, bytes, changeTree, index, ope
970
947
  // }
971
948
  // }
972
949
  // TODO: inline this function call small performance gain
973
- encodeValue(encoder, bytes,
974
- // ref,
975
- type, value,
976
- // index,
977
- operation, it);
950
+ encodeValue(encoder, bytes, type, value, operation, it);
978
951
  };
979
952
  /**
980
953
  * Used for collections (MapSchema, ArraySchema, etc.)
@@ -1018,11 +991,7 @@ const encodeArray = function (encoder, bytes, changeTree, field, operation, it,
1018
991
  // items: ref.toJSON(),
1019
992
  // });
1020
993
  // TODO: inline this function call small performance gain
1021
- encodeValue(encoder, bytes,
1022
- // ref,
1023
- type, value,
1024
- // field,
1025
- operation, it);
994
+ encodeValue(encoder, bytes, type, value, operation, it);
1026
995
  };
1027
996
 
1028
997
  /**
@@ -1328,7 +1297,9 @@ function decodeValue(decoder, operation, ref, index, type, bytes, it, allChanges
1328
1297
  if (!value) {
1329
1298
  value = decoder.createInstanceOfType(childType);
1330
1299
  }
1331
- $root.addRef(refId, value, (value !== previousValue));
1300
+ $root.addRef(refId, value, (value !== previousValue || // increment ref count if value has changed
1301
+ (operation === exports.OPERATION.DELETE_AND_ADD && value === previousValue) // increment ref count if the same instance is being added again
1302
+ ));
1332
1303
  }
1333
1304
  }
1334
1305
  else if (typeof (type) === "string") {
@@ -3475,24 +3446,40 @@ class Root {
3475
3446
  return this.nextUniqueId++;
3476
3447
  }
3477
3448
  add(changeTree) {
3478
- const refCount = this.refCount.get(changeTree) || 0;
3479
- this.refCount.set(changeTree, refCount + 1);
3449
+ const previousRefCount = this.refCount.get(changeTree);
3450
+ if (previousRefCount === 0) {
3451
+ //
3452
+ // When a ChangeTree is re-added, it means that it was previously removed.
3453
+ // We need to re-add all changes to the `changes` map.
3454
+ //
3455
+ changeTree.allChanges.forEach((operation, index) => {
3456
+ changeTree.changes.set(index, operation);
3457
+ });
3458
+ }
3459
+ const refCount = (previousRefCount || 0) + 1;
3460
+ this.refCount.set(changeTree, refCount);
3461
+ return refCount;
3480
3462
  }
3481
3463
  remove(changeTree) {
3482
- const refCount = this.refCount.get(changeTree);
3483
- if (refCount <= 1) {
3464
+ const refCount = (this.refCount.get(changeTree)) - 1;
3465
+ if (refCount <= 0) {
3466
+ //
3467
+ // Only remove "root" reference if it's the last reference
3468
+ //
3469
+ changeTree.root = undefined;
3484
3470
  this.allChanges.delete(changeTree);
3485
3471
  this.changes.delete(changeTree);
3486
3472
  if (changeTree.isFiltered || changeTree.isPartiallyFiltered) {
3487
3473
  this.allFilteredChanges.delete(changeTree);
3488
3474
  this.filteredChanges.delete(changeTree);
3489
3475
  }
3490
- this.refCount.delete(changeTree);
3476
+ this.refCount.set(changeTree, 0);
3491
3477
  }
3492
3478
  else {
3493
- this.refCount.set(changeTree, refCount - 1);
3479
+ this.refCount.set(changeTree, refCount);
3494
3480
  }
3495
3481
  changeTree.forEachChild((child, _) => this.remove(child));
3482
+ return refCount;
3496
3483
  }
3497
3484
  clear() {
3498
3485
  this.changes.clear();
@@ -3526,7 +3513,7 @@ class Encoder {
3526
3513
  const shouldClearChanges = !isEncodeAll && !hasView;
3527
3514
  for (const [changeTree, changes] of changeTrees.entries()) {
3528
3515
  const ref = changeTree.ref;
3529
- const ctor = ref['constructor'];
3516
+ const ctor = ref.constructor;
3530
3517
  const encoder = ctor[$encoder];
3531
3518
  const filter = ctor[$filter];
3532
3519
  // try { throw new Error(); } catch (e) {
@@ -3550,8 +3537,7 @@ class Encoder {
3550
3537
  buffer[it.offset++] = SWITCH_TO_STRUCTURE & 255;
3551
3538
  number$1(buffer, changeTree.refId, it);
3552
3539
  }
3553
- const changesIterator = changes.entries();
3554
- for (const [fieldIndex, operation] of changesIterator) {
3540
+ for (const [fieldIndex, operation] of changes.entries()) {
3555
3541
  //
3556
3542
  // first pass (encodeAll), identify "filtered" operations without encoding them
3557
3543
  // they will be encoded per client, based on their view.
@@ -3577,7 +3563,12 @@ class Encoder {
3577
3563
  encoder(this, buffer, changeTree, fieldIndex, operation, it, isEncodeAll, hasView);
3578
3564
  }
3579
3565
  // if (shouldClearChanges) {
3580
- // changeTree.endEncode();
3566
+ // // changeTree.endEncode();
3567
+ // changeTree.changes.clear();
3568
+ // // ArraySchema and MapSchema have a custom "encode end" method
3569
+ // changeTree.ref[$onEncodeEnd]?.();
3570
+ // // Not a new instance anymore
3571
+ // delete changeTree[$isNew];
3581
3572
  // }
3582
3573
  }
3583
3574
  if (it.offset > buffer.byteLength) {