Skip to content

Commit

Permalink
feat: adding has mounted to component (#1418)
Browse files Browse the repository at this point in the history
* feat: adding has mounted to component

* feat: pr suggestions

* feat: improving hasMounted

* feat: renaming hasMounted to mounted

* feat: pr suggestion
  • Loading branch information
erickzanardo authored Mar 8, 2022
1 parent dfeafdd commit f8f9e04
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/flame/lib/src/components/component.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:collection';

import 'package:flutter/painting.dart';
Expand Down Expand Up @@ -53,6 +54,8 @@ class Component {
bool get hasChildren => _children?.isNotEmpty ?? false;
ComponentSet? _children;

Completer<void>? _mountCompleter;

@protected
_LifecycleManager get lifecycle => _lifecycleManager ??= _LifecycleManager();
_LifecycleManager? _lifecycleManager;
Expand Down Expand Up @@ -203,6 +206,17 @@ class Component {
/// ```
void onMount() {}

/// A future that will complete once the component is mounted on its parent
Future<void> get mounted {
if (isMounted) {
return Future.value();
}

_mountCompleter ??= Completer<void>();

return _mountCompleter!.future;
}

/// This method is called periodically by the game engine to request that your
/// component updates itself.
///
Expand Down Expand Up @@ -258,6 +272,7 @@ class Component {
/// Changes the current parent for another parent and prepares the tree under
/// the new root.
void changeParent(Component component) {
_mountCompleter = null;
parent?.remove(this);
nextParent = component;
}
Expand Down Expand Up @@ -403,6 +418,8 @@ class Component {
if (existingChild || _state == LifecycleState.removed) {
onGameResize(findGame()!.canvasSize);
}
_mountCompleter?.complete();
_mountCompleter = null;
onMount();
_state = LifecycleState.mounted;
if (!existingChild) {
Expand Down
46 changes: 46 additions & 0 deletions packages/flame/test/components/component_lifecycle_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,52 @@ void main() {
);
});

flameGame.test('component mounted completes', (game) async {
final component = _MyComponent();
await game.add(component);
final mounted = component.mounted;

await game.ready();

return expectLater(mounted, completes);
});

flameGame.test(
'component mounted completes even after the '
'component is already mounted',
(game) async {
final component = _MyComponent();
await game.add(component);
await game.ready();

final mounted = component.mounted;

return expectLater(mounted, completes);
},
);

flameGame.test(
'component mounted completes when changing parent',
(game) async {
final parent = _MyComponent('parent');
final child = _MyComponent('child');
parent.add(child);
game.add(parent);

var mounted = child.mounted;
await game.ready();

await expectLater(mounted, completes);

child.changeParent(game);
mounted = child.mounted;
game.update(0);
await game.ready();

await expectLater(mounted, completes);
},
);

// Obsolete scenario, when we used to have a separate "prepare" stage
flameGame.test('parent prepares the component', (game) async {
final parent = _MyComponent('parent');
Expand Down

0 comments on commit f8f9e04

Please sign in to comment.