Skip to content

Instantly share code, notes, and snippets.

@krehl
Last active May 4, 2021 14:01
Show Gist options
  • Save krehl/8c4c593c7c6fab82170b58a4407e7f98 to your computer and use it in GitHub Desktop.
Save krehl/8c4c593c7c6fab82170b58a4407e7f98 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 tankfull = (context, event) => {
return context.tank2 >= 1000;
};
const tanknotfull = (context, event) => {
return context.tank2 < 1000;
};
const equipment = Machine({
id: 'equipment',
initial: 'idle',
context: {
tank1: 0,
tank2: 0,
pumpspeed: 0,
interval: 100
},
states: {
idle: {
on: {
START: 'starting',
SET_PUMP: {
actions: [
(ctx, e) => ctx.pumpspeed = e.value,
(ctx, e) => console.log(e)
]
},
PUMP: {
actions: send({type:"SET_PUMP",value:100})
}
}
},
starting: {
on: {
STOP: 'stopping'
},
after: {
2000: 'running'
}
},
running: {
on: {
STOP: 'stopping',
'': [{target: 'stopping', cond: tankfull},
{target: 'pump', cond: tanknotfull}]
},
},
pump: {
after: {
1000: 'running'
},
entry: ['pump'],
on: {
STOP: 'stopping'
}
},
stopping: {
after: {
2000: 'idle'
}
},
}
},
{
actions: {
pump: (ctx, event) => {
// pump
ctx.tank2 = ctx.tank2 + ctx.pumpspeed;
console.log(ctx.tank2);
},
drain: (ctx, event) => {
// drain
ctx.tank2 = 0;
console.log(ctx.tank2);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment