Skip to content

Instantly share code, notes, and snippets.

@rthor
Created September 21, 2019 23:42
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/ff280132f2608fee2fe314fe8075aa9a to your computer and use it in GitHub Desktop.
Save rthor/ff280132f2608fee2fe314fe8075aa9a to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const activeTripMachine = Machine({
id: 'activeTrip',
initial: 'init',
context: {
trip: null,
lastTrip: null,
pollInterval: 15000,
},
states: {
init: {
exit: ['hasActiveTrip'],
on: {
NEXT: 'idle',
},
},
idle: {
on: {
START_TRIP: 'starting',
RESTART: 'active',
SERVER_STOP: 'stoppedByServer',
},
},
starting: {
invoke: {
id: 'startingTrip',
src: 'startTrip',
onDone: {
target: 'active',
actions: assign({
trip: (_, event) => event.data,
}),
},
onError: 'idle',
},
},
stopping: {
invoke: {
id: 'stoppingTrip',
src: 'stopTrip',
onDone: 'ended',
onError: 'active',
},
},
stoppedByServer: {
onEntry: assign({
lastTrip: (_, { lastTrip }) => lastTrip,
}),
on: {
NEXT: 'ended',
},
},
ended: {
onEntry: assign({
trip: () => null,
}),
on: {
NEXT: 'idle',
},
},
active: {
activities: ['polling'],
on: {
END_TRIP: 'stopping',
SERVER_STOP: 'stoppedByServer',
STOP_POLLING: 'init',
},
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment