@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.
- package/build/cjs/index.js +37 -25
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +37 -25
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +37 -25
- package/lib/Schema.js +6 -1
- package/lib/Schema.js.map +1 -1
- package/lib/annotations.d.ts +1 -1
- package/lib/annotations.js +21 -9
- package/lib/annotations.js.map +1 -1
- package/lib/encoder/ChangeTree.js +1 -1
- package/lib/encoder/ChangeTree.js.map +1 -1
- package/lib/encoder/EncodeOperation.js +0 -9
- package/lib/encoder/EncodeOperation.js.map +1 -1
- package/lib/encoder/Encoder.js +9 -5
- package/lib/encoder/Encoder.js.map +1 -1
- package/package.json +1 -1
- package/src/Schema.ts +6 -1
- package/src/annotations.ts +24 -10
- package/src/encoder/ChangeTree.ts +1 -1
- package/src/encoder/EncodeOperation.ts +0 -13
- package/src/encoder/Encoder.ts +10 -5
package/build/esm/index.mjs
CHANGED
|
@@ -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(
|
|
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
|
|
3185
|
-
const
|
|
3186
|
-
if (typeof (
|
|
3187
|
-
if (
|
|
3188
|
-
defaultValues[fieldName] =
|
|
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 (
|
|
3191
|
-
viewTagFields[fieldName] = (typeof (
|
|
3183
|
+
if (value['view'] !== undefined) {
|
|
3184
|
+
viewTagFields[fieldName] = (typeof (value['view']) === "boolean")
|
|
3192
3185
|
? DEFAULT_VIEW_TAG
|
|
3193
|
-
:
|
|
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,
|
|
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
|
-
|
|
4098
|
-
|
|
4099
|
-
|
|
4100
|
-
|
|
4101
|
-
|
|
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.
|