Skip to content

Instantly share code, notes, and snippets.

@lillypiri
Last active December 16, 2019 23:47
Show Gist options
  • Save lillypiri/e978a7228b15b61a26ac26108b8727a5 to your computer and use it in GitHub Desktop.
Save lillypiri/e978a7228b15b61a26ac26108b8727a5 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 fetchMachine = Machine({
id: 'provisioning',
initial: 'connection',
context: {
retries: 0,
status: 'Create approval pending'
},
states: {
connection: {
on: {
FETCH: 'pollingIbm'
}
},
pollingIbm: {
on: {
RESOLVE: 'success',
REJECT: 'failure'
}
},
success: {
on: {
"" : [
{ target: "pending", cond: "approvalPending"},
{ target: "creating", cond: "creatingConnection"},
{target: "provisioned", cond: "connectionProvisioned"},
{target: "rejected", cond: "connectionRejected"},
{target: "error", cond: "errorOccurred"},
{target: "unknown", cond: "unknownStatus"},
]
}
},
pending: {on: { REPOLL: 'pollingIbm'}},
creating: {on: { REPOLL: 'pollingIbm'}},
provisioned: {},
rejected: {},
error: {},
unknown: {},
failure: {
on: {
RETRY: {
target: 'pollingIbm',
actions: assign({
retries: (context, event) => context.retries + 1
})
}
}
}
}
},
{
guards: {
approvalPending: (context, event) => {
// when we have IBM state 'Create approval pending', we need to keep polling until user approves connection
// real one might look like:
return context.status === "Create approval pending";
},
creatingConnection: (context, event) => {
// when we have IBM state 'Create in progress':
// 1. check if PG state is 'PENDING_CREATE_APPROVAL'
// if (pg.state === 'PENDING_CREATE_APPROVAL') { update the state to be 'CREATING' }
// we need to keep polling until connection is provisioned
return context.status === "Create in progress";
},
connectionProvisioned: (context, event) => {
//
return context.status === "PROVISIONED";
},
connectionRejected: (context, event) => {
//
return context.status === "REJECTED";
},
errorOccurred: (context, event) => {
//
return context.status === "ERROR";
},
unknownStatus: (context, event) => {
//
return context.status === "UNKNOWN";
},
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment