Skip to content

Instantly share code, notes, and snippets.

@zhentian-wan
Created May 9, 2023 07:12
Show Gist options
  • Save zhentian-wan/56e31817e0aa39f6fe3a93ff5d5b53ef to your computer and use it in GitHub Desktop.
Save zhentian-wan/56e31817e0aa39f6fe3a93ff5d5b53ef 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 fetchMachine = Machine({
id: 'fetch',
initial: 'idle',
context: {
retries: 0,
results: null,
error: null
},
states: {
idle: {
on: {
FETCH: 'loading'
}
},
loading: {
id: 'fetchData',
src: 'fetchData',
invoke: {
onDone: {
target: 'success',
actions: ['setResults']
},
onError: {
target: 'failed',
actions: ['setError']
}
}
},
success: {
initial: 'unknown',
on: {
FETCH: 'loading'
},
states: {
unknown: {
on: {
'': [
{target: 'withData', cond: 'hasData'},
{target: 'withoutData'}
]
}
},
withData: {},
withoutData: {}
}
},
failed: {
on: {
RETRY: 'loading',
actions: ['countRetries']
}
}
}
}, {
actions: {
setResults: assign((ctx, event) => ({
results: event.data
})),
setError: assign((ctx, event) => ({
error: event.data
}))
},
guards: {
hasData: () => {
return false
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment