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: Check for removing state while adding a child #3050

Merged
merged 3 commits into from
Feb 25, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/flame/lib/src/components/core/component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ class Component {
_clearRemovingBit();
}
game.enqueueMove(child, this);
} else if (isMounted && !child.isMounted) {
} else if (isMounted && !isRemoving && !child.isMounted) {
child._parent = this;
game.enqueueAdd(child, this);
} else {
Expand Down
21 changes: 21 additions & 0 deletions packages/flame/test/components/component_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,27 @@ void main() {
expect(wrapper.contains(child), true);
},
);

testWithFlameGame('when parent is in removing state', (game) async {
final parent = Component();
final child = Component();

await game.add(parent);
await game.ready();

// Remove the parent and add the child in the same tick.
parent.removeFromParent();
await parent.add(child);

// Timeout is added because processLifecycleEvents of ComponentTreeRoot
// gets blocked in such cases.
expect(game.ready().timeout(const Duration(seconds: 2)), completes);

// Adding the parent again should eventually mount the child as well.
await game.add(parent);
await game.ready();
expect(child.isMounted, true);
});
});

group('Removing components', () {
Expand Down
Loading