Skip to content

Instantly share code, notes, and snippets.

@jkarsrud
Last active May 22, 2020 11:01
Show Gist options
  • Save jkarsrud/3fadc32896fc31be3cb38cace2368d9f to your computer and use it in GitHub Desktop.
Save jkarsrud/3fadc32896fc31be3cb38cace2368d9f to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const now = new Date();
const vehicles = [
{ availableFrom: now },
{ availableFrom: now.setMinutes(now.getMinutes() + 5) },
{ availableFrom: null }
];
const VehicleTypeMachine = Machine({
id: 'VehicleType',
initial: 'unknown',
context: {
vehicles,
},
states: {
unknown: {
on: {
'': [
{ target: 'available', cond: 'isAvailable'},
{ target: 'incoming', cond: 'isIncoming'},
{ target: 'unavailable' }
]
}
},
available: {},
incoming: {},
unavailable: {},
},
},
{
guards: {
isAvailable(context) {
return context.vehicles.filter(vehicle => vehicle.availableFrom).some(vehicle => vehicle.availableFrom <= new Date());
},
isIncoming(context) {
const unrented = context.vehicles
.filter(vehicle => vehicle.availableFrom);
return unrented.length ?
unrented.every(vehicle => vehicle.availableFrom > new Date()) : false;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment