Skip to content

Commit 2cbfa45

Browse files
committed
StateView: fix removing child Schema instances from visible set
1 parent 40bee24 commit 2cbfa45

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

src/encoder/StateView.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ export class StateView {
237237
// DELETE / DELETE BY REF ID
238238
changes[changeTree.parentIndex] = OPERATION.DELETE;
239239

240+
// Remove child schema from visible set
241+
changeTree.forEachChild((childChangeTree) => {
242+
this.visible.delete(childChangeTree);
243+
});
244+
240245
} else {
241246
// delete all "tagged" properties.
242247
metadata?.[$viewFieldIndexes]?.forEach((index) =>

test/StateView.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,41 @@ describe("StateView", () => {
11221122

11231123
assert.deepStrictEqual(client.state.items.get("1").toJSON(), item1.toJSON());
11241124
});
1125+
1126+
it("view.remove() should remove nested items", () => {
1127+
class Coordinates extends Schema {
1128+
@type("number") x: number;
1129+
@type("number") y: number;
1130+
}
1131+
class Diamond extends Schema {
1132+
@type(Coordinates) position: Coordinates;
1133+
}
1134+
class State extends Schema {
1135+
@view() @type({ map: Diamond }) diamonds = new MapSchema<Diamond>();
1136+
}
1137+
1138+
const state = new State();
1139+
const encoder = getEncoder(state);
1140+
1141+
const diamond = new Diamond().assign({
1142+
position: new Coordinates().assign({ x: 10, y: 20 })
1143+
});
1144+
state.diamonds.set("one", diamond);
1145+
1146+
const client1 = createClientWithView(state);
1147+
client1.view.add(diamond);
1148+
encodeMultiple(encoder, state, [client1]);
1149+
1150+
client1.view.remove(diamond);
1151+
encodeMultiple(encoder, state, [client1]);
1152+
1153+
diamond.position.x++;
1154+
encodeMultiple(encoder, state, [client1]);
1155+
1156+
assert.doesNotThrow(() =>
1157+
encodeAllMultiple(encoder, state, [client1]));
1158+
});
1159+
11251160
});
11261161

11271162
describe("ArraySchema", () => {

0 commit comments

Comments
 (0)