Last active
October 16, 2021 12:35
-
-
Save pyoner/dd9f729209717beb2702c6b73d7ef9c9 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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