Skip to content

Instantly share code, notes, and snippets.

@Ric-Lavers
Last active March 29, 2020 00:42
Show Gist options
  • Save Ric-Lavers/37a6d2c4e4d160b4b859514407b528ba to your computer and use it in GitHub Desktop.
Save Ric-Lavers/37a6d2c4e4d160b4b859514407b528ba 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 carDoorMachine = Machine({
id: "carDoor",
initial: "idle",
context: {
isOpen: true,
},
states: {
idle: {
on: {
DOWN_BUTTON_PRESSED: {
target: "lowering",
actions: assign(context => ({ ...context, isOpen: "pending" })),
cond: ({ isOpen }) => isOpen || isOpen === "pending",
},
UP_BUTTON_PRESSED: {
target: "rising",
actions: assign(context => ({ ...context, isOpen: "pending" })),
cond: ({ isOpen }) => !isOpen || isOpen === "pending",
},
},
},
lowering: {
on: {
DOWN_BUTTON_PRESSED: { target: "idle" },
UP_BUTTON_PRESSED: "rising",
},
after: {
3000: {
target: "idle",
actions: assign(context => {
return {
...context,
isOpen: false,
}
}),
},
},
},
rising: {
on: {
UP_BUTTON_PRESSED: { target: "idle" },
DOWN_BUTTON_PRESSED: "lowering",
},
after: {
3000: {
target: "idle",
actions: assign(context => {
return {
...context,
isOpen: true,
}
}),
},
},
},
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment