Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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