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