Skip to content

Instantly share code, notes, and snippets.

@tmlangley
Created September 17, 2020 19:55
Show Gist options
  • Save tmlangley/d155d4ff600138ae22b53cd8216ed326 to your computer and use it in GitHub Desktop.
Save tmlangley/d155d4ff600138ae22b53cd8216ed326 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const ResultsMachine = Machine(
{
id: 'resultsMachine',
initial: 'waiting',
context: {
responseData: null,
filterParams: null,
error: '',
},
states: {
waiting: {
on: {
FETCH_RESULTS: {
target: 'fetching',
actions: ['setFilterParams'],
},
},
},
fetching: {
invoke: {
id: 'fetchResults',
src: (_context, { params }) => fetchResults(params),
onDone: {
target: 'success',
actions: [
assign({
responseData: (_context, event) => event.data,
}),
],
},
onError: {
target: 'failed',
actions: [assign({ error: (context, event) => event.data })],
},
},
},
success: {
on: {
FETCH_RESULTS: {
target: 'fetching',
actions: ['setFilterParams'],
},
},
},
failed: {},
noResults: {},
},
},
{
actions: {
setFilterParams: assign({
filterParams: (context, event) => event.params,
}),
},
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment