schema
schema copied to clipboard
Re-using schema instances on many places & missing callbacks
When an instance is re-used in many places, their callbacks are triggered only once, at places not easily predictable.
Failing test case
class Player extends Schema {
@type("number") hp: number;
}
class State extends Schema {
@type(Player) player1: Player;
@type(Player) player2: Player;
}
const state = new State();
const player = new Player().assign({ hp: 100 });;
state.player1 = player
state.player2 = player;
const decodedState = Reflection.decode<State>(Reflection.encode(state));
let numTriggered = 0;
decodedState.player1.listen('hp', () => numTriggered++);
decodedState.player2.listen('hp', () => numTriggered++); // THIS DOES NOT TRIGGER
decodedState.decode(state.encode());
assert.strictEqual(decodedState.player1.hp, 100);
assert.strictEqual(decodedState.player2.hp, 100);
assert.strictEqual(2, numTriggered); // ERROR: 2 !== 1
In the example above, only the 1st .listen() callback is triggered.
This issue does not happen when cloning the instances, only when sharing the same instance - which will result in the instance being shared on the client-side as well.