Skip to content

Commit

Permalink
$mol_service_plugin extracted, used static, fetch hook moved back to …
Browse files Browse the repository at this point in the history
…$mol_service
  • Loading branch information
zerkalica committed Oct 30, 2024
1 parent b168b72 commit 354468b
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 88 deletions.
26 changes: 0 additions & 26 deletions fetch/service/service.ts

This file was deleted.

27 changes: 0 additions & 27 deletions fetch/service/service.web.ts

This file was deleted.

2 changes: 1 addition & 1 deletion notify/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ namespace $ {

}

$mol_notify_service.attach()
$mol_service.attach($mol_notify_service)

}
6 changes: 3 additions & 3 deletions notify/service/service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
namespace $ {
export class $mol_notify_service extends $mol_service {
override message_data(data: {}) {
export class $mol_notify_service extends $mol_service_plugin {
static override message_data(data: {}) {
if ('uri' in data && 'message' in data) {
this.info(data as $mol_notify_info)
}
return null
}

async info(info: $mol_notify_info) {}
static async info(info: $mol_notify_info) {}

}

Expand Down
8 changes: 4 additions & 4 deletions notify/service/service.web.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
namespace $ {
export class $mol_notify_service_web extends $mol_notify_service {
override init() {
static override init() {
const worker = this.$.$mol_service_web.worker()
worker.addEventListener( 'notificationclick', this.notification_click_event.bind(this))
}

protected notification_click_event(event: NotificationEvent) {
protected static notification_click_event(event: NotificationEvent) {
event.waitUntil(this.notification_click(event.notification))
}

override async info({ context: title, message: body, uri: data }: $mol_notify_info) {
static override async info({ context: title, message: body, uri: data }: $mol_notify_info) {
const worker = this.$.$mol_service_web.worker()
const tag = data
const existen = await worker.registration.getNotifications({ tag })
Expand All @@ -28,7 +28,7 @@ namespace $ {

}

async notification_click( notification: Notification ) {
static async notification_click( notification: Notification ) {
const worker = this.$.$mol_service_web.worker()

const clients = await worker.clients.matchAll({ includeUncontrolled: true, type: 'window' })
Expand Down
2 changes: 1 addition & 1 deletion offline/install/install.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace $ {
$mol_offline.attach()
$mol_service.attach($mol_offline)
}
10 changes: 5 additions & 5 deletions offline/offline.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
namespace $ {

export class $mol_offline extends $mol_fetch_service {
blocked_urls = [
export class $mol_offline extends $mol_service_plugin {
static blocked_urls = [
'//cse.google.com/adsense/search/async-ads.js'
]

override blocked( request: Request ) {
static override blocked( request: Request ) {
const normalized_url = request.url.replace( /^https?:/, '' )

return this.blocked_urls.includes(normalized_url)
}

override modify(request: Request) {
static override modify(request: Request) {

if( request.method !== 'GET' ) return null
if( !/^https?:/.test( request.url ) ) return null
Expand All @@ -21,7 +21,7 @@ namespace $ {
return this.respond(request)
}

async respond(request: Request) {
protected static async respond(request: Request) {
let fallback_header

const index_html = /.+\/index\.html/.test(request.url)
Expand Down
13 changes: 13 additions & 0 deletions service/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace $ {
export class $mol_service_plugin extends $mol_object {
static init() {}
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> }
}
}
29 changes: 12 additions & 17 deletions service/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,23 @@ namespace $ {
@ $mol_action
static send(data: {}) {}

protected static plugins = new Set<$mol_service>()
protected static plugins = new Set<typeof $mol_service_plugin>()

protected static add(plugin: $mol_service) {
static attach(plugin: typeof $mol_service_plugin) {
this.plugins.add(plugin)
this.init()
}

static attach< This extends typeof $mol_service >(
this : This,
config?: Partial< InstanceType< This > >,
) {
const plugin = this.make(config ?? {})
protected static init() {}

this.add(plugin)

return plugin
static blocked_response() {
return new Response(
null,
{
status: 418,
statusText: 'Blocked'
},
)
}

init() {}
before_install() {}
install() { return null as undefined | null | Promise<unknown> }
activate() { return null as undefined | null | Promise<unknown> }
state_change() {}
message_data(data: {}) { return null as null | undefined | Promise<unknown> }
}
}
20 changes: 16 additions & 4 deletions service/service.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ namespace $ {

protected static inited = false

protected static override add(plugin: $mol_service) {
super.add(plugin)

protected static override init() {
if (this.inited) return

if ( this.in_worker() ) {
Expand All @@ -45,7 +43,6 @@ namespace $ {
this.inited = true
}


static async registration_init() {

try {
Expand Down Expand Up @@ -92,6 +89,7 @@ namespace $ {
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()
}
Expand Down Expand Up @@ -155,6 +153,20 @@ namespace $ {
})
}

static fetch_event(event: FetchEvent) {
const request = event.request

for (const plugin of this.plugins) {
if (plugin.blocked(request)) {
return event.respondWith(this.blocked_response())
}
}

for (const plugin of this.plugins) {
const response = plugin.modify(request)
if (response) return event.respondWith(response)
}
}
}

$.$mol_service = $mol_service_web
Expand Down

0 comments on commit 354468b

Please sign in to comment.