Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-using schema instances on many places & missing callbacks #151

Open
endel opened this issue Aug 22, 2023 · 0 comments
Open

Re-using schema instances on many places & missing callbacks #151

endel opened this issue Aug 22, 2023 · 0 comments
Labels
bug Something isn't working callbacks

Comments

@endel
Copy link
Member

endel commented Aug 22, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working callbacks
Projects
None yet
Development

No branches or pull requests

1 participant