Last active
July 30, 2021 00:08
-
-
Save 2WheelCoder/3c97ba4beeec28bf9f971075f537e778 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
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