Skip to content

Instantly share code, notes, and snippets.

@ChristianMurphy
Last active April 27, 2021 15:51
Show Gist options
  • Save ChristianMurphy/f68893d849bd0eba2afd6a5a7a0769df to your computer and use it in GitHub Desktop.
Save ChristianMurphy/f68893d849bd0eba2afd6a5a7a0769df 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: "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