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

feat: adding has mounted to component #1418

Merged
merged 10 commits into from
Mar 8, 2022
Merged

feat: adding has mounted to component #1418

merged 10 commits into from
Mar 8, 2022

Conversation

erickzanardo
Copy link
Member

Description

Solves #1417

Checklist

  • The title of my PR starts with a [Conventional Commit] prefix (fix:, feat:, docs: etc).
  • I have read the [Contributor Guide] and followed the process outlined for submitting PRs.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • I have updated/added relevant examples in examples.

Breaking Change

  • Yes, this is a breaking change.
  • No, this is not a breaking change.

@erickzanardo erickzanardo requested a review from spydon March 4, 2022 19:47
flameGame.test('component hasMounted completes', (game) async {
final component = _MyComponent();
await game.add(component);
final hasMounted = component.hasMounted;
Copy link
Contributor

Choose a reason for hiding this comment

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

I presume it is not possible to await for this future, since it cannot complete without the game loop running its course -- hence this would become an infinite cycle.

Perhaps it would be useful to have at least some test where we await component.hasMounted, a test case that would mimic the intended use case for this feature.

Copy link
Member Author

Choose a reason for hiding this comment

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

I double checked and the future does completes, otherwise the test would fail. I changed to expectLater to be sure, but even the original code was working.

Copy link
Contributor

Choose a reason for hiding this comment

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

But can you do await component.hasMounted;? I mean, isn't that how this future is supposed to be used?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, you can await it, the completes matcher makes sure that it finishes.

I could change my test to make use of the await keyword, but would make the test uglier IMO

Copy link
Contributor

Choose a reason for hiding this comment

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

The point is, right now we have no tests to check whether this future is actually awaitable.

Copy link
Member Author

Choose a reason for hiding this comment

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

How come? This test is doing exactly that 🤔

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

Choose a reason for hiding this comment

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

Nice solution!

@@ -403,6 +418,7 @@ class Component {
if (existingChild || _state == LifecycleState.removed) {
onGameResize(findGame()!.canvasSize);
}
_mountCompleter?.complete();
Copy link
Contributor

Choose a reason for hiding this comment

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

Set it to null here?

@@ -203,6 +206,17 @@ class Component {
/// ```
void onMount() {}

/// A future that will complete once the component is mounted on its parent
Future<void> get hasMounted {
Copy link
Contributor

@alestiago alestiago Mar 7, 2022

Choose a reason for hiding this comment

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

I don't know why, but hasMounted doesn't convince me as the method name. If I ever read this I'll expect to get a boolean value from it not a Future<void>. @erickzanardo suggested mounted which I somewhat preferred over hasMounted.

In addition, I have a question on why do we think that using a Completer is better than registering callbacks (for example, whenMounted(callback))? Is this to avoid callback hell? Do we highly recommend always awaiting for hasMounted, hence the Future type? or do we also suggest to unawait if necessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

I like the mounted name! @spydon what do you think?

About the future vs callback, I think it have a nicer API, and actually, by using a future, we can let the user use a callback if they prefer, by doing component.mounted.wheComplete(fn), so we can have the best of both worlds.

Copy link
Contributor

Choose a reason for hiding this comment

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

@erickzanardo true. Missed that API!

I think it's nice if we have it as mounted. I love how it reads:

component.mounted.whenComplete
and
await component.mounted

Copy link
Contributor

Choose a reason for hiding this comment

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

component.mounted still sounds alot like a boolean property, similar to .isMounted. Maybe a better name would be .whenMounted:

await component.whenMounted()

@erickzanardo erickzanardo merged commit f8f9e04 into main Mar 8, 2022
@erickzanardo erickzanardo deleted the erick.has_mounted branch March 8, 2022 17:17
st-pasha pushed a commit to st-pasha/flame that referenced this pull request Mar 9, 2022
* feat: adding has mounted to component

* feat: pr suggestions

* feat: improving hasMounted

* feat: renaming hasMounted to mounted

* feat: pr suggestion
spydon added a commit that referenced this pull request Mar 9, 2022
* ProcessQueues() method

* Added deadQueue in LifecycleManager

* refactor removeFromParent()

* make shouldRemove non-overridable

* Added lifecycle state "removing"

* prevent double-removal

* shouldRemove can only be set to true

* eliminate _shouldRemove

* rename _dead -> _dying

* added adoption queue

* removed nextParent

* deprecate ComponentSet.clear and .removeAll

* ComponentSet no longer handles removal

* remove usages of shouldRemove

* doc-comments

* onRemove refactor

* cleanup

* remove obsolete paragraph in docs

* rename queue dying -> removals

* feat: update examples dashbook (#1398)

* docs: Added tutorial for creating a bare Flame project (#1376)

* feat: Add missing optional priority to SpriteBodyComponent (#1404)

* Add missing optional priority to SpriteBodyComponent

Gives you the option to set the priority directly when creating the component.

* Add optional parameter priority

Adds priority as an optional parameter

* removed wrong trailing comma

Added a comma at the wrong position.

* feat:  Added getImageLayer to flame_tiled (#1405)

* feat: Create sphinx extension for integrating Flutter apps into the documentation site (#1393)

* feat: adding FlameBloc mixin to allow its usage with enhanced FlameGame classes (#1399)

* feat: adding FlameBloc mixin to allow its usage with enhanced FlameGame classes

* fixing tests

* Apply suggestions from code review

Co-authored-by: Lukas Klingsbo <me@lukas.fyi>
Co-authored-by: Pasha Stetsenko <stpasha@google.com>
Co-authored-by: Luan Nico <luanpotter27@gmail.com>

Co-authored-by: Lukas Klingsbo <me@lukas.fyi>
Co-authored-by: Pasha Stetsenko <stpasha@google.com>
Co-authored-by: Luan Nico <luanpotter27@gmail.com>

* refactor: Organize tests in the game/ folder (#1403)

* position_type_test

* detectors_test

* reformat projections_test

* Reorganize tests in projector_test

* review flame_game_test

* move some cameratests

* created viewport_test file

* reformat camera tests

* feat: Add missing optional priority to SpriteBodyComponent (#1404)

* Add missing optional priority to SpriteBodyComponent

Gives you the option to set the priority directly when creating the component.

* Add optional parameter priority

Adds priority as an optional parameter

* removed wrong trailing comma

Added a comma at the wrong position.

* feat:  Added getImageLayer to flame_tiled (#1405)

* feat: Create sphinx extension for integrating Flutter apps into the documentation site (#1393)

Co-authored-by: KurtLa <KurtLa@users.noreply.github.com>
Co-authored-by: Munsterlander <munsterlander@users.noreply.github.com>
Co-authored-by: Erick <erickzanardoo@gmail.com>

* feat: improving generics on position body component (#1397)

* chore(release): publish packages (#1407)

- flame@1.1.0-releasecandidate.1
 - flame_bloc@1.2.0-releasecandidate.1
 - flame_rive@1.1.0-releasecandidate.1
 - flame_test@1.2.0-releasecandidate.1
 - flame_tiled@1.3.0-releasecandidate.1

* chore: fixing pub deps to allow publish (#1408)

* chore(release): publish packages

 - flame@1.1.0-releasecandidate.1
 - flame_bloc@1.2.0-releasecandidate.1
 - flame_rive@1.1.0-releasecandidate.1
 - flame_test@1.2.0-releasecandidate.1
 - flame_tiled@1.3.0-releasecandidate.1

* chore: fixing deps to enable pub publish

* fixing vector math version

* chore(flame_forge2d): export all files in barrel file (#1409)

* chore(release): publish packages (#1410)

- flame_forge2d@0.9.0-releasecandidate.1

* chore: commented out PR template sections (#1412)

* feat: Make ContactCallback begin end methods optional overrides (#1415)

* feat: made begin and end optional overrides

* chore: removed unecessary end override

* feat: Camera as a component (#1355)

* feat(collision detection)!: Use a broadphase to make collision detection more efficient (#1252)

* fix: PositionBodyComponent had an async onMount, without needing (#1424)

* fix: Fix collision detection comments and typo (#1422)

* Fix collision detection comments and typo

* Update packages/flame/lib/src/collisions/collision_callbacks.dart

Co-authored-by: Pasha Stetsenko <stpasha@google.com>

* Update doc/flame/collision_detection.md

Co-authored-by: Pasha Stetsenko <stpasha@google.com>

Co-authored-by: Pasha Stetsenko <stpasha@google.com>

* feat: adding has mounted to component (#1418)

* feat: adding has mounted to component

* feat: pr suggestions

* feat: improving hasMounted

* feat: renaming hasMounted to mounted

* feat: pr suggestion

* chore(release): publish packages (#1427)

- flame_svg@1.1.0-releasecandidate.1
 - flame@1.1.0-releasecandidate.2
 - flame_bloc@1.2.0-releasecandidate.2
 - flame_forge2d@0.9.0-releasecandidate.2
 - flame_rive@1.1.0-releasecandidate.2
 - flame_test@1.2.0-releasecandidate.2
 - flame_tiled@1.3.0-releasecandidate.2

* fix a test

* fix broken merge

* rename parent->owner in LifecycleManager

* update doc-comment

Co-authored-by: Erick <erickzanardoo@gmail.com>
Co-authored-by: KurtLa <KurtLa@users.noreply.github.com>
Co-authored-by: Munsterlander <munsterlander@users.noreply.github.com>
Co-authored-by: Lukas Klingsbo <me@lukas.fyi>
Co-authored-by: Luan Nico <luanpotter27@gmail.com>
Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com>
Co-authored-by: Alejandro Santiago <dev@alestiago.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants