From dcb846324d82a8663e284f4969ba4ea36719a027 Mon Sep 17 00:00:00 2001 From: kolaente Date: Mon, 26 Jul 2021 23:09:49 +0200 Subject: [PATCH] Cleanup broken sw functions --- src/models/namespace.js | 2 +- src/models/task.js | 4 --- src/registerServiceWorker.js | 24 ------------- src/sw.js | 65 +----------------------------------- 4 files changed, 2 insertions(+), 93 deletions(-) diff --git a/src/models/namespace.js b/src/models/namespace.js index 88c049aa..823ccf08 100644 --- a/src/models/namespace.js +++ b/src/models/namespace.js @@ -41,4 +41,4 @@ export default class NamespaceModel extends AbstractModel { updated: null, } } -} \ No newline at end of file +} diff --git a/src/models/task.js b/src/models/task.js index 10b8f855..7beeca23 100644 --- a/src/models/task.js +++ b/src/models/task.js @@ -223,10 +223,6 @@ export default class TaskModel extends AbstractModel { icon: '/images/icons/android-chrome-512x512.png', data: {taskId: this.id}, actions: [ - { - action: 'mark-as-done', - title: 'Done', - }, { action: 'show-task', title: 'Show task', diff --git a/src/registerServiceWorker.js b/src/registerServiceWorker.js index b5edffd9..a497d63a 100644 --- a/src/registerServiceWorker.js +++ b/src/registerServiceWorker.js @@ -1,7 +1,6 @@ /* eslint-disable no-console */ import {register} from 'register-service-worker' -import {getToken} from './helpers/auth' if (import.meta.env.PROD) { register('/sw.js', { @@ -32,26 +31,3 @@ if (import.meta.env.PROD) { }, }) } - -if (navigator && navigator.serviceWorker) { - navigator.serviceWorker.addEventListener('message', event => { - // for every message we expect an action field - // determining operation that we should perform - const {action} = event.data - // we use 2nd port provided by the message channel - const port = event.ports[0] - - if (action === 'getBearerToken') { - console.debug('Token request from sw') - port.postMessage({ - authToken: getToken(), - }) - } else { - console.error('Unknown event', event) - port.postMessage({ - error: 'Unknown request', - }) - } - }) -} - diff --git a/src/sw.js b/src/sw.js index 05656792..1b5f876f 100644 --- a/src/sw.js +++ b/src/sw.js @@ -36,76 +36,12 @@ self.addEventListener('message', (e) => { } }) -const getBearerToken = async () => { - // we can't get a client that sent the current request, therefore we need - // to ask any controlled page for auth token - const allClients = await self.clients.matchAll() - const client = allClients.filter(client => client.type === 'window')[0] - - // if there is no page in scope, we can't get any token - // and we indicate it with null value - if (!client) { - return null - } - - // to communicate with a page we will use MessageChannels - // they expose pipe-like interface, where a receiver of - // a message uses one end of a port for messaging and - // we use the other end for listening - const channel = new MessageChannel() - - client.postMessage({ - 'action': 'getBearerToken', - }, [channel.port1]) - - // ports support only onmessage callback which - // is cumbersome to use, so we wrap it with Promise - return new Promise((resolve, reject) => { - channel.port2.onmessage = event => { - if (event.data.error) { - console.error('Port error', event.error) - reject(event.data.error) - } - - resolve(event.data.authToken) - } - }) -} - // Notification action self.addEventListener('notificationclick', function (event) { const taskId = event.notification.data.taskId event.notification.close() switch (event.action) { - case 'mark-as-done': - // FIXME: Ugly as hell, but no other way of doing this, since we can't use modules - // in service workers for now. - fetch('/config.json') - .then(r => r.json()) - .then(config => { - - getBearerToken() - .then(token => { - fetch(`${config.VIKUNJA_API_BASE_URL}tasks/${taskId}`, { - method: 'post', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${token}`, - }, - body: JSON.stringify({id: taskId, done: true}), - }) - .then(r => r.json()) - .then(r => { - console.debug('Task marked as done from notification', r) - }) - .catch(e => { - console.debug('Error marking task as done from notification', e) - }) - }) - }) - break case 'show-task': clients.openWindow(`/tasks/${taskId}`) break @@ -116,3 +52,4 @@ workbox.core.clientsClaim() // The precaching code provided by Workbox. self.__precacheManifest = [].concat(self.__precacheManifest || []) workbox.precaching.precacheAndRoute(self.__precacheManifest, {}) +