Skip to content

Commit

Permalink
$mol_service_plugin namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Oct 30, 2024
1 parent 7a0cf1e commit d53c252
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 79 deletions.
4 changes: 2 additions & 2 deletions notify/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
1 change: 0 additions & 1 deletion notify/notify.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ namespace $ {
}

$.$mol_notify = $mol_notify_web

}
2 changes: 1 addition & 1 deletion notify/service/service.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions notify/service/service.web.ts
Original file line number Diff line number Diff line change
@@ -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))
}

Expand All @@ -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 })

Expand All @@ -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)
Expand All @@ -45,5 +45,5 @@ namespace $ {
}
}

$.$mol_notify_service = $mol_notify_service_web
$.$mol_service.$mol_notify_service = $mol_notify_service_web
}
3 changes: 1 addition & 2 deletions offline/install/install.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
namespace $ {
// Not needed, just refer to $mol_offline anywhere
// $mol_service.attach($mol_offline)
$.$mol_service.$mol_offline
}
4 changes: 1 addition & 3 deletions offline/offline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace $ {
namespace $.$mol_service {

export class $mol_offline extends $mol_service_plugin {
static blocked_urls = [
Expand Down Expand Up @@ -64,6 +64,4 @@ namespace $ {
return clone
}
}

$mol_service.attach($mol_offline)
}
23 changes: 23 additions & 0 deletions service/host/host.ts
Original file line number Diff line number Diff line change
@@ -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'
},
)
}
}

}
45 changes: 24 additions & 21 deletions service/service.web.ts → service/host/host.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.' )
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {}[]
Expand Down Expand Up @@ -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()

}
13 changes: 0 additions & 13 deletions service/plugin.ts

This file was deleted.

41 changes: 10 additions & 31 deletions service/service.ts
Original file line number Diff line number Diff line change
@@ -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<typeof $mol_service_plugin>()

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<unknown> }
static activate() { return null as undefined | null | Promise<unknown> }
static state_change() {}
static message_data(data: {}) { return null as null | undefined | Promise<unknown> }

static blocked(res: Request) { return false }
static modify(res: Request) { return null as null | Response | PromiseLike<Response> }
}

}

0 comments on commit d53c252

Please sign in to comment.