nullable persistent message channels

This commit is contained in:
NicolasCantu 2025-02-11 09:17:32 +01:00
parent 463f4d952c
commit d5c5224a9f

View File

@ -1,13 +1,13 @@
import Services from './service';
class Database {
export class Database {
private static instance: Database;
private db: IDBDatabase | null = null;
private dbName: string = '4nk';
private dbVersion: number = 1;
private serviceWorkerRegistration: ServiceWorkerRegistration | null = null;
private messageChannel: MessageChannel = new MessageChannel();
private messageChannelForGet: MessageChannel = new MessageChannel();
private messageChannel: MessageChannel | null = null;
private messageChannelForGet: MessageChannel | null = null;
private storeDefinitions = {
AnkLabels: {
name: 'labels',
@ -113,6 +113,10 @@ class Database {
}
private async initServiceWorker() {
if (this.messageChannel) {
console.log('Persistent update channel already initialized.');
return;
}
if ('serviceWorker' in navigator) {
try {
const registration = await navigator.serviceWorker.register('/src/service-workers/database.worker.js', { type: 'module' });
@ -122,6 +126,8 @@ class Database {
await this.checkForUpdates();
// Set up the message channels
this.messageChannel = new MessageChannel();
this.messageChannelForGet = new MessageChannel();
this.messageChannel.port1.onmessage = this.handleAddObjectResponse;
this.messageChannelForGet.port1.onmessage = this.handleGetObjectResponse;
registration.active?.postMessage(