Skip to content

Instantly share code, notes, and snippets.

@LevelbossMike
Last active October 21, 2020 14:42
Show Gist options
  • Select an option

  • Save LevelbossMike/b90b7a517f8f756d2c87dce240f102fd to your computer and use it in GitHub Desktop.

Select an option

Save LevelbossMike/b90b7a517f8f756d2c87dce240f102fd to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
function noop() {}
const async = Machine(
{
id: 'async',
initial: "idle",
context: {
trigger: noop,
onSuccess: noop,
onError: noop
},
states: {
idle: {
on: {
DO: "busy"
}
},
busy: {
invoke: {
id: "async",
src: (context) => context.trigger,
onDone: {
target: "success"
},
onError: {
target: "error"
}
}
},
success: {
entry: "handleSuccess",
on: {
DO: "busy"
}
},
error: {
entry: "handleError",
on: {
DO: "busy"
}
}
}
},
{
actions: {
handleSuccess(context, event) {
const { data } = event;
context.onSuccess(data);
},
handleError(context, event) {
const { data } = event;
context.onError(data);
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment