Last active
August 5, 2021 22:39
-
-
Save aeschylus/f4d74b12b8c9292b4c9cb196062461f8 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 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