Skip to content

Instantly share code, notes, and snippets.

@edenworky
Last active January 23, 2020 14:20
Show Gist options
  • Select an option

  • Save edenworky/b8056de94a9b99b70d66bd29a7654eae to your computer and use it in GitHub Desktop.

Select an option

Save edenworky/b8056de94a9b99b70d66bd29a7654eae to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
let NOT_SET = Symbol('NOT_SET')
let abstractActions = {
assignInput: assign({input: (_, ev) => ev.data}),
assignOutput: assign({output: (_, ev) => ev.data}),
assignError: assign({error: (_, ev) => ev.error}),
notifyClean: () => console.log('clean'),
notifyPiping: () => console.log('piping'),
notifyPiped: () => console.log('piped'),
notifyLeaking: () => console.warn('leak'),
notifyEnded: () => console.log('ended'),
clean: assign(['input', 'output', 'error'].reduce((acc, key) => ({...acc, [key]: NOT_SET}), {})),
handleLeak: (_, ev) => console.error(ev),
assemble: () => console.log('assemble'),
},
CLEAN = 'clean',
ASSEMBLE = {
actions: 'assemble',
},
PIPE_IN = {
target: 'piping',
actions: 'assignInput',
},
PIPE_OUT = {
target: 'piped',
actions: 'assignOutput',
},
LEAK = {
target: 'leaked',
actions: 'assignError',
}
let pipeMachineDef = {
id: 'pipe',
initial: 'clean',
context: {
input: NOT_SET,
output: NOT_SET,
},
states: {
clean: {
entry: [
'clean',
'notifyClean'
],
on: {
ASSEMBLE,
PIPE_IN,
}
},
piping: {
entry: 'notifyPiping',
invoke: {
id: 'piping',
src: 'pipe',
onDone: PIPE_OUT,
onError: LEAK,
}
},
leaked: {
entry: [
'notifyLeaking',
'handleLeak',
],
type: 'final',
},
piped: {
entry: 'notifyPiped',
type: 'final',
},
},
}
let pipe = input => input * 2
let pipeMachineConfig = {
actions: abstractActions,
services: {
pipe: ctx => Promise.resolve(pipe(ctx.input)),
},
}
let pipeMachine = Machine(pipeMachineDef, pipeMachineConfig)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment