Last active
August 4, 2020 04:37
-
-
Save adamkl/db52f5aa9bfdb205c8e9cf288a6a7d77 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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