@colyseus/schema 3.0.49 → 3.0.51

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.
@@ -1104,7 +1104,7 @@ class ChangeTree {
1104
1104
  if (typeof (this.ref[$childType]) !== "string") {
1105
1105
  // MapSchema / ArraySchema, etc.
1106
1106
  for (const [key, value] of this.ref.entries()) {
1107
- callback(value[$changes], key);
1107
+ callback(value[$changes], this.indexes?.[key] ?? key);
1108
1108
  }
1109
1109
  }
1110
1110
  }
@@ -1543,10 +1543,6 @@ const encodeSchemaOperation = function (encoder, bytes, changeTree, index, opera
1543
1543
  const encodeKeyValueOperation = function (encoder, bytes, changeTree, index, operation, it) {
1544
1544
  // encode operation
1545
1545
  bytes[it.offset++] = operation & 255;
1546
- // custom operations
1547
- if (operation === exports.OPERATION.CLEAR) {
1548
- return;
1549
- }
1550
1546
  // encode index
1551
1547
  encode.number(bytes, index, it);
1552
1548
  // Do not encode value for DELETE operations
@@ -1592,7 +1588,12 @@ const encodeArray = function (encoder, bytes, changeTree, field, operation, it,
1592
1588
  const useOperationByRefId = hasView && changeTree.isFiltered && (typeof (changeTree.getType(field)) !== "string");
1593
1589
  let refOrIndex;
1594
1590
  if (useOperationByRefId) {
1595
- refOrIndex = ref['tmpItems'][field][$changes].refId;
1591
+ const item = ref['tmpItems'][field];
1592
+ // Skip encoding if item is undefined (e.g. when clear() is called)
1593
+ if (!item) {
1594
+ return;
1595
+ }
1596
+ refOrIndex = item[$changes].refId;
1596
1597
  if (operation === exports.OPERATION.DELETE) {
1597
1598
  operation = exports.OPERATION.DELETE_BY_REFID;
1598
1599
  }
@@ -1605,11 +1606,6 @@ const encodeArray = function (encoder, bytes, changeTree, field, operation, it,
1605
1606
  }
1606
1607
  // encode operation
1607
1608
  bytes[it.offset++] = operation & 255;
1608
- // custom operations
1609
- if (operation === exports.OPERATION.CLEAR ||
1610
- operation === exports.OPERATION.REVERSE) {
1611
- return;
1612
- }
1613
1609
  // encode index
1614
1610
  encode.number(bytes, refOrIndex, it);
1615
1611
  // Do not encode value for DELETE operations
@@ -3413,7 +3409,12 @@ class Schema {
3413
3409
  ? ` [×${root.refCount[refId]}]`
3414
3410
  : '';
3415
3411
  let output = `${getIndent(level)}${keyPrefix}${ref.constructor.name} (refId: ${refId})${refCount}${contents}\n`;
3416
- changeTree.forEachChild((childChangeTree, key) => {
3412
+ changeTree.forEachChild((childChangeTree, indexOrKey) => {
3413
+ let key = indexOrKey;
3414
+ if (typeof indexOrKey === 'number' && ref['$indexes']) {
3415
+ // MapSchema
3416
+ key = ref['$indexes'].get(indexOrKey) ?? indexOrKey;
3417
+ }
3417
3418
  const keyPrefix = (ref['forEach'] !== undefined && key !== undefined) ? `["${key}"]: ` : "";
3418
3419
  output += this.debugRefIds(childChangeTree.ref, showContents, level + 1, decoder, keyPrefix);
3419
3420
  });
@@ -4108,11 +4109,15 @@ class Encoder {
4108
4109
  }
4109
4110
  for (let j = 0; j < numChanges; j++) {
4110
4111
  const fieldIndex = changeSet.operations[j];
4111
- const operation = (fieldIndex < 0)
4112
- ? Math.abs(fieldIndex) // "pure" operation without fieldIndex (e.g. CLEAR, REVERSE, etc.)
4113
- : (isEncodeAll)
4114
- ? exports.OPERATION.ADD
4115
- : changeTree.indexedOperations[fieldIndex];
4112
+ if (fieldIndex < 0) {
4113
+ // "pure" operation without fieldIndex (e.g. CLEAR, REVERSE, etc.)
4114
+ // encode and continue early - no need to reach $filter check
4115
+ buffer[it.offset++] = Math.abs(fieldIndex) & 255;
4116
+ continue;
4117
+ }
4118
+ const operation = (isEncodeAll)
4119
+ ? exports.OPERATION.ADD
4120
+ : changeTree.indexedOperations[fieldIndex];
4116
4121
  //
4117
4122
  // first pass (encodeAll), identify "filtered" operations without encoding them
4118
4123
  // they will be encoded per client, based on their view.