Skip to content

Instantly share code, notes, and snippets.

@bwaidelich
Created March 21, 2021 14:57
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/a3eb62ce08ea0c32d34f60342d52b1d4 to your computer and use it in GitHub Desktop.
Save bwaidelich/a3eb62ce08ea0c32d34f60342d52b1d4 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const machine = Machine({
initial: 'idle',
context: {
userId: null,
projectId: null,
revision: 0
},
type: 'parallel',
states: {
auth: {
initial: "loggedOut",
states: {
loggedOut: {
on: {
LOGIN: "authenticate"
}
},
authenticate: {
invoke: {
src: "authenticate",
onDone: {
target: 'loggedIn',
actions: assign({ userId: (context, event) => event.userId })
},
onError: {
target: "error"
}
}
},
loggedIn: {
on: {
LOGOUT: {
target: "loggedOut",
actions: "logout"
}
}
},
error: {
}
}
},
project: {
initial: "new",
states: {
'new': {
id: "projectNew",
on: {
CHANGE: "pendingChanges",
}
},
"pendingChanges": {
on: {
CHANGE: { target: 'pendingChanges', internal: true},
SAVE: "saving"
}
},
saving: {
}
}
},
persistence: {
initial: "idle",
states: {
idle: {
initial: 'idle',
states: {
idle: {
on: {
LOAD: 'loading',
SAVE: "creating",
}
},
loading: {
invoke: {
src: "load",
onDone: [
{
target: '#ownedProject',
cond: "isOwner",
actions: assign({ projectId: (context, event) => event.projectId,
revision: (context, event) => event.revision })
},
{target: '#foreignProject',
actions: assign({ projectId: (context, event) => event.projectId,
revision: (context, event) => event.revision })}
]
}
},
creating: {
invoke: {
src: "create",
onDone: {
target: "#ownedProject",
actions: assign({ projectId: (context, event) => event.projectId, revision: 0 })
}
}
}
}
},
ownedProject: {
id: 'ownedProject',
initial: 'idle',
states: {
idle: {
on: {
SAVE: "saving",
}
},
saving: {
invoke: {
src: "save",
onDone: {
target: "idle",
actions: assign({ revision: (context, event) => event.revision })
}
}
}
}
},
foreignProject: {
id: 'foreignProject',
initial: 'idle',
states: {
idle: {
on: {
FORK: "forking"
}
},
forking: {
invoke: {
src: "fork",
onDone: {
target: '#ownedProject',
actions: assign({ id: (context, event) => event.id, revision: 0 })
}
}
}
}
}
}
}
}
}, {
guards: {
isOwner: (context, event) => {
return true
},
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment