Skip to content

Commit 573200e

Browse files
committed
fix attaching callbacks to immediatelly available collections
1 parent 2c3a097 commit 573200e

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
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.7",
3+
"version": "3.0.8",
44
"description": "Binary state serializer with delta encoding for games",
55
"bin": {
66
"schema-codegen": "./bin/schema-codegen",

src/decoder/strategy/StateCallbacks.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ export function getDecoderStateCallbacks<T extends Schema>(decoder: Decoder<T>):
107107
let currentOnAddCallback: Function | undefined;
108108

109109
decoder.triggerChanges = function (allChanges: DataChange[]) {
110+
// console.log("ALL CHANGES =>", allChanges);
111+
110112
const uniqueRefIds = new Set<number>();
111113

112114
for (let i = 0, l = allChanges.length; i < l; i++) {
@@ -290,7 +292,9 @@ export function getDecoderStateCallbacks<T extends Schema>(decoder: Decoder<T>):
290292
const instance = context.instance?.[prop];
291293
const onInstanceAvailable: OnInstanceAvailableCallback = (
292294
(callback: (ref: Ref, existing: boolean) => void) => {
295+
console.log("onInstanceAvailable!!");
293296
const unbind = $(context.instance).listen(prop, (value, _) => {
297+
console.log("prop changed!", prop);
294298
callback(value, false);
295299

296300
// FIXME: by "unbinding" the callback here,
@@ -306,6 +310,7 @@ export function getDecoderStateCallbacks<T extends Schema>(decoder: Decoder<T>):
306310
}
307311
}
308312
);
313+
309314
return getProxy(metadataField.type, {
310315
// make sure refId is available, otherwise need to wait for the instance to be available.
311316
instance: ($root.refIds.get(instance) && instance),
@@ -344,10 +349,12 @@ export function getDecoderStateCallbacks<T extends Schema>(decoder: Decoder<T>):
344349
};
345350

346351
const onRemove = function (ref: Ref, callback: (value: any, key: any) => void) {
352+
console.log("REGISTER ON REMOVE ON", $root.refIds.get(ref));
347353
return $root.addCallback($root.refIds.get(ref), OPERATION.DELETE, callback);
348354
};
349355

350356
const onChange = function (ref: Ref, callback: (value: any, key: any) => void) {
357+
console.log("REGISTER ON CHANGE ON", $root.refIds.get(ref));
351358
return $root.addCallback($root.refIds.get(ref), OPERATION.REPLACE, callback);
352359
};
353360

@@ -373,7 +380,10 @@ export function getDecoderStateCallbacks<T extends Schema>(decoder: Decoder<T>):
373380
}
374381
},
375382
onRemove: function(callback: (value, key) => void) {
376-
if (context.onInstanceAvailable) {
383+
if (context.instance) {
384+
return onRemove(context.instance, callback);
385+
386+
} else if (context.onInstanceAvailable) {
377387
// collection instance not received yet
378388
let detachCallback = () => {};
379389

@@ -382,13 +392,13 @@ export function getDecoderStateCallbacks<T extends Schema>(decoder: Decoder<T>):
382392
});
383393

384394
return () => detachCallback();
385-
386-
} else if (context.instance) {
387-
return onRemove(context.instance, callback);
388395
}
389396
},
390397
onChange: function(callback: (value, key) => void) {
391-
if (context.onInstanceAvailable) {
398+
if (context.instance) {
399+
return onChange(context.instance, callback);
400+
401+
} else if (context.onInstanceAvailable) {
392402
// collection instance not received yet
393403
let detachCallback = () => {};
394404

@@ -397,9 +407,6 @@ export function getDecoderStateCallbacks<T extends Schema>(decoder: Decoder<T>):
397407
});
398408

399409
return () => detachCallback();
400-
401-
} else if (context.instance) {
402-
return onChange(context.instance, callback);
403410
}
404411
},
405412
}, {

test/ArraySchema.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,36 @@ describe("ArraySchema Tests", () => {
17701770
assertDeepStrictEqualEncodeAll(state);
17711771
});
17721772

1773+
it("clear should trigger callbacks properly", () => {
1774+
const state = new State();
1775+
const decodedState = new State();
1776+
1777+
decodedState.decode(state.encodeAll());
1778+
const $ = getCallbacks(decodedState);
1779+
1780+
let onAddCallCount = 0;
1781+
let onRemoveCallCount = 0;
1782+
let onChangeCallCount = 0;
1783+
$(decodedState).points.onAdd(() => { onAddCallCount++; });
1784+
$(decodedState).points.onRemove(() => { onRemoveCallCount++; });
1785+
$(decodedState).points.onChange(() => { onChangeCallCount++; });
1786+
1787+
state.points.push(new Point().assign({ x: 0, y: 0 }));
1788+
state.points.push(new Point().assign({ x: 1, y: 1 }));
1789+
decodedState.decode(state.encode());
1790+
1791+
assert.strictEqual(2, onAddCallCount);
1792+
assert.strictEqual(0, onRemoveCallCount);
1793+
assert.strictEqual(2, onChangeCallCount);
1794+
1795+
state.points.clear();
1796+
decodedState.decode(state.encode());
1797+
1798+
assert.strictEqual(2, onAddCallCount);
1799+
assert.strictEqual(2, onRemoveCallCount);
1800+
assert.strictEqual(4, onChangeCallCount);
1801+
});
1802+
17731803
xit("should trigger onAdd callback only once after clearing and adding one item", () => {
17741804
const state = new State();
17751805
const decodedState = new State();

0 commit comments

Comments
 (0)