Skip to content

Instantly share code, notes, and snippets.

@JM-Mendez
Last active May 26, 2020 14:42
Show Gist options
  • Save JM-Mendez/dae4d6f6837d193ed2149e2e4515e206 to your computer and use it in GitHub Desktop.
Save JM-Mendez/dae4d6f6837d193ed2149e2e4515e206 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)
// will prevent components from adding more constraints
const sendMsgToBus = (msg) => console.log(msg)
const queryMachine = Machine({
id: 'queryBuilder',
initial: 'disabled',
context: {
count: 0
},
states: {
disabled: {
on: {
CONSTRAINT_ADDED: {
target: 'idle',
actions: 'addConstraint'
}
}
},
idle: {
on: {
CONSTRAINT_ADDED: [
{
target: 'idle',
cond: ctx => ctx.count < 5,
actions: 'addConstraint'
},
{
target: 'maxConstraints'
}
],
CONSTRAINT_REMOVED: [
{
target: 'disabled',
cond: ctx => ctx.count === 1,
actions: 'removeConstraint'
},
{
target: 'idle',
actions: 'removeConstraint'
}
]
}
},
maxConstraints: {
entry: 'lockConstraints',
on: {
CONSTRAINT_REMOVED: {
target: 'idle',
actions: ['removeConstraint', 'unlockConstraints']
}
}
}
}
},{
actions: {
addConstraint: assign({
count: ctx => ctx.count + 1
}),
removeConstraint: assign({
count: ctx => ctx.count - 1
}),
lockConstraints: () => sendMsgToBus('CONSTRAINTS_LOCKED'),
unlockConstraints: () => sendMsgToBus('CONSTRAINTS_UNLOCKED')
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment