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