Skip to content

Commit f887535

Browse files
committed
fixes callbacks with .clear() operation.
1 parent bcb059c commit f887535

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@colyseus/schema",
3-
"version": "3.0.5",
3+
"version": "3.0.6",
44
"description": "Binary state serializer with delta encoding for games",
55
"bin": {
66
"schema-codegen": "./bin/schema-codegen",

src/decoder/Decoder.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,12 @@ export class Decoder<T extends Schema = any> {
132132
}
133133

134134
removeChildRefs(ref: Collection, allChanges: DataChange[]) {
135-
const changeTree = ref[$changes];
136-
137135
const needRemoveRef = typeof (ref[$childType]) !== "string";
138-
const refId = changeTree.refId;
136+
const refId = this.root.refIds.get(ref as Ref);
139137

140138
ref.forEach((value: any, key: any) => {
141139
allChanges.push({
142-
ref: value,
140+
ref: ref as Ref,
143141
refId,
144142
op: OPERATION.DELETE,
145143
field: key,

test/ArraySchema.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,45 @@ describe("ArraySchema Tests", () => {
17311731
});
17321732
});
17331733

1734+
it("clear should trigger onRemove + onChange", () => {
1735+
const state = new State();
1736+
state.points.push(
1737+
new Point().assign({ x: 0, y: 0 }),
1738+
new Point().assign({ x: 1, y: 1 }),
1739+
new Point().assign({ x: 2, y: 2 }),
1740+
);
1741+
1742+
const decodedState = new State();
1743+
const $ = getCallbacks(decodedState);
1744+
1745+
let onAddCallCount = 0;
1746+
let onRemoveCallCount = 0;
1747+
let onChangeCallCount = 0;
1748+
1749+
$(decodedState).points.onAdd(() => { onAddCallCount++; });
1750+
$(decodedState).points.onRemove(() => { onRemoveCallCount++; });
1751+
$(decodedState).points.onChange(() => { onChangeCallCount++; });
1752+
1753+
let encoded = state.encodeAll();
1754+
decodedState.decode(encoded);
1755+
assert.strictEqual(3, decodedState.points.length);
1756+
assert.strictEqual(3, onAddCallCount);
1757+
assert.strictEqual(0, onRemoveCallCount);
1758+
assert.strictEqual(3, onChangeCallCount);
1759+
1760+
state.points.clear();
1761+
1762+
encoded = state.encode();
1763+
decodedState.decode(encoded);
1764+
assert.strictEqual(0, decodedState.points.length);
1765+
1766+
assert.strictEqual(3, onAddCallCount);
1767+
assert.strictEqual(3, onRemoveCallCount);
1768+
assert.strictEqual(6, onChangeCallCount);
1769+
1770+
assertDeepStrictEqualEncodeAll(state);
1771+
});
1772+
17341773
xit("should trigger onAdd callback only once after clearing and adding one item", () => {
17351774
const state = new State();
17361775
const decodedState = new State();

0 commit comments

Comments
 (0)