2020-02-25 18:33:38 -05:00
|
|
|
/* eslint-env serviceworker */
|
2020-11-09 19:20:55 +00:00
|
|
|
|
2020-11-19 16:27:44 -05:00
|
|
|
/* eslint-disable import/namespace,import/named */
|
2020-11-09 19:20:55 +00:00
|
|
|
import { skipWaiting, clientsClaim } from 'workbox-core';
|
2020-11-19 16:27:44 -05:00
|
|
|
import { precacheAndRoute } from 'workbox-precaching';
|
|
|
|
/* eslint-enable import/namespace,import/named */
|
2020-11-09 19:20:55 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-06-03 10:14:28 -04:00
|
|
|
// Do not precache files in development so live reload works as expected
|
|
|
|
/* eslint-disable-next-line no-undef -- NODE_ENV is replaced by webpack */
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
// this is needed by the webpack Workbox plugin
|
|
|
|
/* eslint-disable-next-line no-restricted-globals,no-undef */
|
|
|
|
precacheAndRoute(self.__WB_MANIFEST);
|
|
|
|
}
|