Last active
May 4, 2021 14:01
-
-
Save krehl/8c4c593c7c6fab82170b58a4407e7f98 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
// 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