Skip to content

Instantly share code, notes, and snippets.

@keithernet
Last active May 27, 2021 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keithernet/1ba6d4b9f832831aae44119fc1778d7d to your computer and use it in GitHub Desktop.
Save keithernet/1ba6d4b9f832831aae44119fc1778d7d 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({
initial: 'loadingSettings',
context: { notifications: {} },
invoke: {
id: 'requests',
src: 'requestManager',
},
states: {
loadingSettings: {
invoke: {
src: 'loadSettings',
onDone: 'settingsLoaded',
},
},
settingsLoaded: {
entry: ['assignNotifications'],
initial: 'idle',
states: {
idle: {
on: {
SAVE: {
cond: 'notAlreadyUpdating',
target: 'sendingUpdate',
},
UPDATE: { actions: ['updateNotification'] },
},
},
sendingUpdate: {
entry: ['setActive', 'sendRequest'],
always: 'idle',
},
},
},
},
}, {
guards: {
notAlreadyUpdating: (ctx, event) => !ctx.notifications[1]
},
actions: {
assignNotifications: assign({notifications: (_, event)=> event.data}),
setActive: assign({notifications: (ctx, evt) => ({...ctx.notifications, 1: true})})
},
services: {
loadSettings: () => Promise.resolve({1: false, 2: false}),
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment