@colyseus/schema 3.0.25 → 3.0.27
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 +81 -38
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +81 -38
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +81 -40
- package/lib/encoder/ChangeTree.d.ts +3 -2
- package/lib/encoder/ChangeTree.js +47 -13
- package/lib/encoder/ChangeTree.js.map +1 -1
- package/lib/encoder/EncodeOperation.js +1 -0
- package/lib/encoder/EncodeOperation.js.map +1 -1
- package/lib/encoder/Encoder.d.ts +2 -1
- package/lib/encoder/Encoder.js +3 -3
- package/lib/encoder/Encoder.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js.map +1 -1
- package/lib/types/custom/ArraySchema.js +47 -22
- package/lib/types/custom/ArraySchema.js.map +1 -1
- package/package.json +1 -1
- package/src/encoder/ChangeTree.ts +54 -15
- package/src/encoder/EncodeOperation.ts +2 -0
- package/src/encoder/Encoder.ts +5 -5
- package/src/index.ts +1 -1
- package/src/types/custom/ArraySchema.ts +56 -25
package/build/esm/index.mjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import 'util';
|
|
2
|
+
|
|
1
3
|
const SWITCH_TO_STRUCTURE = 255; // (decoding collides with DELETE_AND_ADD + fieldIndex = 63)
|
|
2
4
|
const TYPE_ID = 213;
|
|
3
5
|
/**
|
|
@@ -971,10 +973,18 @@ function setOperationAtIndex(changeSet, index) {
|
|
|
971
973
|
}
|
|
972
974
|
}
|
|
973
975
|
function deleteOperationAtIndex(changeSet, index) {
|
|
974
|
-
|
|
975
|
-
if (operationsIndex
|
|
976
|
-
|
|
976
|
+
let operationsIndex = changeSet.indexes[index];
|
|
977
|
+
if (operationsIndex === undefined) {
|
|
978
|
+
//
|
|
979
|
+
// if index is not found, we need to find the last operation
|
|
980
|
+
// FIXME: this is not very efficient
|
|
981
|
+
//
|
|
982
|
+
// > See "should allow consecutive splices (same place)" tests
|
|
983
|
+
//
|
|
984
|
+
operationsIndex = Object.values(changeSet.indexes).at(-1);
|
|
985
|
+
index = Object.entries(changeSet.indexes).find(([_, value]) => value === operationsIndex)?.[0];
|
|
977
986
|
}
|
|
987
|
+
changeSet.operations[operationsIndex] = undefined;
|
|
978
988
|
delete changeSet.indexes[index];
|
|
979
989
|
}
|
|
980
990
|
function enqueueChangeTree(root, changeTree, changeSet, queueRootIndex = changeTree[changeSet].queueRootIndex) {
|
|
@@ -1137,7 +1147,7 @@ class ChangeTree {
|
|
|
1137
1147
|
const newIndexes = {};
|
|
1138
1148
|
for (const index in this.indexedOperations) {
|
|
1139
1149
|
newIndexedOperations[Number(index) + shiftIndex] = this.indexedOperations[index];
|
|
1140
|
-
newIndexes[Number(index) + shiftIndex] = changeSet[index];
|
|
1150
|
+
newIndexes[Number(index) + shiftIndex] = changeSet.indexes[index];
|
|
1141
1151
|
}
|
|
1142
1152
|
this.indexedOperations = newIndexedOperations;
|
|
1143
1153
|
changeSet.indexes = newIndexes;
|
|
@@ -1159,15 +1169,22 @@ class ChangeTree {
|
|
|
1159
1169
|
}
|
|
1160
1170
|
_shiftAllChangeIndexes(shiftIndex, startIndex = 0, changeSet) {
|
|
1161
1171
|
const newIndexes = {};
|
|
1172
|
+
let newKey = 0;
|
|
1162
1173
|
for (const key in changeSet.indexes) {
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
}
|
|
1174
|
+
newIndexes[newKey++] = changeSet.indexes[key];
|
|
1175
|
+
}
|
|
1176
|
+
// const newIndexes = {};
|
|
1177
|
+
// let newKey = 0;
|
|
1178
|
+
// for (const key in changeSet.indexes) {
|
|
1179
|
+
// const index = changeSet.indexes[key];
|
|
1180
|
+
// newIndexes[newKey++] = changeSet.indexes[key];
|
|
1181
|
+
// console.log("...shiftAllChangeIndexes", { index: key, targetIndex: index, startIndex, shiftIndex });
|
|
1182
|
+
// if (index > startIndex) {
|
|
1183
|
+
// newIndexes[Number(key) + shiftIndex] = index;
|
|
1184
|
+
// } else {
|
|
1185
|
+
// newIndexes[Number(key)] = index;
|
|
1186
|
+
// }
|
|
1187
|
+
// }
|
|
1171
1188
|
changeSet.indexes = newIndexes;
|
|
1172
1189
|
for (let i = 0; i < changeSet.operations.length; i++) {
|
|
1173
1190
|
const index = changeSet.operations[i];
|
|
@@ -1231,6 +1248,7 @@ class ChangeTree {
|
|
|
1231
1248
|
: this.changes;
|
|
1232
1249
|
this.indexedOperations[index] = operation ?? OPERATION.DELETE;
|
|
1233
1250
|
setOperationAtIndex(changeSet, index);
|
|
1251
|
+
deleteOperationAtIndex(this.allChanges, allChangesIndex);
|
|
1234
1252
|
const previousValue = this.getValue(index);
|
|
1235
1253
|
// remove `root` reference
|
|
1236
1254
|
if (previousValue && previousValue[$changes]) {
|
|
@@ -1246,7 +1264,6 @@ class ChangeTree {
|
|
|
1246
1264
|
//
|
|
1247
1265
|
this.root?.remove(previousValue[$changes]);
|
|
1248
1266
|
}
|
|
1249
|
-
deleteOperationAtIndex(this.allChanges, allChangesIndex);
|
|
1250
1267
|
//
|
|
1251
1268
|
// FIXME: this is looking a ugly and repeated
|
|
1252
1269
|
//
|
|
@@ -1510,6 +1527,7 @@ const encodeArray = function (encoder, bytes, changeTree, field, operation, it,
|
|
|
1510
1527
|
}
|
|
1511
1528
|
const type = changeTree.getType(field);
|
|
1512
1529
|
const value = changeTree.getValue(field, isEncodeAll);
|
|
1530
|
+
// console.log({ type, field, value });
|
|
1513
1531
|
// console.log("encodeArray -> ", {
|
|
1514
1532
|
// ref: changeTree.ref.constructor.name,
|
|
1515
1533
|
// field,
|
|
@@ -1988,8 +2006,6 @@ class ArraySchema {
|
|
|
1988
2006
|
return undefined;
|
|
1989
2007
|
}
|
|
1990
2008
|
this[$changes].delete(index, undefined, this.items.length - 1);
|
|
1991
|
-
// this.tmpItems[index] = undefined;
|
|
1992
|
-
// this.tmpItems.pop();
|
|
1993
2009
|
this.deletedIndexes[index] = true;
|
|
1994
2010
|
return this.items.pop();
|
|
1995
2011
|
}
|
|
@@ -2099,10 +2115,11 @@ class ArraySchema {
|
|
|
2099
2115
|
return undefined;
|
|
2100
2116
|
}
|
|
2101
2117
|
// const index = Number(Object.keys(changeTree.indexes)[0]);
|
|
2102
|
-
const index = this.tmpItems.findIndex((item, i) => item === this.items[0]);
|
|
2103
2118
|
const changeTree = this[$changes];
|
|
2104
|
-
|
|
2105
|
-
|
|
2119
|
+
const index = this.tmpItems.findIndex(item => item === this.items[0]);
|
|
2120
|
+
const allChangesIndex = this.items.findIndex(item => item === this.items[0]);
|
|
2121
|
+
changeTree.delete(index, OPERATION.DELETE, allChangesIndex);
|
|
2122
|
+
changeTree.shiftAllChangeIndexes(-1, allChangesIndex);
|
|
2106
2123
|
// this.deletedIndexes[index] = true;
|
|
2107
2124
|
return this.items.shift();
|
|
2108
2125
|
}
|
|
@@ -2139,31 +2156,50 @@ class ArraySchema {
|
|
|
2139
2156
|
* @param deleteCount The number of elements to remove.
|
|
2140
2157
|
* @param insertItems Elements to insert into the array in place of the deleted elements.
|
|
2141
2158
|
*/
|
|
2142
|
-
splice(start, deleteCount
|
|
2159
|
+
splice(start, deleteCount, ...insertItems) {
|
|
2143
2160
|
const changeTree = this[$changes];
|
|
2161
|
+
const itemsLength = this.items.length;
|
|
2144
2162
|
const tmpItemsLength = this.tmpItems.length;
|
|
2145
2163
|
const insertCount = insertItems.length;
|
|
2146
2164
|
// build up-to-date list of indexes, excluding removed values.
|
|
2147
2165
|
const indexes = [];
|
|
2148
2166
|
for (let i = 0; i < tmpItemsLength; i++) {
|
|
2149
|
-
// if (this.tmpItems[i] !== undefined) {
|
|
2150
2167
|
if (this.deletedIndexes[i] !== true) {
|
|
2151
2168
|
indexes.push(i);
|
|
2152
2169
|
}
|
|
2153
2170
|
}
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2171
|
+
if (itemsLength > start) {
|
|
2172
|
+
// if deleteCount is not provided, delete all items from start to end
|
|
2173
|
+
if (deleteCount === undefined) {
|
|
2174
|
+
deleteCount = itemsLength - start;
|
|
2175
|
+
}
|
|
2176
|
+
//
|
|
2177
|
+
// delete operations at correct index
|
|
2178
|
+
//
|
|
2179
|
+
for (let i = start; i < start + deleteCount; i++) {
|
|
2180
|
+
const index = indexes[i];
|
|
2181
|
+
changeTree.delete(index, OPERATION.DELETE);
|
|
2182
|
+
this.deletedIndexes[index] = true;
|
|
2183
|
+
}
|
|
2160
2184
|
}
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2185
|
+
else {
|
|
2186
|
+
// not enough items to delete
|
|
2187
|
+
deleteCount = 0;
|
|
2188
|
+
}
|
|
2189
|
+
// insert operations
|
|
2190
|
+
if (insertCount > 0) {
|
|
2191
|
+
if (insertCount > deleteCount) {
|
|
2192
|
+
console.error("Inserting more elements than deleting during ArraySchema#splice()");
|
|
2193
|
+
throw new Error("ArraySchema#splice(): insertCount must be equal or lower than deleteCount.");
|
|
2194
|
+
}
|
|
2195
|
+
for (let i = 0; i < insertCount; i++) {
|
|
2196
|
+
const addIndex = (indexes[start] ?? itemsLength) + i;
|
|
2197
|
+
changeTree.indexedOperation(addIndex, (this.deletedIndexes[addIndex])
|
|
2198
|
+
? OPERATION.DELETE_AND_ADD
|
|
2199
|
+
: OPERATION.ADD);
|
|
2200
|
+
// set value's parent/root
|
|
2201
|
+
insertItems[i][$changes]?.setParent(this, changeTree.root, addIndex);
|
|
2202
|
+
}
|
|
2167
2203
|
}
|
|
2168
2204
|
//
|
|
2169
2205
|
// delete exceeding indexes from "allChanges"
|
|
@@ -2171,6 +2207,16 @@ class ArraySchema {
|
|
|
2171
2207
|
//
|
|
2172
2208
|
if (deleteCount > insertCount) {
|
|
2173
2209
|
changeTree.shiftAllChangeIndexes(-(deleteCount - insertCount), indexes[start + insertCount]);
|
|
2210
|
+
// debugChangeSet("AFTER SHIFT indexes", changeTree.allChanges);
|
|
2211
|
+
}
|
|
2212
|
+
//
|
|
2213
|
+
// FIXME: this code block is duplicated on ChangeTree
|
|
2214
|
+
//
|
|
2215
|
+
if (changeTree.filteredChanges !== undefined) {
|
|
2216
|
+
enqueueChangeTree(changeTree.root, changeTree, 'filteredChanges');
|
|
2217
|
+
}
|
|
2218
|
+
else {
|
|
2219
|
+
enqueueChangeTree(changeTree.root, changeTree, 'changes');
|
|
2174
2220
|
}
|
|
2175
2221
|
return this.items.splice(start, deleteCount, ...insertItems);
|
|
2176
2222
|
}
|
|
@@ -2426,9 +2472,6 @@ class ArraySchema {
|
|
|
2426
2472
|
: this.deletedIndexes[index]
|
|
2427
2473
|
? this.items[index]
|
|
2428
2474
|
: this.tmpItems[index] || this.items[index];
|
|
2429
|
-
// return (isEncodeAll)
|
|
2430
|
-
// ? this.items[index]
|
|
2431
|
-
// : this.tmpItems[index] ?? this.items[index];
|
|
2432
2475
|
}
|
|
2433
2476
|
[$deleteByIndex](index) {
|
|
2434
2477
|
this.items[index] = undefined;
|
|
@@ -3800,10 +3843,10 @@ class Encoder {
|
|
|
3800
3843
|
view.invisible.delete(changeTree); // remove from invisible list
|
|
3801
3844
|
}
|
|
3802
3845
|
}
|
|
3803
|
-
const
|
|
3846
|
+
const changeSet = changeTree[changeSetName];
|
|
3804
3847
|
const ref = changeTree.ref;
|
|
3805
3848
|
// TODO: avoid iterating over change tree if no changes were made
|
|
3806
|
-
const numChanges =
|
|
3849
|
+
const numChanges = changeSet.operations.length;
|
|
3807
3850
|
if (numChanges === 0) {
|
|
3808
3851
|
continue;
|
|
3809
3852
|
}
|
|
@@ -3818,7 +3861,7 @@ class Encoder {
|
|
|
3818
3861
|
encode.number(buffer, changeTree.refId, it);
|
|
3819
3862
|
}
|
|
3820
3863
|
for (let j = 0; j < numChanges; j++) {
|
|
3821
|
-
const fieldIndex =
|
|
3864
|
+
const fieldIndex = changeSet.operations[j];
|
|
3822
3865
|
const operation = (fieldIndex < 0)
|
|
3823
3866
|
? Math.abs(fieldIndex) // "pure" operation without fieldIndex (e.g. CLEAR, REVERSE, etc.)
|
|
3824
3867
|
: (isEncodeAll)
|