Skip to content

Instantly share code, notes, and snippets.

@pyoner
Last active October 16, 2021 12:35
Show Gist options
  • Save pyoner/dd9f729209717beb2702c6b73d7ef9c9 to your computer and use it in GitHub Desktop.
Save pyoner/dd9f729209717beb2702c6b73d7ef9c9 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)
const asyncState = (src, onDone, onError = 'err') => {
const err = onError === 'err' ? {
on: {
RETRY: 'await'
}
} : null;
const state = {
initial: 'await',
states: {
await: {
invoke: {
src,
onDone,
onError
}
}
}
}
if (err) {
state.states.err = err
}
return state;
}
const CRUDMachine = Machine({
id: 'crud',
initial: 'init',
states: {
init: {
on: {
CREATE: 'asyncCreate'
}
},
// CREATE
asyncCreate: asyncState('create', '#crud.entity'),
entity: {
on: {
READ: 'asyncRead',
UPDATE: 'asyncUpdate',
DELETE: 'asyncDelete',
}
},
// READ
asyncRead: asyncState('read', '#crud.entity'),
// UPDATE
asyncUpdate: asyncState('update', '#crud.entity'),
// DELETE
asyncDelete: asyncState('delete', '#crud.trash'),
trash: {
type: 'final'
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment