Skip to content

Instantly share code, notes, and snippets.

@bwaidelich
Created March 19, 2021 18:47
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 bwaidelich/7354578ebb99b997544919c3e1730a68 to your computer and use it in GitHub Desktop.
Save bwaidelich/7354578ebb99b997544919c3e1730a68 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const myMachine = Machine({
id: "machine",
initial: "idle",
context: {
id: null,
revision: 0,
isOwner: false,
isAuthenticated: true
},
states: {
idle: {
on: {
CHANGE: "pendingChanges",
LOAD: "loading",
},
},
pendingChanges: {
on: {
CHANGE: { target: "pendingChanges", internal: true },
SAVE: [
{
target: 'saving',
cond: "isOwner",
actions: "createRevision"
},
{
target: 'choosingPod',
cond: "isAuthenticated"
},
{target: 'saving'}
]
},
},
saving: {
invoke: {
id: 'saving',
src: "save",
onDone: {
target: 'idle',
actions: assign({ isOwner: (context, event) => true })
},
onError: {
target: 'error'
}
}
},
loading: {
invoke: {
id: 'loading',
src: "load",
onDone: {
target: 'idle',
actions: assign({ isOwner: (context, event) => true })
},
onError: {
target: 'error'
}
}
},
choosingPod: {
on: {
CHOOSE: "saving",
}
},
error: {}
},
}, {
guards: {
isAuthenticated: (context, event) => context.isAuthenticated,
isOwner: (context, event) => context.isOwner,
},
actions: {
},
services: {
save: () => true,
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment