@colyseus/schema 3.0.48 → 3.0.50

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
@@ -1605,11 +1601,6 @@ const encodeArray = function (encoder, bytes, changeTree, field, operation, it,
1605
1601
  }
1606
1602
  // encode operation
1607
1603
  bytes[it.offset++] = operation & 255;
1608
- // custom operations
1609
- if (operation === exports.OPERATION.CLEAR ||
1610
- operation === exports.OPERATION.REVERSE) {
1611
- return;
1612
- }
1613
1604
  // encode index
1614
1605
  encode.number(bytes, refOrIndex, it);
1615
1606
  // Do not encode value for DELETE operations
@@ -3180,20 +3171,29 @@ function defineTypes(target, fields, options) {
3180
3171
  }
3181
3172
  return target;
3182
3173
  }
3183
- function schema(fields, name, inherits = Schema) {
3174
+ function schema(fieldsAndMethods, name, inherits = Schema) {
3175
+ const fields = {};
3176
+ const methods = {};
3184
3177
  const defaultValues = {};
3185
3178
  const viewTagFields = {};
3186
- for (let fieldName in fields) {
3187
- const field = fields[fieldName];
3188
- if (typeof (field) === "object") {
3189
- if (field['default'] !== undefined) {
3190
- defaultValues[fieldName] = field['default'];
3179
+ for (let fieldName in fieldsAndMethods) {
3180
+ const value = fieldsAndMethods[fieldName];
3181
+ if (typeof (value) === "object") {
3182
+ if (value['default'] !== undefined) {
3183
+ defaultValues[fieldName] = value['default'];
3191
3184
  }
3192
- if (field['view'] !== undefined) {
3193
- viewTagFields[fieldName] = (typeof (field['view']) === "boolean")
3185
+ if (value['view'] !== undefined) {
3186
+ viewTagFields[fieldName] = (typeof (value['view']) === "boolean")
3194
3187
  ? DEFAULT_VIEW_TAG
3195
- : field['view'];
3188
+ : value['view'];
3196
3189
  }
3190
+ fields[fieldName] = value;
3191
+ }
3192
+ else if (typeof (value) === "function") {
3193
+ methods[fieldName] = value;
3194
+ }
3195
+ else {
3196
+ fields[fieldName] = value;
3197
3197
  }
3198
3198
  }
3199
3199
  const klass = Metadata.setFields(class extends inherits {
@@ -3205,6 +3205,9 @@ function schema(fields, name, inherits = Schema) {
3205
3205
  for (let fieldName in viewTagFields) {
3206
3206
  view(viewTagFields[fieldName])(klass.prototype, fieldName);
3207
3207
  }
3208
+ for (let methodName in methods) {
3209
+ klass.prototype[methodName] = methods[methodName];
3210
+ }
3208
3211
  if (name) {
3209
3212
  Object.defineProperty(klass, "name", { value: name });
3210
3213
  }
@@ -3401,7 +3404,12 @@ class Schema {
3401
3404
  ? ` [×${root.refCount[refId]}]`
3402
3405
  : '';
3403
3406
  let output = `${getIndent(level)}${keyPrefix}${ref.constructor.name} (refId: ${refId})${refCount}${contents}\n`;
3404
- changeTree.forEachChild((childChangeTree, key) => {
3407
+ changeTree.forEachChild((childChangeTree, indexOrKey) => {
3408
+ let key = indexOrKey;
3409
+ if (typeof indexOrKey === 'number' && ref['$indexes']) {
3410
+ // MapSchema
3411
+ key = ref['$indexes'].get(indexOrKey) ?? indexOrKey;
3412
+ }
3405
3413
  const keyPrefix = (ref['forEach'] !== undefined && key !== undefined) ? `["${key}"]: ` : "";
3406
3414
  output += this.debugRefIds(childChangeTree.ref, showContents, level + 1, decoder, keyPrefix);
3407
3415
  });
@@ -4096,11 +4104,15 @@ class Encoder {
4096
4104
  }
4097
4105
  for (let j = 0; j < numChanges; j++) {
4098
4106
  const fieldIndex = changeSet.operations[j];
4099
- const operation = (fieldIndex < 0)
4100
- ? Math.abs(fieldIndex) // "pure" operation without fieldIndex (e.g. CLEAR, REVERSE, etc.)
4101
- : (isEncodeAll)
4102
- ? exports.OPERATION.ADD
4103
- : changeTree.indexedOperations[fieldIndex];
4107
+ if (fieldIndex < 0) {
4108
+ // "pure" operation without fieldIndex (e.g. CLEAR, REVERSE, etc.)
4109
+ // encode and continue early - no need to reach $filter check
4110
+ buffer[it.offset++] = Math.abs(fieldIndex) & 255;
4111
+ continue;
4112
+ }
4113
+ const operation = (isEncodeAll)
4114
+ ? exports.OPERATION.ADD
4115
+ : changeTree.indexedOperations[fieldIndex];
4104
4116
  //
4105
4117
  // first pass (encodeAll), identify "filtered" operations without encoding them
4106
4118
  // they will be encoded per client, based on their view.