forked from vesoapp/veso-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceworker.js
52 lines (43 loc) · 1.64 KB
/
serviceworker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* eslint-env serviceworker */
/* eslint-disable import/namespace,import/named */
import { skipWaiting, clientsClaim } from 'workbox-core';
import { precacheAndRoute } from 'workbox-precaching';
/* eslint-enable import/namespace,import/named */
skipWaiting();
clientsClaim();
function getApiClient(serverId) {
return Promise.resolve(window.connectionManager.getApiClient(serverId));
}
function executeAction(action, data, serverId) {
return getApiClient(serverId).then(function (apiClient) {
switch (action) {
case 'cancel-install':
return apiClient.cancelPackageInstallation(data.id);
case 'restart':
return apiClient.restartServer();
default:
clients.openWindow('/');
return Promise.resolve();
}
});
}
/* eslint-disable-next-line no-restricted-globals -- self is valid in a serviceworker environment */
self.addEventListener('notificationclick', function (event) {
const notification = event.notification;
notification.close();
const data = notification.data;
const serverId = data.serverId;
const action = event.action;
if (!action) {
clients.openWindow('/');
event.waitUntil(Promise.resolve());
return;
}
event.waitUntil(executeAction(action, data, serverId));
}, false);
// Do not precache files when running with webpack dev server so live reload works as expected
if (!__WEBPACK_SERVE__) { // eslint-disable-line no-undef
// this is needed by the webpack Workbox plugin
/* eslint-disable-next-line no-restricted-globals,no-undef */
precacheAndRoute(self.__WB_MANIFEST);
}