Created
September 2, 2020 23:12
-
-
Save AlexFrazer/eb85ca13c8e077cfe6bedd010ea233a9 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const machine = Machine({ | |
id: "fetch", | |
initial: "idle", | |
context: { | |
data: null, | |
error: null, | |
lastRequest: -Infinity | |
}, | |
states: { | |
idle: { | |
on: { | |
FETCH: "loading" | |
} | |
}, | |
loading: { | |
invoke: { | |
src: "fetchData", | |
onDone: { | |
target: "success", | |
actions: assign({ | |
data: (_, event) => event.data | |
}) | |
}, | |
onError: { | |
target: "failure", | |
actions: assign({ | |
error: (_, event) => event.data | |
}) | |
} | |
} | |
}, | |
success: { | |
entry: "notifySuccess", | |
on: { | |
RETRY: "loading" | |
} | |
}, | |
failure: { | |
initial: 'error', | |
states: { | |
error: { | |
on: { | |
'': [ | |
{ target: 'internal_server_error', cond: '500' }, | |
{ target: 'unauthorized', cond: '401' }, | |
], | |
} | |
}, | |
internal_server_error: { | |
type: 'final', | |
}, | |
unauthorized: { | |
entry: 'login_redirect', | |
} | |
}, | |
on: { | |
RETRY: 'loading', | |
} | |
} | |
} | |
}, { | |
guards: { | |
500: ctx => ctx.error?.response?.status === 500, | |
401: ctx => ctx.error?.response?.status === 401 | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment