diff options
| author | Henry Jameson <me@hjkos.com> | 2018-12-20 09:17:59 +0300 |
|---|---|---|
| committer | Henry Jameson <me@hjkos.com> | 2018-12-20 09:17:59 +0300 |
| commit | 957b2a6f7ef8ca381fc44b79a17639e400407b67 (patch) | |
| tree | a0eb359a268b243f34b33babf72ba4f5fc317645 /src/main.js | |
| parent | d0b47488fb69b2b8449fa73e8d94e2cdcc9adda8 (diff) | |
simplified some code, made it possible to unregister serviceworker altogether
Diffstat (limited to 'src/main.js')
| -rw-r--r-- | src/main.js | 25 |
1 files changed, 11 insertions, 14 deletions
diff --git a/src/main.js b/src/main.js index bf92e78e..c22a762e 100644 --- a/src/main.js +++ b/src/main.js @@ -54,24 +54,21 @@ const persistedStateOptions = { const registerPushNotifications = store => { store.subscribe((mutation, state) => { const vapidPublicKey = state.instance.vapidPublicKey + const webPushNotification = state.config.webPushNotifications const permission = state.interface.notificationPermission === 'granted' - const isUserMutation = mutation.type === 'setCurrentUser' - - if (isUserMutation && vapidPublicKey && permission) { - return store.dispatch('registerPushNotifications') - } - const user = state.users.currentUser - const isVapidMutation = mutation.type === 'setInstanceOption' && mutation.payload.name === 'vapidPublicKey' - - if (isVapidMutation && user && permission) { - return store.dispatch('registerPushNotifications') - } + const isUserMutation = mutation.type === 'setCurrentUser' + const isVapidMutation = mutation.type === 'setInstanceOption' && mutation.payload.name === 'vapidPublicKey' const isPermMutation = mutation.type === 'setNotificationPermission' && mutation.payload === 'granted' - - if (isPermMutation && user && vapidPublicKey) { - return store.dispatch('registerPushNotifications') + const isUserConfigMutation = mutation.type === 'setOption' && mutation.payload.name === 'webPushNotifications' + + if (isUserMutation || isVapidMutation || isPermMutation || isUserConfigMutation) { + if (user && vapidPublicKey && permission && webPushNotification) { + return store.dispatch('registerPushNotifications') + } else if (isUserConfigMutation && !webPushNotification) { + return store.dispatch('unregisterPushNotifications') + } } }) } |
