Skip to content

Instantly share code, notes, and snippets.

@androide-osorio
Last active May 27, 2020 13:03
Show Gist options
  • Save androide-osorio/b98b496aa31edf11bdfbc0488da9be45 to your computer and use it in GitHub Desktop.
Save androide-osorio/b98b496aa31edf11bdfbc0488da9be45 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: {
data: null,
retries: 0,
},
states: {
idle: {
on: {
FETCH: 'loading'
}
},
loading: {
on: {
RESOLVE: 'success',
REJECT: 'failure',
CANCEL: 'cancelled',
}
},
success: {
entry: ['saveData'],
on: {
REFRESH: 'loading',
}
},
failure: {
on: {
RETRY: {
target: 'loading',
actions: ['incRetries'],
}
}
},
cancelled: {
type: 'final'
},
}
}, {
actions: {
incRetries: assign({
retries: (context) => context.retries + 1,
}),
saveData: assign({
data: (_, event) => event.data,
}),
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment