Skip to content

Instantly share code, notes, and snippets.

@schpet
Created April 14, 2021 21:29
Show Gist options
  • Save schpet/9cacffee8198c470f69cc84a9e965b45 to your computer and use it in GitHub Desktop.
Save schpet/9cacffee8198c470f69cc84a9e965b45 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: "pending",
context: {
currentLocation: null,
locationError: null,
locationPersistError: null,
locationPersistAttempted: false,
},
states: {
pending: {
on: {
CHECK_PERMISSIONS: "checkingPermissions",
},
},
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: {
on: {
REREQUEST_PERMISSION: "checkingPermissions",
},
},
fetchingLocation: {
invoke: {
src: "readCurrentLocation",
onDone: {
target: "persistingLocation",
actions: assign({ currentLocation: (_context, event) => event.data }),
},
onError: {
target: "locationError",
actions: assign({ locationError: (_context, event) => event.data }),
},
},
},
persistingLocation: {
invoke: {
src: "persistCurrentLocation",
},
on: {
LOCATION_PERSISTED: {
target: "friendsNearby",
actions: assign({
locationPersistError: (_context, _event) => null,
locationPersistAttempted: (_context, _event) => true,
}),
},
LOCATION_PERSIST_FAILED: {
target: "locationPersistError",
actions: assign({
locationPersistError: (_context, event) => event.error,
locationPersistAttempted: (_context, _event) => true,
}),
},
},
},
friendsNearby: {
on: {
REREQUEST_PERMISSION: "checkingPermissions",
},
},
locationPersistError: {
on: {
REREQUEST_PERMISSION: "checkingPermissions",
},
},
locationError: {},
unsupportedPlatform: {},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment