Created
August 26, 2022 14:50
-
-
Save machty/a7156da29997e1e08c104db54b13b795 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
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const fetchMachine = Machine({ | |
initial: 'inactive', | |
context: { | |
// Location[] | null | |
locations: null, | |
// Location | null | |
currentLocation: null, | |
// Coords | |
coords: null, | |
// ProfileKey | |
appProfileKey: '', // passed from outside, | |
// Date | null | |
lastLocationRefresh: null, | |
}, | |
states: { | |
inactive: { | |
on: { | |
START: 'active', | |
}, | |
}, | |
active: { | |
initial: 'geolocating', | |
states: { | |
geolocating: { | |
invoke: { | |
src: 'geolocateUser', | |
onDone: 'geolocateSuccess', | |
onError: 'geolocateError', | |
}, | |
}, | |
geolocateSuccess: { | |
entry: ['assignCoords', 'resetLocationOnCurrent'], | |
exit: ['unassignCoords'], | |
initial: 'queryingNearbyCheckoutLocations', | |
on: { | |
// no need to drop - we get that for free with a statechart | |
REFRESH: { | |
target: 'geolocating', | |
actions: ['trackRefresh'], | |
}, | |
}, | |
states: { | |
queryingNearbyCheckoutLocations: { | |
invoke: { | |
src: 'queryNearbyCheckoutLocations', | |
onDone: [ | |
{ | |
target: 'storesAvailable', | |
actions: [ | |
'assignLocationData', | |
'showLocations', | |
'trackStoreLookupInStore', | |
], | |
cond: 'locationsAvailable', | |
}, | |
{ | |
target: 'noStores', | |
actions: [ | |
'assignLocationData', | |
'showLocations', | |
'trackStoreLookupEmpty', | |
], | |
}, | |
], | |
onError: 'queryError', | |
}, | |
}, | |
storesAvailable: { | |
// show locate-stores/location-picker | |
}, | |
noStores: { | |
// this is modeled implicitly in the current code | |
// this is actually a substate and | |
// displays a different template | |
// `locate-stores/not-at-location` | |
}, | |
queryError: { | |
// handle query error here | |
}, | |
}, | |
}, | |
geolocateError: { | |
// we know geolocate failed -> handle accordingly | |
on: { | |
REFRESH: { target: 'geolocating', actions: ['trackRefresh'] }, | |
}, | |
}, | |
}, | |
on: { | |
STOP: 'inactive', | |
}, | |
}, | |
}, | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment