mirror of
https://github.com/HeyPuter/puter.git
synced 2026-11-04 13:03:37 +00:00
Merge juancastro/put-2171-push-service-worker into juancastro/put-2172-notifications-settings
This commit is contained in:
commit
2ad49e1040
2 files changed
+5
-28
No files matched your search
@@ -124,25 +124,6 @@ export const showLocalBanner = async ({ uid, notification, appUid = null }, { re
|
||||
}
|
||||
};
|
||||
|
||||
const isPuterWorker = (registration) => [registration?.active, registration?.waiting, registration?.installing]
|
||||
.some((worker) => {
|
||||
try {
|
||||
return !! worker && new URL(worker.scriptURL).pathname === WORKER_URL;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
/** @param {Navigator} nav */
|
||||
const unregisterPuterWorker = async (nav) => {
|
||||
try {
|
||||
const registrations = await nav?.serviceWorker?.getRegistrations?.();
|
||||
await Promise.all((registrations ?? []).filter(isPuterWorker).map((r) => r.unregister()));
|
||||
} catch ( err ) {
|
||||
console.warn('Could not unregister the service worker:', err);
|
||||
}
|
||||
};
|
||||
|
||||
let started = false;
|
||||
|
||||
/**
|
||||
@@ -166,10 +147,8 @@ export const startPushWorker = async ({
|
||||
if ( opened ) dispatchStoredTap(opened);
|
||||
}
|
||||
|
||||
if ( ! pushWorkerEnabled(params, nav) ) {
|
||||
await unregisterPuterWorker(nav);
|
||||
return;
|
||||
}
|
||||
// Leave an existing worker: removing it ends this browser's subscription.
|
||||
if ( ! pushWorkerEnabled(params, nav) ) return;
|
||||
nav.serviceWorker.addEventListener('message', (event) => {
|
||||
if ( event.data?.type !== WORKER_CLICK_MESSAGE || ! normalizePushTap(event.data.notification) ) return;
|
||||
event.source?.postMessage?.({ type: WORKER_CLICK_ACK_MESSAGE, id: event.data.id });
|
||||
|
||||
@@ -226,14 +226,12 @@ describe('startPushWorker', () => {
|
||||
expect(nav.serviceWorker.register).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('unregisters an existing /sw.js where push is off, and registers nothing', async () => {
|
||||
it('leaves an existing /sw.js alone where push is off, and registers nothing', async () => {
|
||||
const puter = { active: { scriptURL: 'https://puter.test/sw.js' }, unregister: vi.fn(async () => true) };
|
||||
const other = { active: { scriptURL: 'https://puter.test/other.js' }, unregister: vi.fn() };
|
||||
const nav = makeNav([puter, other]);
|
||||
const nav = makeNav([puter]);
|
||||
await start(await load(), { nav, params: { pushEnabled: false } });
|
||||
expect(nav.serviceWorker.register).not.toHaveBeenCalled();
|
||||
expect(puter.unregister).toHaveBeenCalledOnce();
|
||||
expect(other.unregister).not.toHaveBeenCalled();
|
||||
expect(puter.unregister).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('hands over the notification uid a page was opened for and clears it', async () => {
|
||||
|
||||
Reference in new issue
Block a user