Skip to content

Instantly share code, notes, and snippets.

@reedspool
Created March 19, 2021 18:24
Show Gist options
  • Save reedspool/373863cf8bbf181223056a7c21a194fb to your computer and use it in GitHub Desktop.
Save reedspool/373863cf8bbf181223056a7c21a194fb 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({
strict: true,
id: "core",
type: "parallel",
states : {
individuals : {
initial: "loadingRandom",
states: {
loadingRandom: {
invoke: {
id: "fetchRandom",
src: "fetchRandom",
onDone: {
target: "active",
actions: ["recordGroup"]
},
onError: {
target: "#core.individuals.failure",
actions: () => alert("There was a problem with the Pokemon API :(")
}
},
},
active: {
entry: [ "sortItems" ],
initial: "loadingIndividuals",
states: {
loadingIndividuals: {
invoke: {
id: "fetchIndividuals",
src: "fetchIndividuals",
onDone: {
target: "idle",
actions: ["recordIndividuals"]
},
onError: {
target: "#core.individuals.failure",
actions: ["alertProblemWithAPI"]
}
},
},
idle: {}
},
on: {
FAVORITE: { actions: ["favoriteOne"] },
UNFAVORITE: { actions: ["unfavoriteOne"] }
}
},
failure: { type: "final" }
},
},
sorting: {
initial: "byId",
states: {
byId: {
entry: [ "setSortingFunctionById", "sortItems" ],
on : { SORT_BY_NAME : "byName" }
},
byName: {
entry: [ "setSortingFunctionByName", "sortItems" ],
on : { SORT_BY_ID : "byId" }
}
},
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment