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

[core] Add waitFor helper #3190

Merged
merged 15 commits into from
Apr 7, 2022
Merged

[core] Add waitFor helper #3190

merged 15 commits into from
Apr 7, 2022

Conversation

davidkpiano
Copy link
Member

@davidkpiano davidkpiano commented Apr 4, 2022

This PR adds the opt-in waitFor(...) helper function, that asynchronously waits for the emitted value of an actor to satisfy a predicate before returning that value.

See discussion here: https://discord.com/channels/795785288994652170/955085268677963776

Basic usage:

import { waitFor } from 'xstate/lib/waitFor';

// ...
const loginService = interpret(loginMachine).start();

const loggedInState = await waitFor(loginService, state => state.hasTag('loggedIn'));

loggedInState.hasTag('loggedIn'); // true

cc. @mattpocock @gavination

TODO:

  • Add changeset

@changeset-bot
Copy link

changeset-bot bot commented Apr 4, 2022

🦋 Changeset detected

Latest commit: 281f1fe

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
xstate Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ghost
Copy link

ghost commented Apr 4, 2022

CodeSee Review Map:

Review these changes using an interactive CodeSee Map

Review in an interactive map

View more CodeSee Maps

Legend

CodeSee Map Legend

@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 4, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 281f1fe:

Sandbox Source
XState Example Template Configuration
XState React Template Configuration

@@ -1090,7 +1090,7 @@ export class Interpreter<
})
.start();

return actor;
return actor as any;
Copy link
Member

Choose a reason for hiding this comment

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

was this change needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Tests were failing without it locally

sub?.unsubscribe();
};

const sub = actorRef.subscribe({
Copy link
Member

Choose a reason for hiding this comment

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

what about actors that do not emit their current state at subscription time? or are actors supposed to always do that? Note that today an actor made out of an observable might not emit immediately because the emit time is controlled by the observable itself

Should we run predicate with .getSnapshot() first? With that I'm a little bit worried about handling non-started interpreters as they would instantiate their .initialState and that is not a pure operation right now 😢

Copy link
Member Author

Choose a reason for hiding this comment

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

For now, let's have the behavior as-is - if actors (like observables) do not emit state upon subscription, this utility should respect that.

That avoids the impure initial state problem, too 👍

Andarist and others added 2 commits April 7, 2022 14:37
…ter (#3200)

* Fixed `waitFor` hanging till timeout with final state matching the predicate

* Fixed `waitFor` hanging till timeout with final state not matching the predicate

* Fixed spammy sends in the waitFor test file
* Add missing changeset about changes in the core from #3200

* Update .changeset/red-walls-lay.md

Co-authored-by: David Khourshid <davidkpiano@gmail.com>

Co-authored-by: David Khourshid <davidkpiano@gmail.com>
@davidkpiano davidkpiano merged commit 518d2f9 into main Apr 7, 2022
@davidkpiano davidkpiano deleted the davidkpiano/waitFor branch April 7, 2022 17:40
@github-actions github-actions bot mentioned this pull request Apr 7, 2022
nevilm-lt pushed a commit to nevilm-lt/xstate that referenced this pull request Apr 22, 2022
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.

3 participants