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