Last active
March 26, 2020 20:21
-
-
Save androide-osorio/4291c04972f71ef7d8d40eaf0b750aa3 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
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
/* | |
Null events and transient transitions | |
----------------------------------------------- | |
It is often useful to identify conditional branching in your machine as a state itself. A state that is designed to determine the next state does not need a specific event sent to trigger the transition. Instead, we can use the "null event" to trigger an immediate, transient transition. | |
The null event is identified with an event name of an empty string '', and is immediately sent to the state upon entry. We can setup multiple targets with conditionals, or fire off actions to set up a future state with this transient transitions. | |
*/ | |
const ifAtFirstyouDontSuceed = Machine({ | |
id: 'tryTryAgain', | |
initial: 'idle', | |
context: { | |
tries: 0, | |
}, | |
states: { | |
idle: { | |
on: { TRY: 'trying' } | |
}, | |
trying: { | |
entry: ['incTries'], | |
on: { | |
'': [ | |
{ target: 'success', cond: 'triedEnough' }, | |
{ target: 'idle' }, | |
], | |
} | |
}, | |
success: {} | |
} | |
}, { | |
actions: { | |
incTries: assign({ | |
tries: ctx => ctx.tries + 1, | |
}) | |
}, | |
guards: { | |
triedEnough: ctx => ctx.tries > 2, | |
}, | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment