Created
December 15, 2020 15:15
-
-
Save decaylala/f97ec52ca1ffd44e5cd6873696ee6844 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 FULL_STOCK = 5; | |
const machine = Machine({ | |
initial: 'normal', | |
context: { | |
supply: 5 | |
}, | |
states: { | |
normal: { | |
on: { | |
"": [ | |
{ | |
target: 'empty', | |
cond: 'isEmpty', | |
}, | |
{ | |
target: 'full', | |
cond: 'isFull' | |
}, | |
], | |
DISPENSE: { | |
target: 'normal', | |
actions: 'dispense' | |
}, | |
RESTOCK: { | |
target: 'normal', | |
actions: 'restock', | |
} | |
}, | |
}, | |
full: { | |
on: { | |
DISPENSE: { | |
target: 'normal', | |
actions: 'dispense' | |
}, | |
} | |
}, | |
empty: { | |
on: { | |
RESTOCK: { | |
target: 'normal', | |
actions: 'restock' | |
} | |
} | |
} | |
} | |
}, { | |
actions: { | |
dispense: assign({ | |
supply: (context, event) => context.supply - 1 | |
}), | |
restock: assign({ | |
supply: (context, event) => FULL_STOCK | |
}) | |
}, | |
guards: { | |
isEmpty: (context, event) => { | |
return context.supply === 0; | |
}, | |
isFull: (context, event) => { | |
return context.supply === FULL_STOCK; | |
} | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment