Skip to content

Instantly share code, notes, and snippets.

@Codyooo
Created March 16, 2021 03:49
Show Gist options
  • Select an option

  • Save Codyooo/254fa1764291d944bbea64725e0dbb52 to your computer and use it in GitHub Desktop.

Select an option

Save Codyooo/254fa1764291d944bbea64725e0dbb52 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
/**
* @file: description
* @author: hufan
* @Date: 2021-03-12 17:27:49
* @LastEditors: hufan
* @LastEditTime: 2021-03-12 19:00:31
*/
const stopLightMachine = Machine(
{
id: "stopLight",
initial: "green",
context: {
currentlight: "green",
},
states: {
green: {
entry: assign({
currentlight: "green",
}),
after: {
GREEN_TIMER: "greenB",
},
},
greenB: {
after: {
GREEN_BLINK_TIMER: "yellow",
},
},
yellow: {
after: {
YELLOW_TIMER: "yellowB",
},
},
yellowB: {
after: {
YELLOW_BLINK_TIMER: [
{
target: "red",
cond: (context, event) => {
console.log("context.currentlight", context.currentlight);
if (context.currentlight === "green") return true;
return false;
},
},
{
target: "green",
cond: (context, event) => {
if (context.currentlight === "red") return true;
return false;
},
},
],
},
},
red: {
after: {
RED_TIMER: "redB",
},
entry: assign({
currentlight: "red",
}),
},
redB: {
after: {
RED_BLINK_TIMER: "yellow",
},
},
hist: {
type: "history",
},
},
},
{
delays: {
GREEN_TIMER: 3000,
GREEN_BLINK_TIMER: 1000,
YELLOW_TIMER: 2000,
YELLOW_BLINK_TIMER: 3000,
RED_TIMER: 3000,
RED_BLINK_TIMER: 1000,
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment