Skip to content

Instantly share code, notes, and snippets.

@john-bell-gw
Last active December 2, 2021 19:00
Show Gist options
  • Save john-bell-gw/c7bd29ce6c96130a66b6504e9463507e to your computer and use it in GitHub Desktop.
Save john-bell-gw/c7bd29ce6c96130a66b6504e9463507e to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const lightBulbMachine = Machine(
{
id: 'lightbulb',
strict: true,
initial: 'lit',
context: {
count: 0,
},
states: {
lit: {
exit: () => {
console.log('it is dark');
},
on: {
BREAK: 'broken',
TOGGLE: {
target: 'unlit',
actions: ['logUnlit', 'logToggle', 'logCount'],
},
},
},
unlit: {
exit: () => {
console.log('it is light');
},
on: {
BREAK: 'broken',
TOGGLE: {
target: 'lit',
actions: ['logLit', 'logToggle', 'logCount'],
},
},
},
broken: { type: 'final', entry: ['logBroken', 'resetCount'] },
},
},
{
actions: {
logBroken: (context, event) => {
console.log(`I broke while ${context}`);
},
logLit: () => {
console.log('Lit!');
},
logUnlit: () => {
console.log('Unlit!');
},
logToggle: (context) => {
context.count++;
},
logCount: (context) => {
console.log(context.count)
},
resetCount: (context) => {
context.count = 0;
}
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment