Skip to content

Instantly share code, notes, and snippets.

@michaelsbradleyjr
Last active August 2, 2019 17:16
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 michaelsbradleyjr/fbbd5dab67c92587cd726648ad1fa212 to your computer and use it in GitHub Desktop.
Save michaelsbradleyjr/fbbd5dab67c92587cd726648ad1fa212 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const machind = Machine({
id: 'command',
context: {},
initial: 'starting',
states: {
starting: {
type: 'parallel',
states: {
A: {
initial: 'starting',
states: {
starting: {
on: {
DID_START_A: 'started',
STOP_A: 'stopped'
}
},
started: {
on: {
STOP_A: 'stopped'
}
},
stopped: {}
}
},
B: {
initial: 'starting',
states: {
starting: {
on: {
DID_START_B: 'started',
STOP_B: 'stopped'
}
},
started: {
on: {
STOP_B: 'stopped'
}
},
stopped: {}
}
}
},
entry: 'clearStartingConds',
on: {
'': [
{
target: 'running',
cond: 'allStarted',
actions: 'flagAllStarted'
},
{
target: 'stopped',
cond: 'allStopped',
actions: 'flagAllStopped'
}
]
}
},
running: {
type: 'parallel',
states: {
A: {},
B: {}
},
on: {
STOP: 'stopped'
}
},
stopped: {
on: {
RESTART: 'starting'
}
}
}
},
{
actions: {
clearStartingConds: assign(context => ({
...context,
allStarted: undefined,
allStopped: undefined
})),
flagAllStarted: assign(context => ({
...context,
allStarted: true,
allStopped: false
})),
flagAllStopped: assign(context => ({
...context,
allStarted: false,
allStopped: true
}))
},
guards: {
allStarted: (context, event, meta) => {
let cond, state;
if (meta) ({cond, state} = meta);
console.log('context:', context);
console.log('event:', event);
console.log('cond:', cond);
console.log('state:', state);
console.log('-----------------');
if (context.allStarted !== undefined) return context.allStarted;
if (state && state.matches('starting')) {
return Object.values(
state.value.starting
).every(state => state === 'started')
}
return;
},
allStopped: (context, event, meta) => {
let cond, state;
if (meta) ({cond, state} = meta);
console.log('context:', context);
console.log('event:', event);
console.log('cond:', cond);
console.log('state:', state);
console.log('-----------------');
if (context.allStopped !== undefined) return context.allStopped;
if (state && state.matches('starting')) {
return Object.values(
state.value.starting
).every(state => state === 'stopped')
}
return;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment