/// <reference lib="webworker" />
export default null;
declare let self: ServiceWorkerGlobalScope

addEventListener('install', event => {
    self.skipWaiting();
});

addEventListener('push', (event) => {
    (event as ExtendableMessageEvent).waitUntil((async () => {
        if ((event as ExtendableMessageEvent).data) {
            const eventdata = JSON.parse((event as ExtendableMessageEvent).data.text());
            await self.clients.matchAll({ includeUncontrolled: true, type: 'window' }).then(clients => {
                clients.forEach(client => client.postMessage(eventdata));
            });
        } else {
            console.error('This push event has no data.');
        }
    })());
});

addEventListener('notificationerror', console.error);