Skip to content

Instantly share code, notes, and snippets.

@renerbaffa
Created June 17, 2020 15:32
Show Gist options
  • Save renerbaffa/076bedad38fc514066bce79f64513405 to your computer and use it in GitHub Desktop.
Save renerbaffa/076bedad38fc514066bce79f64513405 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const TIMEOUT = 3000
Machine({
id: 'fetchMachine',
initial: 'idle',
context: {
response: null,
error: null,
},
states: {
idle: {
on: {
FETCH: 'loading',
},
},
loading: {
entry: ['load'],
after: {
[TIMEOUT]: {
target: 'fail',
actions: assign({
response: () => null,
error: 'error.timeout',
}),
},
},
on: {
RESOLVE: {
target: 'success',
actions: assign({
response: (_, event) => event.data,
error: () => null,
}),
},
REJECT: {
target: 'fail',
actions: assign({
response: () => null,
error: (_, event) => event.error,
}),
},
},
},
success: {},
fail: {},
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment