Skip to content

Instantly share code, notes, and snippets.

@rthor
Last active September 24, 2019 10:32
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 rthor/6c4892fa059e38ef71270cbbebe6cf38 to your computer and use it in GitHub Desktop.
Save rthor/6c4892fa059e38ef71270cbbebe6cf38 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const activeTripMachine = Machine(
{
id: 'activeTrip',
initial: 'inactive',
context: {
trip: null,
errorCode: null,
},
states: {
inactive: {
invoke: {
src: 'apiCheckLastTrip',
},
on: {
START_TRIP: 'starting',
RESTART_LAST_TRIP: {
actions: 'onSaveActiveTripToStore',
target: 'active',
},
SERVER_STOP: 'stoppedByServer',
},
},
starting: {
invoke: {
id: 'startingTrip',
src: 'apiStartTrip',
onDone: {
actions: 'onSaveActiveTripToStore',
target: 'active',
},
onError: {
target: 'inactive',
actions: 'onShowError',
},
},
},
active: {
initial: 'polling',
on: {
END_TRIP: '.checkingIfParkingIsValid',
SERVER_STOP: 'stoppedByServer',
},
states: {
polling: {
initial: 'updating',
states: {
getActiveTrip: {
invoke: {
id: 'apiGetActiveTrip',
src: 'apiGetActiveTrip',
onDone: 'updating',
},
},
updating: {
entry: 'onUpdateTrip',
after: {
POLLING_INTERVAL: 'getActiveTrip',
},
},
},
},
checkingIfParkingIsValid: {
invoke: {
id: 'canPark',
src: 'apiCanParkCheck',
},
on: {
CAN_PARK: '#activeTrip.endTrip',
CANNOT_PARK: {
actions: 'onSetErrorCode',
target: 'cannotEndRide',
},
},
},
cannotEndRide: {
onExit: 'onSetErrorCode',
on: {
CONTINUE_TRIP: 'polling',
END_TRIP: '#activeTrip.endTrip',
},
},
},
},
endTrip: {
id: 'endTrip',
type: 'parallel',
onDone: 'showingTripSummary',
states: {
payment: {
initial: 'paying',
states: {
paying: {
invoke: {
src: 'apiStopTrip',
onDone: {
actions: 'onUpdateTrip',
target: 'paid',
},
},
on: {
CANNOT_PARK: {
actions: 'onSetErrorCode',
target: '#activeTrip.active.cannotEndRide',
},
},
},
paid: {
entry: 'onRemoveActiveTripFromStore',
type: 'final',
},
},
},
parking: {
initial: 'takingAPhoto',
states: {
takingAPhoto: {
on: {
PARK: 'parked',
},
},
parked: {
type: 'final',
},
},
},
},
},
stoppedByServer: {
entry: ['onUpdateTrip', 'onRemoveActiveTripFromStore'],
on: {
NEXT: 'showingTripSummary',
},
},
showingTripSummary: {
on: {
NEXT: 'inactive',
},
},
},
},
{
delays: {
POLLING_INTERVAL: 15000,
},
actions: {
onUpdateTrip: assign({
trip: ({ trip }, { data }) => (data ? data.trip : trip),
}),
onSetErrorCode: assign({
errorCode: (_, { errorCode }) => errorCode,
}),
},
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment