Last active
April 27, 2021 15:51
-
-
Save ChristianMurphy/f68893d849bd0eba2afd6a5a7a0769df to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const fetchMachine = Machine({ | |
id: "search", | |
initial: "idle", | |
context: { | |
retries: 0, | |
error: undefined, | |
results: undefined | |
}, | |
states: { | |
idle: { | |
on: { | |
SEARCH: "loading", | |
}, | |
}, | |
loading: { | |
invoke: { | |
id: "getSearchResults", | |
src: () => Promise.resolve(""), // replace with real fetch | |
onDone: { | |
target: "success", | |
actions: assign({ results: (context, event) => event.data }), | |
}, | |
onError: { | |
target: "failure", | |
actions: assign({ error: (context, event) => event.data }), | |
}, | |
}, | |
}, | |
success: { | |
type: "final", | |
on: { | |
SEARCH: "loading", | |
} | |
}, | |
failure: { | |
on: { | |
RETRY: { | |
target: "loading", | |
actions: assign({ | |
retries: (context, event) => context.retries + 1, | |
}), | |
}, | |
SEARCH: "loading", | |
}, | |
}, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment