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

ArraySchema: CLEAR + PUSH results in onAdd called twice #107

Open
endel opened this issue Apr 28, 2021 · 0 comments
Open

ArraySchema: CLEAR + PUSH results in onAdd called twice #107

endel opened this issue Apr 28, 2021 · 0 comments
Labels
bug Something isn't working

Comments

@endel
Copy link
Member

endel commented Apr 28, 2021

Due to how new connections receive the full state through .encodeAll(), and incoming patches through .encode() right after, it is a known issue that onAdd is going to be called twice when a client joins a room on this scenario:

state.points.clear();
state.points.push(new Point().assign({ x: 2, y: 2 }));
state.points.push(new Point().assign({ x: 3, y: 3 }));
  • When the client joins a room, he's going to receive the FULL STATE through state.encodeAll(). This includes the OPERATION.CLEAR on the array.
  • The next patch (via state.encode()) is also going to include the OPERATION.CLEAR - thus causing the array items to be included again.

Related issue: colyseus/colyseus#249

Here's a test-case reproducing this problem:

schema/test/ArraySchemaTest.ts

Lines 1293 to 1311 in 448870a

xit("should trigger onAdd callback only once after clearing and adding one item", () => {
const state = new State();
const decodedState = new State();
// state.points.push(new Point().assign({ x: 0, y: 0 }));
// state.points.push(new Point().assign({ x: 1, y: 1 }));
state.points.clear();
state.points.push(new Point().assign({ x: 2, y: 2 }));
state.points.push(new Point().assign({ x: 3, y: 3 }));
decodedState.points.onAdd = (point, key) => console.log(point.toJSON(), key);
const onAddSpy = sinon.spy(decodedState.points, 'onAdd');
decodedState.decode(state.encodeAll());
decodedState.decode(state.encode());
sinon.assert.callCount(onAddSpy, 2);
});

@endel endel added the bug Something isn't working label Apr 28, 2021
endel added a commit that referenced this issue Apr 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant