-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2197 from davidkpiano/davidkpiano/getsnapshot
[core] Add `.getSnapshot()` to actor refs
- Loading branch information
Showing
6 changed files
with
112 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
'xstate': minor | ||
--- | ||
|
||
All spawned and invoked actors now have a `.getSnapshot()` method, which allows you to retrieve the latest value emitted from that actor. That value may be `undefined` if no value has been emitted yet. | ||
|
||
```js | ||
const machine = createMachine({ | ||
context: { | ||
promiseRef: null | ||
}, | ||
initial: 'pending', | ||
states: { | ||
pending: { | ||
entry: assign({ | ||
promiseRef: () => spawn(fetch(/* ... */), 'some-promise') | ||
}) | ||
} | ||
} | ||
}); | ||
|
||
const service = interpret(machine) | ||
.onTransition((state) => { | ||
// Read promise value synchronously | ||
const resolvedValue = state.context.promiseRef?.getSnapshot(); | ||
// => undefined (if promise not resolved yet) | ||
// => { ... } (resolved data) | ||
}) | ||
.start(); | ||
|
||
// ... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.