Skip to content

Instantly share code, notes, and snippets.

@gsong
Last active March 20, 2020 17:21
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 gsong/4c9c4927ec93419a53354835a918f19a to your computer and use it in GitHub Desktop.
Save gsong/4c9c4927ec93419a53354835a918f19a 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)
// https://xstate.js.org/viz/?gist=4c9c4927ec93419a53354835a918f19a
const applicationMachine = Machine(
{
id: "application",
context: { uploadRetries: 0, downloadRetries: 0 },
initial: "assembled",
states: {
assembled: {
on: {
UPLOAD: { internal: true, actions: ["uploadToIPD"] },
"": { target: "failed", cond: "tooManyUploadRetries" },
STAGED: "staged",
},
},
staged: {
on: {
DOWNLOAD: { internal: true, actions: ["downloadFromIPD"] },
"": { target: "failed", cond: "tooManyDownloadRetries" },
ACCEPTED: "processing",
ERROR: "failed",
},
},
manuallyProcessing: {
entry: ["notifyCustomer", "notifyHKP"],
on: {
MANUALLY_PROCESSED: {
target: "processing",
actions: ["uploadAcceptance"],
},
VOID: "voided",
},
},
processing: { entry: ["notifyCustomer", "notifyHKP"], type: "final" },
failed: {
entry: ["notifyCustomer", "notifyHKP"],
on: { CUSTOMER_RETRY: "manuallyProcessing", CUSTOMER_VOID: "voided" },
},
voided: { type: "final", entry: ["notifyCustomer"] },
},
},
{
actions: {
uploadToIPD: assign(context => ({
uploadRetries: context.uploadRetries + 1,
})),
downloadFromIPD: assign(context => ({
downloadRetries: context.downloadRetries + 1,
})),
uploadAcceptance: {},
notifyHKP: {},
notifyCustomer: {},
},
guards: {
tooManyUploadRetries: context => context.uploadRetries >= 3,
tooManyDownloadRetries: context => context.downloadRetries >= 3,
},
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment