diff --git a/notify/notify.ts b/notify/notify.ts index c1e13a7897..ab8284fac6 100644 --- a/notify/notify.ts +++ b/notify/notify.ts @@ -14,11 +14,11 @@ namespace $ { } static show( info: $mol_notify_info ) { - this.$.$mol_service.send(info) + this.$.$mol_service_host.send(info) } } - $mol_service.attach($mol_notify_service) + $mol_service.$mol_notify_service } diff --git a/notify/notify.web.ts b/notify/notify.web.ts index 872d734331..81a39b07b3 100644 --- a/notify/notify.web.ts +++ b/notify/notify.web.ts @@ -28,5 +28,4 @@ namespace $ { } $.$mol_notify = $mol_notify_web - } diff --git a/notify/service/service.ts b/notify/service/service.ts index 215e984042..330742fc79 100644 --- a/notify/service/service.ts +++ b/notify/service/service.ts @@ -1,4 +1,4 @@ -namespace $ { +namespace $.$mol_service { export class $mol_notify_service extends $mol_service_plugin { static override message_data(data: {}) { if ('uri' in data && 'message' in data) { diff --git a/notify/service/service.web.ts b/notify/service/service.web.ts index 12adb79610..b1e1afa79b 100644 --- a/notify/service/service.web.ts +++ b/notify/service/service.web.ts @@ -1,7 +1,7 @@ -namespace $ { +namespace $.$mol_service { export class $mol_notify_service_web extends $mol_notify_service { static override init() { - const worker = this.$.$mol_service_web.worker() + const worker = this.$.$mol_service_host_web.worker() worker.addEventListener( 'notificationclick', this.notification_click_event.bind(this)) } @@ -10,7 +10,7 @@ namespace $ { } static override async info({ context: title, message: body, uri: data }: $mol_notify_info) { - const worker = this.$.$mol_service_web.worker() + const worker = this.$.$mol_service_host_web.worker() const tag = data const existen = await worker.registration.getNotifications({ tag }) @@ -29,7 +29,7 @@ namespace $ { } static async notification_click( notification: Notification ) { - const worker = this.$.$mol_service_web.worker() + const worker = this.$.$mol_service_host_web.worker() const clients = await worker.clients.matchAll({ includeUncontrolled: true, type: 'window' }) const last = clients.at(-1) @@ -45,5 +45,5 @@ namespace $ { } } - $.$mol_notify_service = $mol_notify_service_web + $.$mol_service.$mol_notify_service = $mol_notify_service_web } diff --git a/offline/install/install.ts b/offline/install/install.ts index 9145751341..321c548734 100644 --- a/offline/install/install.ts +++ b/offline/install/install.ts @@ -1,4 +1,3 @@ namespace $ { - // Not needed, just refer to $mol_offline anywhere - // $mol_service.attach($mol_offline) + $.$mol_service.$mol_offline } diff --git a/offline/offline.ts b/offline/offline.ts index c398dcbcd1..e646ad56ea 100644 --- a/offline/offline.ts +++ b/offline/offline.ts @@ -1,4 +1,4 @@ -namespace $ { +namespace $.$mol_service { export class $mol_offline extends $mol_service_plugin { static blocked_urls = [ @@ -64,6 +64,4 @@ namespace $ { return clone } } - - $mol_service.attach($mol_offline) } diff --git a/service/host/host.ts b/service/host/host.ts new file mode 100644 index 0000000000..2a06aa7e0d --- /dev/null +++ b/service/host/host.ts @@ -0,0 +1,23 @@ +namespace $ { + export class $mol_service_host extends $mol_object { + static in_worker() { return typeof window === 'undefined' } + + static path() { return 'web.js' } + + @ $mol_action + static send(data: {}) {} + + static init() {} + + static blocked_response() { + return new Response( + null, + { + status: 418, + statusText: 'Blocked' + }, + ) + } + } + +} diff --git a/service/service.web.ts b/service/host/host.web.ts similarity index 79% rename from service/service.web.ts rename to service/host/host.web.ts index b23413f477..c83d1199ae 100644 --- a/service/service.web.ts +++ b/service/host/host.web.ts @@ -4,7 +4,7 @@ namespace $ { export type $mol_service_reg_active = ServiceWorkerRegistration & { active: ServiceWorker } export type $mol_service_reg_installing = ServiceWorkerRegistration & { installing: ServiceWorker } - export class $mol_service_web extends $mol_service { + export class $mol_service_host_web extends $mol_service_host { static is_supported() { if( location.protocol !== 'https:' && location.hostname !== 'localhost' ) { console.warn( 'HTTPS or localhost is required for service workers.' ) @@ -32,15 +32,13 @@ namespace $ { static override init() { if (this.inited) return + this.inited = true if ( this.in_worker() ) { - this.worker() - return + this.worker_init() + } else if ( this.is_supported() ) { + this.registration_init() } - - if ( ! this.is_supported() ) return - this.registration_init() - this.inited = true } static async registration_init() { @@ -76,25 +74,30 @@ namespace $ { worker.addEventListener( 'statechange', this.state_change.bind(this, worker)) } + static plugins = [] as (typeof $mol_service.$mol_service_plugin)[] + static state_change(worker: ServiceWorker) { for (const plugin of this.plugins) { plugin.state_change() } } - static worker() { - const worker = self as unknown as ServiceWorkerGlobalScope - if (! this.inited) { - worker.addEventListener( 'beforeinstallprompt' , this.before_install.bind(this) ) - worker.addEventListener( 'install' , this.install.bind(this)) - worker.addEventListener( 'activate' , this.activate.bind(this)) - worker.addEventListener( 'message', this.message.bind(this)) - worker.addEventListener('fetch', this.fetch_event.bind(this)) - this.inited = true - for (const plugin of this.plugins) plugin.init() - } + static async worker_init() { + await Promise.resolve() + const worker = this.worker() + worker.addEventListener( 'beforeinstallprompt' , this.before_install.bind(this) ) + worker.addEventListener( 'install' , this.install.bind(this)) + worker.addEventListener( 'activate' , this.activate.bind(this)) + worker.addEventListener( 'message', this.message.bind(this)) + worker.addEventListener('fetch', this.fetch_event.bind(this)) + + this.plugins = Object.values(this.$.$mol_service).filter(plug => plug !== $mol_service.$mol_service_plugin) - return worker + for (const plugin of this.plugins) plugin.init() + } + + static worker() { + return self as unknown as ServiceWorkerGlobalScope } protected static send_delayed = [] as {}[] @@ -169,8 +172,8 @@ namespace $ { } } - $.$mol_service = $mol_service_web + $.$mol_service_host = $mol_service_host_web - $mol_service_web.init() + $mol_service_host_web.init() } diff --git a/service/plugin.ts b/service/plugin.ts deleted file mode 100644 index 1d48b99bfe..0000000000 --- a/service/plugin.ts +++ /dev/null @@ -1,13 +0,0 @@ -namespace $ { - export class $mol_service_plugin extends $mol_object { - static init() {} - static before_install() {} - static install() { return null as undefined | null | Promise } - static activate() { return null as undefined | null | Promise } - static state_change() {} - static message_data(data: {}) { return null as null | undefined | Promise } - - static blocked(res: Request) { return false } - static modify(res: Request) { return null as null | Response | PromiseLike } - } -} diff --git a/service/service.ts b/service/service.ts index fd9f421f97..576d726d37 100644 --- a/service/service.ts +++ b/service/service.ts @@ -1,34 +1,13 @@ -namespace $ { - - export class $mol_service extends $mol_object { - static in_worker() { return typeof window === 'undefined' } - - static path() { return 'web.js' } - - @ $mol_action - static send(data: {}) {} - - protected static plugins = new Set() - - static attach(plugin: typeof $mol_service_plugin) { - this.plugins.add(plugin) - } - - static detach(plugin: typeof $mol_service_plugin) { - this.plugins.delete(plugin) - } - +namespace $.$mol_service { + export class $mol_service_plugin extends $mol_object { static init() {} - - static blocked_response() { - return new Response( - null, - { - status: 418, - statusText: 'Blocked' - }, - ) - } + static before_install() {} + static install() { return null as undefined | null | Promise } + static activate() { return null as undefined | null | Promise } + static state_change() {} + static message_data(data: {}) { return null as null | undefined | Promise } + + static blocked(res: Request) { return false } + static modify(res: Request) { return null as null | Response | PromiseLike } } - }