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

fix: destroy renderGroups #10566

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/scene/container/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,9 @@ export class Container<C extends ContainerChild = ContainerChild> extends EventE
oldChildren[i].destroy(options);
}
}

this.renderGroup?.destroy();
this.renderGroup = null;
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/scene/container/RenderGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,17 @@ export class RenderGroup implements Instruction
}
}

public destroy()
{
this.renderGroupParent = null;
this.root = null;
(this.childrenRenderablesToUpdate as any) = null;
(this.childrenToUpdate as any) = null;
(this.renderGroupChildren as any) = null;
(this._onRenderContainers as any) = null;
Comment on lines +227 to +230
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take this or leave it, but I think this will avoid the as any casting.

Suggested change
(this.childrenRenderablesToUpdate as any) = null;
(this.childrenToUpdate as any) = null;
(this.renderGroupChildren as any) = null;
(this._onRenderContainers as any) = null;
Object.assign(this, {
childrenRenderablesToUpdate: null,
childrenToUpdate: null,
renderGroupChildren: null,
_onRenderContainers: null,
});

this.instructionSet = null;
}

public getChildren(out: Container[] = []): Container[]
{
const children = this.root.children;
Expand Down
39 changes: 39 additions & 0 deletions tests/renderering/scene/Container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,45 @@ describe('Container', () =>
expect(container.position).toBeNull();
expect(child.position).toBeNull();
});

it('should destroy render groups', () =>
{
const container = new Container({ isRenderGroup: true });

const child = new Container({
isRenderGroup: true,
children: [new Container()],
});

const renderGroup = container.renderGroup;
const renderGroup2 = child.renderGroup;

const spy = jest.spyOn(renderGroup, 'destroy');
const spy2 = jest.spyOn(renderGroup2, 'destroy');

container.addChild(child);
container.destroy({
children: true,
});

expect(spy).toHaveBeenCalled();
expect(spy2).toHaveBeenCalled();

expect(container.renderGroup).toBeNull();
expect(child.renderGroup).toBeNull();

expect(renderGroup.renderGroupParent).toBeNull();
expect(renderGroup.childrenRenderablesToUpdate).toBeNull();
expect(renderGroup.instructionSet).toBeNull();
expect(renderGroup.renderGroupChildren).toBeNull();
expect(renderGroup['_onRenderContainers']).toBeNull();

expect(renderGroup2.renderGroupParent).toBeNull();
expect(renderGroup2.childrenRenderablesToUpdate).toBeNull();
expect(renderGroup2.instructionSet).toBeNull();
expect(renderGroup2.renderGroupChildren).toBeNull();
expect(renderGroup2['_onRenderContainers']).toBeNull();
});
});

function assertRemovedFromParent(parent: Container, container: Container, child: Container, functionToAssert: () => void)
Expand Down