Skip to content

Commit ee508cb

Browse files
committed
Remove entity cleanup from World.destroy()
1 parent 6995fb3 commit ee508cb

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
@@ -121,8 +121,6 @@ export class World {
121121
destroyEntity(this, this[$internal].worldEntity);
122122
this[$internal].worldEntity = null!;
123123

124-
// Destroy itself and all entities.
125-
this.entities.forEach((entity) => destroyEntity(this, entity));
126124
this.reset();
127125
this.#isInitialized = false;
128126

packages/core/tests/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();

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)