Skip to content

Commit 73e0ffa

Browse files
authored
Merge pull request #144 from r04423/fix/world-destroy-cleanup
refactor: remove entity cleanup from World.destroy()
2 parents 4c0de9a + 2b28b0d commit 73e0ffa

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

packages/core/src/world/world.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ export class World {
131131
destroyEntity(this, this[$internal].worldEntity);
132132
this[$internal].worldEntity = null!;
133133

134-
// Destroy itself and all entities.
135-
this.entities.forEach((entity) => destroyEntity(this, entity));
136134
this.reset();
137135
this.#isInitialized = false;
138136

packages/core/tests/world.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ describe('World', () => {
4949
expect(world.entities.length).toBe(1);
5050
});
5151

52+
it('destroy should lead to entities with auto-remove relations being removed as well', () => {
53+
const Node = trait();
54+
const ChildOf = relation({ autoRemoveTarget: true, exclusive: true });
55+
56+
const world = createWorld();
57+
58+
// Create a parent node and two child nodes
59+
const parentNode = world.spawn(Node);
60+
world.spawn(Node, ChildOf(parentNode));
61+
world.spawn(Node, ChildOf(parentNode));
62+
63+
// Expect this to not throw, since the ChildOf relation will automatically
64+
// remove the child node when the parent node is destroyed first
65+
expect(() => world.destroy()).not.toThrow();
66+
});
67+
5268
it('errors if more than 16 worlds are created', () => {
5369
for (let i = 0; i < 16; i++) {
5470
createWorld();

packages/publish/tests/core/world.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ describe('World', () => {
4242
expect(world.entities.length).toBe(1);
4343
});
4444

45+
it('destroy should lead to entities with auto-remove relations being removed as well', () => {
46+
const Node = trait();
47+
const ChildOf = relation({ autoRemoveTarget: true, exclusive: true });
48+
49+
const world = createWorld();
50+
51+
// Create a parent node and two child nodes
52+
const parentNode = world.spawn(Node);
53+
world.spawn(Node, ChildOf(parentNode));
54+
world.spawn(Node, ChildOf(parentNode));
55+
56+
// Expect this to not throw, since the ChildOf relation will automatically
57+
// remove the child node when the parent node is destroyed first
58+
expect(() => world.destroy()).not.toThrow();
59+
});
60+
4561
it('errors if more than 16 worlds are created', () => {
4662
for (let i = 0; i < 16; i++) {
4763
createWorld();

0 commit comments

Comments
 (0)