Skip to content

Instantly share code, notes, and snippets.

@aeschylus
Last active August 5, 2021 22:39
Show Gist options
  • Save aeschylus/f4d74b12b8c9292b4c9cb196062461f8 to your computer and use it in GitHub Desktop.
Save aeschylus/f4d74b12b8c9292b4c9cb196062461f8 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 addWater = assign({
amount: (context, event) => context.amount + 1
});
// Guard to check if the glass is full
function glassIsFull(context, event) {
return context.amount >= 10;
}
const glassMachine = Machine(
{
id: 'glass',
// the initial context (extended state) of the statechart
context: {
amount: 0
},
initial: 'empty',
states: {
empty: {
on: {
FILL: {
target: 'filling',
actions: 'addWater'
}
}
},
filling: {
// Transient transition
always: {
target: 'full',
cond: 'glassIsFull'
},
on: {
FILL: {
target: 'filling',
actions: 'addWater'
}
}
},
full: {}
}
},
{
actions: { addWater },
guards: { glassIsFull }
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment