Last active
September 9, 2019 21:47
-
-
Save rjdestigter/42936b9a4084d383363627ce4bdcc155 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
var StateType; | |
(function (StateType) { | |
StateType["Down"] = "DOWN"; | |
StateType["Booting"] = "BOOTING"; | |
StateType["Failure"] = "FAILURE"; | |
StateType["Up"] = "UP"; | |
StateType["ShuttingDown"] = "SHUTTING_DOWN"; | |
})(StateType || (StateType = {})); | |
var EventType; | |
(function (EventType) { | |
EventType["Start"] = "START"; | |
EventType["Stop"] = "STOP"; | |
EventType["Booted"] = "BOOTED"; | |
EventType["Retry"] = "RETRY"; | |
EventType["Reboot"] = "REBOOT"; | |
})(EventType || (EventType = {})); | |
var ActionType; | |
(function (ActionType) { | |
ActionType["IncrementRunCount"] = "incrementRunCount"; | |
ActionType["DecrementRunCount"] = "decrementRunCount"; | |
ActionType["Reset"] = "reset"; | |
ActionType["ResetOutput"] = "resetOutput"; | |
ActionType["Ok"] = "ok"; | |
ActionType["Bad"] = "bad"; | |
})(ActionType || (ActionType = {})); | |
var Status; | |
(function (Status) { | |
Status["Unresolved"] = "UNRESOLVED"; | |
Status["Pending"] = "PENDING"; | |
Status["Ok"] = "OK"; | |
Status["Bad"] = "BAD"; | |
})(Status || (Status = {})); | |
// Actions | |
const incrementRunCount = assign((ctx, evt) => evt.type !== EventType.Retry && evt.type !== EventType.Reboot | |
? Object.assign({}, ctx, { runCount: ctx.runCount + 1 }) : ctx); | |
const decrementRunCount = assign((ctx) => (Object.assign({}, ctx, { runCount: ctx.runCount - 1 }))); | |
const reset = assign((ctx) => ({ | |
status: Status.Unresolved, | |
start: ctx.start, | |
runCount: 0 | |
})); | |
const resetOutput = assign((ctx) => ({ | |
status: Status.Unresolved, | |
start: ctx.start, | |
runCount: ctx.runCount | |
})); | |
const ok = assign((ctx, evt) => { | |
return ({ | |
status: Status.Ok, | |
start: ctx.start, | |
runCount: ctx.runCount, | |
output: evt.data[0], | |
stop: evt.data[1] | |
}); | |
}); | |
const bad = assign((ctx, evt) => ({ | |
status: Status.Bad, | |
start: ctx.start, | |
runCount: ctx.runCount, | |
error: evt.data | |
})); | |
const withStopAction = { | |
[EventType.Stop]: [ | |
{ | |
actions: ActionType.DecrementRunCount, | |
cond: "runCountIsHigh" | |
}, | |
{ | |
actions: ActionType.DecrementRunCount, | |
target: StateType.ShuttingDown, | |
cond: "runCountIsLow" | |
} | |
] | |
}; | |
const Void = void 0; | |
/** | |
* Machine configuration | |
*/ | |
const config = { | |
id: "program", | |
strict: true, | |
context: { | |
status: Status.Unresolved, | |
start: () => [Void, () => Void], | |
runCount: 0 | |
}, | |
initial: StateType.Down, | |
states: { | |
[StateType.Down]: { | |
entry: ActionType.Reset, | |
on: { | |
[EventType.Start]: StateType.Booting | |
} | |
}, | |
[StateType.Booting]: { | |
entry: ActionType.IncrementRunCount, | |
invoke: { | |
id: "runEffect", | |
src: "startService", | |
onDone: { | |
target: StateType.Up, | |
actions: ok | |
}, | |
onError: { | |
target: StateType.Failure, | |
actions: bad | |
} | |
}, | |
on: Object.assign({ [EventType.Start]: { | |
actions: ActionType.IncrementRunCount | |
} }, withStopAction) | |
}, | |
[StateType.Up]: { | |
on: Object.assign({ [EventType.Start]: { | |
actions: ActionType.IncrementRunCount | |
}, [EventType.Reboot]: { | |
target: StateType.Booting | |
} }, withStopAction) | |
}, | |
[StateType.Failure]: { | |
exit: resetOutput, | |
on: Object.assign({ [EventType.Start]: { | |
actions: ActionType.IncrementRunCount | |
}, [EventType.Retry]: StateType.Booting }, withStopAction) | |
}, | |
[StateType.ShuttingDown]: { | |
on: { | |
[EventType.Start]: StateType.Up | |
}, | |
invoke: { | |
id: "onStop", | |
src: "stopService", | |
onDone: StateType.Down, | |
onError: StateType.Down | |
} | |
} | |
} | |
}; | |
const machine = Machine(config, | |
// @ts-ignor | |
{ | |
actions: { | |
incrementRunCount, | |
decrementRunCount, | |
reset, | |
resetOutput, | |
ok: ok, | |
bad: bad | |
}, | |
services: { | |
startService: async (ctx, evt) => { | |
if (evt.type === EventType.Start) { | |
const [p, x] = ctx.start(); | |
const o = await p; | |
return [o, x]; | |
} | |
}, | |
stopService: ctx => { | |
if (ctx.status === Status.Pending && ctx.stop) { | |
return Promise.resolve(ctx.stop(ctx.result)); | |
} | |
else if (ctx.status === Status.Ok && ctx.stop) { | |
return Promise.resolve(ctx.stop(ctx.output)); | |
} | |
} | |
}, | |
guards: { | |
runCountIsLow: (ctx) => ctx.runCount <= 1, | |
runCountIsHigh: (ctx) => ctx.runCount > 1 | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment