Skip to content

Instantly share code, notes, and snippets.

@androide-osorio
Last active March 26, 2020 20:21
Show Gist options
  • Save androide-osorio/4291c04972f71ef7d8d40eaf0b750aa3 to your computer and use it in GitHub Desktop.
Save androide-osorio/4291c04972f71ef7d8d40eaf0b750aa3 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
/*
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