Prevent WebPush init from attaching events multiple times
This commit is contained in:
parent
eac82db8f4
commit
e93531a4af
|
@ -3,25 +3,25 @@ import { enableHandler } from './enable';
|
||||||
import { dismissHandler } from "./dismiss";
|
import { dismissHandler } from "./dismiss";
|
||||||
import { unsubscribeHandler, checkSubscription } from "retrospring/features/webpush/unsubscribe";
|
import { unsubscribeHandler, checkSubscription } from "retrospring/features/webpush/unsubscribe";
|
||||||
|
|
||||||
let subscriptionChecked = false;
|
let initialized = false;
|
||||||
|
|
||||||
export default (): void => {
|
export default (): void => {
|
||||||
const swCapable = 'serviceWorker' in navigator;
|
const swCapable = 'serviceWorker' in navigator;
|
||||||
const notificationCapable = 'Notification' in window;
|
const notificationCapable = 'Notification' in window;
|
||||||
|
|
||||||
|
// We want to adjust enable/disable buttons on every page load
|
||||||
|
// because the enable button appears on both the settings and inbox pages.
|
||||||
if (swCapable && notificationCapable) {
|
if (swCapable && notificationCapable) {
|
||||||
const enableBtn = document.querySelector('button[data-action="push-enable"]');
|
const enableBtn = document.querySelector('button[data-action="push-enable"]');
|
||||||
|
|
||||||
|
|
||||||
navigator.serviceWorker.getRegistration().then(async registration => {
|
navigator.serviceWorker.getRegistration().then(async registration => {
|
||||||
const subscription = await registration?.pushManager.getSubscription();
|
const subscription = await registration?.pushManager.getSubscription();
|
||||||
if (subscription) {
|
if (subscription) {
|
||||||
document.querySelector('button[data-action="push-enable"]')?.classList.add('d-none');
|
document.querySelector('button[data-action="push-enable"]')?.classList.add('d-none');
|
||||||
document.querySelector('[data-action="push-disable"]')?.classList.remove('d-none');
|
document.querySelector('[data-action="push-disable"]')?.classList.remove('d-none');
|
||||||
|
|
||||||
if (!subscriptionChecked) {
|
if (!initialized) {
|
||||||
checkSubscription(subscription);
|
checkSubscription(subscription);
|
||||||
subscriptionChecked = true;
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -34,15 +34,19 @@ export default (): void => {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
registerEvents([
|
if (!initialized) {
|
||||||
{type: 'click', target: '[data-action="push-enable"]', handler: enableHandler, global: true},
|
registerEvents([
|
||||||
{type: 'click', target: '[data-action="push-dismiss"]', handler: dismissHandler, global: true},
|
{type: 'click', target: '[data-action="push-enable"]', handler: enableHandler, global: true},
|
||||||
{type: 'click', target: '[data-action="push-disable"]', handler: unsubscribeHandler, global: true},
|
{type: 'click', target: '[data-action="push-dismiss"]', handler: dismissHandler, global: true},
|
||||||
{
|
{type: 'click', target: '[data-action="push-disable"]', handler: unsubscribeHandler, global: true},
|
||||||
type: 'click',
|
{
|
||||||
target: '[data-action="push-remove-all"]',
|
type: 'click',
|
||||||
handler: unsubscribeHandler,
|
target: '[data-action="push-remove-all"]',
|
||||||
global: true
|
handler: unsubscribeHandler,
|
||||||
},
|
global: true
|
||||||
]);
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
initialized = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue