Skip to content

Instantly share code, notes, and snippets.

@johnniehard
Last active January 22, 2020 12:02
Show Gist options
  • Save johnniehard/20295648d2631014cb2a2f00047110eb to your computer and use it in GitHub Desktop.
Save johnniehard/20295648d2631014cb2a2f00047110eb to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const todoMachine = Machine({
id: 'todoMachine',
initial: 'fetch',
context: {
todos: null
},
states: {
idle: {
on: {
FETCH: 'fetch',
DELETE: 'delete',
ADD: 'add'
}
},
fetch: {
key: 'fetch',
id: 'fetch',
initial: 'start',
states: {
start: {
invoke: {
src: 'fetchTodos',
onDone: {
target: '#todoMachine.idle',
actions: assign({
todos: 'todos'
})
},
onError: 'fail'
}
},
fail: {
on: {
RETRY: 'start',
CANCEL: '#todoMachine.idle'
}
}
},
success: {
on: {
ADD: '#add',
DELETE: '#delete'
}
},
failure: {
on: {
TRY_AGAIN: 'start'
}
}
},
add: {
key: 'add',
id: 'add',
initial: 'start',
states: {
start: {
invoke: {
src: 'addTodo',
onDone: {
target: '#todoMachine.idle',
actions: assign({
todos: 'todos'
})
}
}
}
},
success: {
on: {
ADD: '#add',
DELETE: '#delete'
}
},
failure: {
on: {
TRY_AGAIN: 'start'
}
}
},
delete: {
key: 'delete',
id: 'delete',
initial: 'start',
states: {
start: {
invoke: {
src: 'deleteTodo',
onDone: {
target: '#todoMachine.idle',
actions: assign({
todos: 'todos'
})
}
}
}
},
success: {
on: {
ADD: '#add',
DELETE: '#delete'
}
},
failure: {
on: {
TRY_AGAIN: 'start'
}
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment