Skip to content

Instantly share code, notes, and snippets.

@isaacplmann
Created July 17, 2019 01:42
Show Gist options
  • Save isaacplmann/f854330868056d9c83500755c637c110 to your computer and use it in GitHub Desktop.
Save isaacplmann/f854330868056d9c83500755c637c110 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const fetchDataMachine = Machine({
id: 'fetchData',
context: { data: undefined },
initial: 'idle',
states: {
idle: {
on: { FETCH: 'pending'}
},
pending: {
on: {
RESOLVE: 'fulfilled',
REJECT: 'rejected'
}
},
fulfilled: {
onEntry: assign({
data: (ctx, event) => event.data
}),
onExit: assign({
data: () => undefined
}),
on: {
REFRESH: 'pending'
},
initial: 'unknown',
states: {
unknown: {
on: {
'': [
{ target: 'withData', cond: 'hasData' },
{ target: 'withoutData' },
]
}
},
withData: {},
withoutData: {}
}
},
rejected: {
on: {
RETRY: 'pending'
}
}
}
}, {
guards: {
hasData: (ctx, event) => ctx.data && ctx.data.length > 0
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment