Last active
May 22, 2020 11:01
-
-
Save jkarsrud/3fadc32896fc31be3cb38cace2368d9f to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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