Skip to content

Instantly share code, notes, and snippets.

@AndresRodH
Last active November 28, 2020 15:55
Show Gist options
  • Save AndresRodH/91f849789d3fd8b6e489b8a91021c018 to your computer and use it in GitHub Desktop.
Save AndresRodH/91f849789d3fd8b6e489b8a91021c018 to your computer and use it in GitHub Desktop.
Example 4
const doTheThingMachine = Machine({
id: 'do-the-thing',
initial: 'validating',
states: {
validating: {
invoke: {
src: 'validateArgs',
onDone: 'handlingThings',
onError: {
target: 'failure',
actions: 'invalidArgsError'
}
}
},
handlingThings: {
type: 'parallel',
onDone: [
{target: 'success', cond: 'noErrorsHappened'},
{target: 'failure', actions: 'performCleanups'},
],
states: {
thingOne: {
initial: 'handlingA',
states: {
handlingA: {
invoke: {
src: 'handleA',
onDone: {
target: 'complete',
actions: 'handleASuccess',
},
onError: {
target: 'complete',
actions: 'handleAError',
},
},
},
complete: {
type: 'final',
},
},
},
thingTwo: {
initial: 'handlingB',
states: {
handlingB: {
invoke: {
src: 'handleB',
onDone: {
target: 'handlingC',
actions: 'handleBSuccess',
},
onError: {
target: 'complete',
actions: 'handleBError',
},
},
},
handlingC: {
invoke: {
src: 'handleC',
onDone: {
target: 'complete',
actions: 'handleCSuccess',
},
onError: {
target: 'complete',
actions: ['handleCError', 'cleanupB'],
},
},
},
complete: {
type: 'final'
}
},
},
},
},
success: {
type: 'final',
},
failure: {
type: 'final',
},
},
},
{
guards: {
noErrorsHappened: () => /* check for errors */ true
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment