Skip to content

Instantly share code, notes, and snippets.

@machty
Created August 26, 2022 14:50
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 machty/a7156da29997e1e08c104db54b13b795 to your computer and use it in GitHub Desktop.
Save machty/a7156da29997e1e08c104db54b13b795 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// 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