Skip to content

Instantly share code, notes, and snippets.

@2WheelCoder
Last active July 30, 2021 00:08
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 2WheelCoder/3c97ba4beeec28bf9f971075f537e778 to your computer and use it in GitHub Desktop.
Save 2WheelCoder/3c97ba4beeec28bf9f971075f537e778 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const updateMapLayerByKey = (key) => (context, event) => event.key === key ? event.newValue : context[key]
const updateMapLayers = assign({
geos: updateMapLayerByKey('geos'),
networkLinks: updateMapLayerByKey('networkLinks'),
transitStops: updateMapLayerByKey('transitStops'),
transitRoutes: updateMapLayerByKey('transitRoutes'),
})
const updateThenLoadTransition = {
on: {
UPDATE: {
target: 'loadingData',
actions: ['updateMapLayers']
}
}
}
const getDataFromServer = () => {
// fetch data here and trigger a RESOLVE or REJECT event on completion
}
const explorerMachine = Machine({
id: 'explorer',
initial: 'idle',
context: {
geos: [],
networkLinks: [],
transitStops: [],
transitRoutes: [],
},
states: {
idle: {
on: {
CLICK_EDIT_GEOS: 'selectingGeos',
CLICK_EDIT_NETWORK_LINKS: 'selectingNetworkLinks',
CLICK_EDIT_TRANSIT_STOPS: 'selectingTransitStops',
CLICK_EDIT_TRANSIT_ROUTES: 'selectingTransitRoutes',
}
},
loadingData: {
on: {
RESOLVE: 'idle',
REJECT: 'error'
}
},
selectingGeos: {
on: {
UPDATE: [{
target: 'loadingData',
actions: ['updateMapLayers'],
cond: 'hasGeosSelected'
}, {
target: 'error'
}]
}
},
selectingNetworkLinks: updateThenLoadTransition,
selectingTransitStops: updateThenLoadTransition,
selectingTransitRoutes: updateThenLoadTransition,
error: {
RESOLVE: 'idle'
}
}
}, {
actions: {
updateMapLayers,
getDataFromServer,
},
guards: {
hasGeosSelected: (context) => context.geos.length
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment