Created
November 13, 2020 19:46
-
-
Save reid/020536b5a6a334877a02ff2d100bf0cd to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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
| // 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' | |
| }, | |
| 'subscribe' | |
| ] | |
| } | |
| }, | |
| refreshExistingSubscription: { | |
| on: { | |
| '': [ | |
| { | |
| target: 'startOver', | |
| cond: 'registrationIsMissing' | |
| }, | |
| '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