2024-05-24 23:28:56 +00:00
|
|
|
import * as plugins from './plugins.js';
|
2024-05-14 00:18:42 +00:00
|
|
|
import * as interfaces from '../dist_ts_interfaces/index.js';
|
2024-05-24 23:28:56 +00:00
|
|
|
import { logger } from "./logging.js";
|
|
|
|
import { NotificationManager } from './classes.notificationmanager.js';
|
|
|
|
import { ActionManager } from './classes.actionmanager.js';
|
|
|
|
import { GlobalSW } from './classes.globalsw.js'
|
2024-05-11 10:51:20 +00:00
|
|
|
|
2024-05-24 23:28:56 +00:00
|
|
|
export class ServiceworkerClient {
|
2024-05-11 10:51:20 +00:00
|
|
|
// STATIC
|
2024-05-24 23:28:56 +00:00
|
|
|
public static async createServiceWorker(): Promise<ServiceworkerClient> {
|
2024-05-11 10:51:20 +00:00
|
|
|
if ('serviceWorker' in navigator) {
|
|
|
|
try {
|
|
|
|
logger.log('info', 'trying to register serviceworker');
|
|
|
|
// this is some magic for Parcel to not pick up the serviceworker
|
|
|
|
const serviceworkerInNavigator: ServiceWorkerContainer = navigator.serviceWorker;
|
2024-05-25 01:04:25 +00:00
|
|
|
const swRegistration: ServiceWorkerRegistration = await serviceworkerInNavigator.register('/serviceworker.bundle.js', {
|
2024-05-11 10:51:20 +00:00
|
|
|
scope: '/',
|
|
|
|
updateViaCache: 'none'
|
|
|
|
});
|
|
|
|
plugins.smartdelay.delayFor(2000).then(async () => {
|
|
|
|
swRegistration.onupdatefound = () => {
|
|
|
|
logger.log('info', 'update found for service worker!');
|
|
|
|
logger.log('warn', 'trying to find convenient time to update');
|
|
|
|
};
|
|
|
|
while(true) {
|
|
|
|
await plugins.smartdelay.delayFor(60000);
|
|
|
|
swRegistration.update();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
logger.log('ok', 'serviceworker registered');
|
|
|
|
await navigator.serviceWorker.ready;
|
|
|
|
logger.log('ok', 'serviceworker is ready!');
|
|
|
|
await this.waitForController();
|
2024-05-24 23:28:56 +00:00
|
|
|
const losslessServiceWorkerInstance = new ServiceworkerClient();
|
2024-05-11 10:51:20 +00:00
|
|
|
return losslessServiceWorkerInstance;
|
|
|
|
} catch (err) {
|
|
|
|
// sentry integration here
|
|
|
|
console.log(err);
|
|
|
|
console.log(err.stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static async waitForController() {
|
|
|
|
const done = new plugins.smartpromise.Deferred();
|
|
|
|
const checkReady = () => {
|
|
|
|
if(navigator.serviceWorker.controller) {
|
|
|
|
logger.log('ok', 'controller is ready');
|
|
|
|
done.resolve();
|
|
|
|
} else {
|
|
|
|
logger.log('warn', 'controller not ready');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
navigator.serviceWorker.oncontrollerchange = checkReady;
|
|
|
|
checkReady();
|
|
|
|
await done.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
// INSTANCE
|
|
|
|
public notificationManager: NotificationManager;
|
|
|
|
public actionManager: ActionManager;
|
|
|
|
public globalSw: GlobalSW;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.notificationManager = new NotificationManager();
|
|
|
|
this.actionManager = new ActionManager();
|
|
|
|
this.globalSw = new GlobalSW(this);
|
|
|
|
}
|
|
|
|
}
|