Skip to content

Instantly share code, notes, and snippets.

@achambers
Last active October 19, 2021 17:27
Show Gist options
  • Save achambers/c809b48e253c605441160b68c45b32ee to your computer and use it in GitHub Desktop.
Save achambers/c809b48e253c605441160b68c45b32ee 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: 'fetch',
initial: 'idle',
context: {
fetchLimit: 5,
items: []
},
states: {
idle: {
on: {
LOAD_ITEMS: {
target: 'loadingItems',
}
}
},
loadingItems: {
invoke: {
src: 'loadItems',
onDone: {
target: 'renderItems',
actions: 'updateItems'
}
}
},
renderItems: {
on: {
LOAD_ITEMS: {
target: 'loadingItems',
actions: 'updateFetchLimit'
}
}
}
}
}, {
actions: {
updateItems: assign({ items: (context, event) => event.data }),
updateFetchLimit: assign({ fetchLimit: ({ fetchLimit }, _) => fetchLimit + 5 })
},
services: {
loadItems({ fetchLimit }/*, eventObject */) {
let arr = Array.from(Array(fetchLimit).keys())
return Promise.resolve(arr);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment