@colyseus/schema 3.0.28 → 3.0.29

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.
@@ -959,6 +959,9 @@ const Metadata = {
959
959
  fields[metadata[i].name] = metadata[i].type;
960
960
  }
961
961
  return fields;
962
+ },
963
+ hasViewTagAtIndex(metadata, index) {
964
+ return metadata?.[$viewFieldIndexes]?.includes(index);
962
965
  }
963
966
  };
964
967
 
@@ -1379,7 +1382,7 @@ class ChangeTree {
1379
1382
  key += `-${this.root.types.schemas.get(parentConstructor)}`;
1380
1383
  }
1381
1384
  key += `-${parentIndex}`;
1382
- const fieldHasViewTag = parentConstructor?.[Symbol.metadata]?.[$viewFieldIndexes]?.includes(parentIndex);
1385
+ const fieldHasViewTag = Metadata.hasViewTagAtIndex(parentConstructor?.[Symbol.metadata], parentIndex);
1383
1386
  this.isFiltered = parent[$changes].isFiltered // in case parent is already filtered
1384
1387
  || this.root.types.parentFiltered[key]
1385
1388
  || fieldHasViewTag;
@@ -1522,7 +1525,7 @@ const encodeArray = function (encoder, bytes, changeTree, field, operation, it,
1522
1525
  // encode index
1523
1526
  encode.number(bytes, refOrIndex, it);
1524
1527
  // Do not encode value for DELETE operations
1525
- if (operation === exports.OPERATION.DELETE) {
1528
+ if (operation === exports.OPERATION.DELETE || operation === exports.OPERATION.DELETE_BY_REFID) {
1526
1529
  return;
1527
1530
  }
1528
1531
  const type = changeTree.getType(field);
@@ -1743,10 +1746,14 @@ const decodeArray = function (decoder, bytes, it, ref, allChanges) {
1743
1746
  else if (operation === exports.OPERATION.ADD_BY_REFID) {
1744
1747
  const refId = decode.number(bytes, it);
1745
1748
  const itemByRefId = decoder.root.refs.get(refId);
1746
- // use existing index, or push new value
1747
- index = (itemByRefId)
1748
- ? ref.findIndex((value) => value === itemByRefId)
1749
- : ref.length;
1749
+ // if item already exists, use existing index
1750
+ if (itemByRefId) {
1751
+ index = ref.findIndex((value) => value === itemByRefId);
1752
+ }
1753
+ // fallback to use last index
1754
+ if (index === -1 || index === undefined) {
1755
+ index = ref.length;
1756
+ }
1750
1757
  }
1751
1758
  else {
1752
1759
  index = decode.number(bytes, it);