@colyseus/schema 5.0.12 → 5.0.13
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/encoder/ChangeTree.d.ts +3 -0
- package/build/encoder/changeTree/parentChain.d.ts +9 -0
- package/build/index.cjs +75 -1
- package/build/index.cjs.map +1 -1
- package/build/index.js +75 -1
- package/build/index.mjs +75 -1
- package/build/index.mjs.map +1 -1
- package/build/types/custom/ArraySchema.d.ts +17 -0
- package/package.json +1 -1
- package/src/encoder/ChangeTree.ts +5 -0
- package/src/encoder/changeTree/parentChain.ts +29 -0
- package/src/types/custom/ArraySchema.ts +40 -1
|
@@ -63,6 +63,23 @@ export declare class ArraySchema<V = any> implements Array<V>, Collection<number
|
|
|
63
63
|
* land on the wrong wire slots.
|
|
64
64
|
*/
|
|
65
65
|
protected $wireIndex(index: number): number;
|
|
66
|
+
/**
|
|
67
|
+
* Re-point children at their wire slot. `ChangeTree._parentIndex` caches
|
|
68
|
+
* the slot a child holds in `tmpItems`, and StateView addresses per-view
|
|
69
|
+
* ADD/DELETE with it — so a reorder that leaves it behind aims those ops
|
|
70
|
+
* at whichever element inherited the slot (issue #231).
|
|
71
|
+
*
|
|
72
|
+
* The filter check is a correctness boundary, not a tunable: StateView is
|
|
73
|
+
* the only reader and reaches the index only through a filtered array
|
|
74
|
+
* (`addParentOf` bails on `hasFilteredFields`, `remove` on the child's
|
|
75
|
+
* `isFiltered`). Everything else stops at the flag read instead of walking
|
|
76
|
+
* its children every tick.
|
|
77
|
+
*
|
|
78
|
+
* Callers name the lowest slot that moved as `from`. Compaction cannot, so
|
|
79
|
+
* it hands over the pre-compaction layout as `staged` and the unchanged
|
|
80
|
+
* prefix is skipped instead. Either way tail churn walks nothing.
|
|
81
|
+
*/
|
|
82
|
+
protected $reindexChildren(from: number, staged?: V[]): void;
|
|
66
83
|
protected $changeAt(index: number, value: V): number | undefined;
|
|
67
84
|
protected $deleteAt(index: number, operation?: OPERATION): void;
|
|
68
85
|
protected $setAt(index: number, value: V, operation: OPERATION): void;
|
package/package.json
CHANGED
|
@@ -34,6 +34,7 @@ import type { DecodeOperation } from "../decoder/DecodeOperation.js";
|
|
|
34
34
|
|
|
35
35
|
import {
|
|
36
36
|
addParent as _addParent, removeParent as _removeParent,
|
|
37
|
+
setParentIndex as _setParentIndex,
|
|
37
38
|
findParent as _findParent, hasParent as _hasParent,
|
|
38
39
|
getAllParents as _getAllParents,
|
|
39
40
|
} from "./changeTree/parentChain.js";
|
|
@@ -804,6 +805,9 @@ export class ChangeTree<T extends Ref = any> implements ChangeRecorder {
|
|
|
804
805
|
|
|
805
806
|
addParent(parent: Ref, index: number): void { _addParent(this, parent, index); }
|
|
806
807
|
|
|
808
|
+
/** Re-point an existing parent's cached index after the parent reindexed. */
|
|
809
|
+
setParentIndex(parent: Ref, index: number): void { _setParentIndex(this, parent, index); }
|
|
810
|
+
|
|
807
811
|
/** @returns true if parent was found and removed */
|
|
808
812
|
removeParent(parent: Ref = this.parent): boolean { return _removeParent(this, parent); }
|
|
809
813
|
|
|
@@ -856,6 +860,7 @@ export class UntrackedChangeTree {
|
|
|
856
860
|
operation(): void {}
|
|
857
861
|
setParent(): void {}
|
|
858
862
|
addParent(): void {}
|
|
863
|
+
setParentIndex(): void {}
|
|
859
864
|
removeParent(): boolean { return false; }
|
|
860
865
|
getChange(): number { return 0; }
|
|
861
866
|
discard(): void {}
|
|
@@ -45,6 +45,35 @@ export function addParent(tree: ChangeTree, parent: Ref, index: number): void {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
/**
|
|
49
|
+
* Move `parent`'s existing chain entry to `index`, skipping the attachment
|
|
50
|
+
* work `addParent` does. `parent` must already be a parent of `tree`.
|
|
51
|
+
*
|
|
52
|
+
* Called by collections whose wire slots shift (ArraySchema): StateView
|
|
53
|
+
* addresses per-view ADD/DELETE by that index, so it has to follow the
|
|
54
|
+
* element it names.
|
|
55
|
+
*/
|
|
56
|
+
export function setParentIndex(tree: ChangeTree, parent: Ref, index: number): void {
|
|
57
|
+
if (tree.extraParents === undefined) {
|
|
58
|
+
tree._parentIndex = index; // sole parent, so it is `parent`
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
// Shared instance — move only the entry `parent` owns. Matching goes
|
|
62
|
+
// through `$changes` because ArraySchema arrives proxied (see removeParent
|
|
63
|
+
// below), and `extraParents` only ever fills by demoting `parentRef`, so
|
|
64
|
+
// the inline parent is set here.
|
|
65
|
+
if (tree.parentRef[$changes] === parent[$changes]) {
|
|
66
|
+
tree._parentIndex = index;
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
for (let entry = tree.extraParents; entry !== undefined; entry = entry.next) {
|
|
70
|
+
if (entry.ref[$changes] === parent[$changes]) {
|
|
71
|
+
entry.index = index;
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
48
77
|
/**
|
|
49
78
|
* Remove a parent from the chain.
|
|
50
79
|
* @returns true if parent was found and removed (Root.remove relies on this).
|
|
@@ -331,6 +331,35 @@ export class ArraySchema<V = any> implements Array<V>, Collection<number, V>, IR
|
|
|
331
331
|
return tmpItems.length + (index - live);
|
|
332
332
|
}
|
|
333
333
|
|
|
334
|
+
/**
|
|
335
|
+
* Re-point children at their wire slot. `ChangeTree._parentIndex` caches
|
|
336
|
+
* the slot a child holds in `tmpItems`, and StateView addresses per-view
|
|
337
|
+
* ADD/DELETE with it — so a reorder that leaves it behind aims those ops
|
|
338
|
+
* at whichever element inherited the slot (issue #231).
|
|
339
|
+
*
|
|
340
|
+
* The filter check is a correctness boundary, not a tunable: StateView is
|
|
341
|
+
* the only reader and reaches the index only through a filtered array
|
|
342
|
+
* (`addParentOf` bails on `hasFilteredFields`, `remove` on the child's
|
|
343
|
+
* `isFiltered`). Everything else stops at the flag read instead of walking
|
|
344
|
+
* its children every tick.
|
|
345
|
+
*
|
|
346
|
+
* Callers name the lowest slot that moved as `from`. Compaction cannot, so
|
|
347
|
+
* it hands over the pre-compaction layout as `staged` and the unchanged
|
|
348
|
+
* prefix is skipped instead. Either way tail churn walks nothing.
|
|
349
|
+
*/
|
|
350
|
+
protected $reindexChildren(from: number, staged?: V[]) {
|
|
351
|
+
if (!this[$changes].hasFilteredFields) { return; } // nothing will read the cache
|
|
352
|
+
if (typeof this[$childType] === "string") { return; } // primitives have no child tree
|
|
353
|
+
const tmpItems = this.tmpItems;
|
|
354
|
+
const length = tmpItems.length;
|
|
355
|
+
if (staged !== undefined) {
|
|
356
|
+
while (from < length && tmpItems[from] === staged[from]) { from++; }
|
|
357
|
+
}
|
|
358
|
+
for (let i = from; i < length; i++) {
|
|
359
|
+
tmpItems[i]?.[$changes]?.setParentIndex(this, i);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
334
363
|
// encoding only. Returns the wire index the change was recorded at
|
|
335
364
|
// (undefined when nothing was recorded).
|
|
336
365
|
protected $changeAt(index: number, value: V): number | undefined {
|
|
@@ -460,6 +489,7 @@ export class ArraySchema<V = any> implements Array<V>, Collection<number, V>, IR
|
|
|
460
489
|
self[$changes].operation(OPERATION.REVERSE);
|
|
461
490
|
self.items.reverse();
|
|
462
491
|
self.tmpItems.reverse();
|
|
492
|
+
self.$reindexChildren(0);
|
|
463
493
|
return this;
|
|
464
494
|
}
|
|
465
495
|
|
|
@@ -515,6 +545,7 @@ export class ArraySchema<V = any> implements Array<V>, Collection<number, V>, IR
|
|
|
515
545
|
sortedItems.forEach((_, i) => changeTree.change(i, OPERATION.REPLACE));
|
|
516
546
|
|
|
517
547
|
self.tmpItems.sort(compareFn);
|
|
548
|
+
self.$reindexChildren(0);
|
|
518
549
|
|
|
519
550
|
self.isMovingItems = false;
|
|
520
551
|
return this;
|
|
@@ -620,6 +651,7 @@ export class ArraySchema<V = any> implements Array<V>, Collection<number, V>, IR
|
|
|
620
651
|
}
|
|
621
652
|
|
|
622
653
|
self.tmpItems.unshift(...items);
|
|
654
|
+
self.$reindexChildren(items.length); // survivors only — the loop above placed the new items
|
|
623
655
|
|
|
624
656
|
return self.items.unshift(...items);
|
|
625
657
|
}
|
|
@@ -938,8 +970,15 @@ export class ArraySchema<V = any> implements Array<V>, Collection<number, V>, IR
|
|
|
938
970
|
|
|
939
971
|
protected [$onEncodeEnd]() {
|
|
940
972
|
const self = this[$proxyTarget] ?? this;
|
|
973
|
+
const staged = self.tmpItems;
|
|
941
974
|
self.tmpItems = self.items.slice();
|
|
942
|
-
|
|
975
|
+
|
|
976
|
+
if (self.deletedIndexes.length > 0) {
|
|
977
|
+
// compaction just closed the staged holes — everything above the
|
|
978
|
+
// lowest one slid down a slot
|
|
979
|
+
self.$reindexChildren(0, staged);
|
|
980
|
+
self.deletedIndexes.length = 0;
|
|
981
|
+
}
|
|
943
982
|
}
|
|
944
983
|
|
|
945
984
|
protected [$onDecodeEnd]() {
|