Skip to content

Instantly share code, notes, and snippets.

@reid
Last active November 13, 2020 20:00
Show Gist options
  • Save reid/42a2f8d61ff73ea16894d671018a706d to your computer and use it in GitHub Desktop.
Save reid/42a2f8d61ff73ea16894d671018a706d 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: 'webpush',
initial: 'unsubscribed',
context: {
apiRequests: 0,
requestContext: {
guidHash: null,
appId: null
},
subscription: null,
subscriptionIds: null,
subscriptionIdsString: null,
storedSubscription: null,
storedSubscriptionIds: null,
permissionState: null,
registrationId: null,
validityPeriod: 7,
timestamp: 0
},
states: {
unsubscribed: {
on: {
SUBSCRIBE: [
{
target: 'refreshExistingSubscription',
cond: 'subscriptionExists'
},
{target: 'subscribe'}
]
}
},
refreshExistingSubscription: {
on: {
'': [
{
target: 'startOver',
cond: 'registrationIsMissing'
},
{target: 'registerAndAssociate'}
]
}
},
startOver: {
invoke: {
id: 'unsubscribe-and-retry',
src: 'unsubscribe',
onDone: {
target: 'subscribe',
actions: assign({
subscription: () => null
})
},
onError: 'failed'
}
},
unsubscribe: {
invoke: {
id: 'unsubscribe',
src: 'unsubscribe',
onDone: {
target: 'deleteAssociation',
actions: assign({
subscription: () => null
})
},
onError: 'failed'
}
},
deleteAssociation: {
invoke: {
id: 'deleteAssociation',
src: 'deleteAssociation',
onDone: {
target: 'unsubscribed',
actions: assign({
registrationId: () => null
})
}
}
},
subscribe: {
invoke: {
id: 'subscribe',
src: 'subscribe',
onDone: {
target: 'registerAndAssociate',
actions: assign({
subscription: (_, event) => event.data
})
},
onError: 'failed'
}
},
registerAndAssociate: {
on: {
'': [
{
target: 'updateAssociation',
cond: 'associationIsStale'
},
{
target: 'updateRegistration',
cond: 'subscriptionEndpointIsStale'
},
{target: 'addRegistration'}
]
}
},
addRegistration: {
exit: 'incrementApiRequestCounter',
invoke: {
id: 'addRegistration',
src: 'addRegistration',
onDone: {
target: 'addAssociation',
actions: assign({
registrationId: (_, event) => event.data.registrationId
})
},
onError: 'failed'
}
},
updateRegistration: {
exit: 'incrementApiRequestCounter',
invoke: {
id: 'updateRegistration',
src: 'updateRegistration',
onDone: 'updateAssociation',
onError: [
{
target: 'subscribe',
cond: 'failedBecauseRegistrationDoesNotExist'
},
{target: 'failed'}
]
}
},
addAssociation: {
invoke: {
id: 'addAssociation',
src: 'addAssociation',
onDone: 'complete',
onError: [
{
target: 'updateAssociation',
cond: 'failedBecauseAssociationDoesNotExist'
},
{target: 'failed'}
]
}
},
updateAssociation: {
invoke: {
id: 'updateAssociation',
src: 'updateAssociation',
onDone: 'complete',
onError: [
{
target: 'addRegistration',
cond: 'failedBecauseAssociationAlreadyExists'
}
]
}
},
failed: {
type: 'final'
},
complete: {
entry: ['updateTimestamp'],
invoke: {
id: 'saveContextToStorage',
src: 'saveContextToStorage',
onDone: {
target: 'processPermissionGrant',
cond: 'previousPermissionStateWasNotGranted'
}
}
},
processPermissionGrant: {
entry: ['onGrantedPermissions'],
on: {
'': 'subscribed'
}
},
subscribed: {
on: {
UNSUBSCRIBE: 'unsubscribe'
}
}
}
},
{
actions: {
updateTimestamp: context => (context.timestamp = String(Date.now())),
onGrantedPermissions: () => {}
},
services: {
subscribe: () => {},
unsubscribe: () => {},
addRegistration: () => {},
updateRegistration: () => {},
addAssociation: () => {},
updateAssociation: () => {},
saveContextToStorage: () => {}
},
guards: {
subscriptionExists: () => {},
registrationIsMissing: () => {},
associationIsStale: () => {},
subscriptionEndpointIsStale: () => {},
failedBecauseRegistrationDoesNotExist: () => {},
failedBecauseAssociationDoesNotExist: () => {},
failedBecauseAssociationAlreadyExists: () => {},
previousPermissionStateWasNotGranted: () => {}
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment