Skip to content

Instantly share code, notes, and snippets.

@adamkl
Last active August 4, 2020 04:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamkl/db52f5aa9bfdb205c8e9cf288a6a7d77 to your computer and use it in GitHub Desktop.
Save adamkl/db52f5aa9bfdb205c8e9cf288a6a7d77 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 workflowMachine = Machine(
{
id: "workflow_machine",
initial: "new",
context: {
recordedEvents: [],
expectedState: null
},
states: {
new: {
on: {
START: "step1"
}
},
step1: {
onEntry: ["recordEvent", "recordState"],
on: {
NEXT: [
{
target: "step1b",
cond: "someCondition"
},
{
target: "step2"
}
]
}
},
step1b: {
onEntry: ["recordEvent", "recordState"],
on: {
NEXT: "step2"
}
},
step2: {
onEntry: ["recordEvent", "recordState"],
on: {
SUBMIT: "submitting"
}
},
submitting: {
onEntry: ["recordEvent", "recordState"],
invoke: {
src: "sendEvents",
onDone: "submitted",
onError: "error"
}
},
submitted: {
onEntry: ["recordEvent", "recordState"],
on: {
APPROVE: [
{ target: "approved", cond: "canApprove" },
{ target: "error" }
]
}
},
approved: {
onEntry: ["recordEvent", "recordState"],
type: "final"
},
error: {
onEntry: ["recordEvent", "recordState"],
type: "final"
}
}
},
{
guards: {
someCondition: () => true,
canApprove: () => true
},
services: {
sendEvents: ({ recordedEvents, expectedState }) => "sent successfully"
},
actions: {
recordEvent: assign(({ recordedEvents }, event) => ({
recordedEvents: [...recordedEvents, event]
})),
recordState: assign((_, __) => {})
// this needs xstate 4.7 -> recordState: assign(({ recordedStates }, __, { state }) => ({ recordedStates: [...recordedStates, state.value] }))
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment