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