Skip to content

Instantly share code, notes, and snippets.

@runeh
Created March 12, 2021 13:34
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 runeh/3186a03433253cbffa990296e5845136 to your computer and use it in GitHub Desktop.
Save runeh/3186a03433253cbffa990296e5845136 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({
strict: true,
id: 'deploy',
initial: 'idle',
context: {
env: undefined,
app: undefined,
tag: undefined,
hasAuth: undefined,
hasGcloud: undefined,
hasGit: undefined,
},
states: {
idle: {
on: {
'': {
target: 'checkingDependencies',
},
},
},
checkingDependencies: {
invoke: [
{
src: 'checkForGit',
onDone: {
actions: assign({ hasGit: (_, event) => event.data }),
},
},
{
src: 'checkForGcloud',
onDone: {
actions: assign({ hasGcloud: (_, event) => event.data }),
},
},
{
src: 'checkForAuth',
onDone: {
actions: assign({ hasAuth: (_, event) => event.data }),
},
},
],
on: {
'': [
{
target: 'editing',
cond: 'hasAllDependencies',
},
{
target: 'failed',
cond: 'missingSomeDependencies',
},
],
},
},
// fixme: change '' action to entry action
editing: {
initial: 'env',
states: {
env: {
on: {
'': {
target: 'app',
cond: 'hasEnv',
},
SET_ENV: {
actions: assign({ env: (context, event) => event.env }),
},
},
},
app: {
on: {
'': {
target: 'fetchingTags',
cond: 'hasApp',
},
SET_APP: {
actions: assign({ app: (context, event) => event.app }),
},
BACK: {
actions: assign(_ => ({ env: undefined })),
target: 'env',
},
},
},
fetchingTags: {
invoke: {
src: 'fetchTags',
onDone: {
target: 'tag',
actions: assign({
fetchedTags: (_, event) => {
return event.data;
},
}),
},
onError: {
target: '#deploy.failed',
actions: assign({
error: (_, event) => {
return event.data;
},
}),
},
},
},
tag: {
on: {
'': {
target: '#deploy.complete',
cond: 'hasTag',
},
SET_TAG: {
actions: assign({ tag: (context, event) => event.tag }),
},
BACK: {
target: 'app',
actions: assign(_ => ({ app: undefined })),
},
},
},
},
},
complete: {
on: {
CONFIRM: {
target: 'deploying',
},
ABORT: {
target: 'aborted',
},
},
},
deploying: {
invoke: [
{
src: 'triggerDeployAction',
onDone: {
target: 'finished',
},
onError: {
target: 'failed',
actions: assign({ error: (_, event) => event.data }),
},
},
],
},
finished: {
type: 'final',
},
failed: {
type: 'final',
},
aborted: {
type: 'final',
},
},
},
{
guards: {
hasAllDependencies: e =>
[e.hasAuth, e.hasGcloud, e.hasGit].every(e => e === true),
missingSomeDependencies: e =>
[e.hasAuth, e.hasGcloud, e.hasGit].some(e => e === false),
hasApp: e => e.app != null,
hasEnv: e => e.env != null,
hasTag: e => e.tag != null,
}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment