Skip to content

Instantly share code, notes, and snippets.

@matthewwoodruff
Created September 9, 2020 19:48
Show Gist options
  • Save matthewwoodruff/ea92fa9e7fe86e4ca5d94f0ea2ee0f91 to your computer and use it in GitHub Desktop.
Save matthewwoodruff/ea92fa9e7fe86e4ca5d94f0ea2ee0f91 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const zombieMachine = Machine({
id: 'Armoured Door',
initial: 'closed',
context: {
door_gap: 0,
},
states: {
locked: {
initial: 'main',
states: {
main: {
on: {
LOCK_DEADBOLT: 'deadbolt',
UNLOCK: '#closed'
}
},
deadbolt: {
on: {
UNLOCK_DEADBOLT: 'main'
}
}
}
},
closed: {
id: 'closed',
on: {
OPEN: [
{
actions: assign({door_gap: () => 100}),
target: 'open'
}
],
LOCK: 'locked'
}
},
open: {
on: {
CLOSE: [
{
cond: (context, event) => context.door_gap - (event.amount ?? context.door_gap) > 0,
actions: assign({door_gap: (context, event) => context.door_gap - (event.amount ?? context.door_gap)}),
},
{
target: 'closed',
actions: assign({door_gap: () => 0}),
}
]
},
},
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment