Skip to content

Instantly share code, notes, and snippets.

@ecancino
Last active April 27, 2020 00:23
Show Gist options
  • Save ecancino/c77ca27215f1c2d69f75be2d35abfd6e to your computer and use it in GitHub Desktop.
Save ecancino/c77ca27215f1c2d69f75be2d35abfd6e to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const delay = timer => ctx =>
ctx.multiplier * timer
const stoplight = Machine({
id: "stoplight",
strict: true,
context: {
multiplier: 2
},
on: {
INC_MULTIPLIER: {
actions: ['rushHour']
}
},
initial: 'red',
states: {
green: {
after: {
GREEN_TIMER: 'yellow'
}
},
yellow: {
after: {
YELLOW_TIMER: 'red'
}
},
red: {
after: {
RED_TIMER: 'green'
}
}
}
}, {
actions: {
rushHour: assign({
multiplier: ctx => ctx.multiplier + 1
})
},
delays: {
GREEN_TIMER: delay(3000),
YELLOW_TIMER: delay(1000),
RED_TIMER: delay(4000),
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment