Skip to content

Commit

Permalink
fix: Take into consideration when child is added to parent that is re…
Browse files Browse the repository at this point in the history
…moved in the same tick (#3428)

Currently we end up in a bad state if a child is added to a parent that
is removed in the same tick, this PR fixes that by adding the child
after the parent has been unmounted.

Closes: #3416
  • Loading branch information
spydon authored Dec 30, 2024
1 parent 1f9b0ea commit 9a5c54b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion packages/flame/lib/src/components/core/component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ class Component {
_clearRemovingBit();
}
game.enqueueMove(child, this);
} else if (isMounted && !isRemoving && !child.isMounted) {
} else if (isMounted && !child.isMounted) {
child._parent = this;
game.enqueueAdd(child, this);
} else {
Expand Down Expand Up @@ -829,6 +829,12 @@ class Component {
} else {
if (parent.isMounted && !isLoading) {
_startLoading();
} else if (parent.isRemoved) {
// This case happens when the child is added to a parent that is being
// removed in the same tick.
_parent = parent;
parent.children.add(this);
return LifecycleEventStatus.done;
}
return LifecycleEventStatus.block;
}
Expand Down
8 changes: 7 additions & 1 deletion packages/flame/test/components/component_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,13 @@ void main() {

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

// Expect the ready future to complete
await expectLater(
game.ready().timeout(const Duration(seconds: 2)),
completes,
);
expect(game.hasLifecycleEvents, isFalse);

// Adding the parent again should eventually mount the child as well.
await game.add(parent);
Expand Down

0 comments on commit 9a5c54b

Please sign in to comment.