Skip to content

Instantly share code, notes, and snippets.

@CAWeissen
Created February 27, 2020 04:06
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 CAWeissen/3b6207db041fdd694012df28f74bd1bc to your computer and use it in GitHub Desktop.
Save CAWeissen/3b6207db041fdd694012df28f74bd1bc to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const pedestrianStates = {
initial: 'walk',
states: {
walk: {
after: {
1000: {
target: 'wait',
cond: 'pedestrians'
}
},
},
wait: {
after: {
500: 'stop'
},
},
stop: {
entry: assign({pedActivated: (context, event) => false})
}
}
};
const lightMachine = Machine(
{
id: 'pedestrianLight',
initial: 'green',
context: {
pedActivated: false
},
states: {
green: {
on: {
TIMER: 'yellow',
ACTIVATE_PED: {
target: 'green',
actions: assign({pedActivated: (context, event) => true})
}
},
after: {
2000: 'yellow'
}
},
yellow: {
on: {
TIMER: 'red',
ACTIVATE_PED: {
target: 'yellow',
actions: assign({pedActivated: (context, event) => true})
}
},
after: {
2000: 'red'
}
},
red: {
on: {
TIMER: 'green',
ACTIVATE_PED: {
target: 'red',
actions: assign({pedActivated: (context, event) => true})
}
},
after: {
2000: {
target: 'green',
cond: 'noPedestrians'
}
},
...pedestrianStates
}
},
},
{
guards: {
noPedestrians: context => context.pedActivated === false,
pedestrians: context => context.pedActivated === true
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment