Skip to content

Instantly share code, notes, and snippets.

@kevinbarabash
Created July 17, 2020 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinbarabash/bf50ca74b77ca728e0f29b865f37f8d7 to your computer and use it in GitHub Desktop.
Save kevinbarabash/bf50ca74b77ca728e0f29b865f37f8d7 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
Machine({
id: "light",
initial: "green",
states: {
red: {
initial: "no_cars",
states: {
no_cars: {
on: {
SENSE_CAR: "cars",
},
meta: {test: async wrapper => {}},
},
cars: {
after: {
1800: "#light.green", // if a car has waited more than 1.8s, switch to green
},
meta: {test: async wrapper => {}},
}
},
after: {
6000: "green", // change after 6s regardless of whether there are cars or not
},
meta: {test: async wrapper => {}},
},
yellow: {
initial: "active",
states: {
active: {
meta: {test: async wrapper => {}},
}, // no internal transitions since we always wait the same time
},
after: {
1800: "red",
},
meta: {
test: async wrapper => {
},
},
},
green: {
initial: "waiting_for_cars",
states: {
waiting_for_cars: {
after: {
// If we haven't received a SENSE_CAR event after 2s change lights
2000: "#light.yellow",
},
on: {
// Once we sense a car, the car drives through the intersection,
// and we start waiting for the next one. This will restart the
// the "after" timer.
SENSE_CAR: "waiting_for_cars",
},
meta: {test: async wrapper => {}},
},
},
after: {
5000: "yellow", // waiting with traffic, since "after" timer gets reset
},
meta: {test: async wrapper => {}},
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment