Skip to content

Instantly share code, notes, and snippets.

@schpet
Created March 2, 2021 21:40
Show Gist options
  • Save schpet/535d2060aa15743e18c99b706d7e394c to your computer and use it in GitHub Desktop.
Save schpet/535d2060aa15743e18c99b706d7e394c 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({
id: "nearbyFriends",
initial: "checkingPermissions",
context: { currentLocation: null, locationError: null },
states: {
checkingPermissions: {
invoke: {
src: "checkPermissions",
},
on: {
PERMISSIONS_BLOCKED: "permissionsBlocked",
PERMISSIONS_GRANTED: "fetchingLocation",
PERMISSIONS_REQUESTABLE: "permissionsPending",
PLATFORM_NOT_SUPPORTED: "unsupportedPlatform",
},
},
permissionsPending: {
on: {
REQUEST_PERMISSION: "permissionsRequested",
},
},
permissionsRequested: {
invoke: {
src: "requestPermissions",
onDone: "fetchingLocation",
onError: "permissionsBlocked",
},
},
permissionsBlocked: {},
fetchingLocation: {
invoke: {
src: "readCurrentLocation",
onDone: {
target: "friendsNearby",
actions: assign({ currentLocation: (_context, event) => event.data }),
},
onError: {
target: "locationError",
actions: assign({ locationError: (_context, event) => event.data }),
},
},
},
friendsNearby: {
invoke: {
src: "persistCurrentLocation",
},
},
locationError: {},
unsupportedPlatform: {},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment