Skip to content

Instantly share code, notes, and snippets.

@tamebadger
Created January 29, 2020 16:23
Show Gist options
  • Save tamebadger/90c84472a8a9371726db077e227907f5 to your computer and use it in GitHub Desktop.
Save tamebadger/90c84472a8a9371726db077e227907f5 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
function isDifferentFlight(flight1, flight2) {
let valid =
(!flight1 && flight2) ||
(
flight1 && flight2 &&
(
flight1.state !== flight2.state ||
flight1.flightNumber !== flight2.flightNumber ||
flight1.takeoffDateTime !== flight2.takeoffDateTime ||
flight1.originIATA !== flight2.originIATA ||
flight1.originICAO !== flight2.originICAO
)
);
return valid;
}
function isValidFlight(flight) {
let valid = (
flight &&
flight.originIATA &&
flight.originIATA.length > 0 &&
flight.originICAO &&
flight.originICAO.length > 0 &&
flight.takeoffDateTime &&
flight.flightNumber &&
flight.flightNumber.length > 0 &&
flight.state &&
flight.state.length > 0
);
return valid;
}
const openFlight = () => {}
const closeFlight = () => {}
const machine = Machine({
id: 'flightdata',
initial: 'unknown',
context: { flightInfo: {} },
states: {
unknown: {
on: {
'': [
{ target: 'open', cond: 'isFlightOpen' },
{ target: 'closed', cond: 'isFlightClosed' }
],
'FLIGHT_DATA_CHANGE': {
actions: 'updateClosedFlightData'
}
}
},
closing: {
invoke: {
id: 'closeFlight',
src: 'closeFlight', // see the corresponding entry in the services object
onDone: { target: 'closed' },
onError: { target: 'unknown' }
}
},
closed: {
on: {
'': [ { target: 'opening', cond: 'isFlightOpen' } ],
'FLIGHT_DATA_CHANGE': {
actions: 'updateClosedFlightData'
}
}
},
opening: {
invoke: {
id: 'openFlight',
src: 'openFlight', // see the corresponding entry in the services object
onDone: { target: 'open' },
onError: { target: 'unknown' }
}
},
open: {
on: {
'': [ { target: 'closing', cond: 'isFlightClosed' } ],
'FLIGHT_DATA_CHANGE': {
actions: [
'updateOpenFlightData'
]
}
}
}
}
},
{
actions: {
// action implementations
updateOpenFlightData: assign((context, event) => { return {} }),
updateClosedFlightData: assign((context, event) => { return {} })
},
guards: {
isFlightOpen: (context, event) => true,
isFlightClosed: (context, event) => false
},
services: {
openFlight,
closeFlight
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment