Skip to content

Commit

Permalink
$mol_service fix init hook
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Oct 29, 2024
1 parent bb46b57 commit 121c90e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
14 changes: 8 additions & 6 deletions service/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ namespace $ {
protected static plugins = {} as Record<string, $mol_service>

protected static inited = false
static init() {}
static ready() {}
protected static init() {}

@ $mol_action
static send(data: {}) {}
Expand Down Expand Up @@ -48,14 +47,17 @@ namespace $ {
)
}

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

init() {}
before_install() {}
install() {}
activate() {}
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> }

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

notification_click(notification: Notification) { return null as null | undefined | Promise<unknown> }
}
}
19 changes: 13 additions & 6 deletions service/service.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ namespace $ {
await ready

this.registration = reg
this.ready()

for (const data of this.send_delayed) {
this.send(data)
Expand Down Expand Up @@ -87,10 +86,10 @@ namespace $ {
worker.addEventListener( 'message', this.message.bind(this))
worker.addEventListener( 'fetch', this.fetch_event.bind(this))
worker.addEventListener( 'notificationclick', this.notification_click.bind(this))
this.inited = true
for (let name in this.plugins) this.plugins[name].init()
}

// for (let name in this.plugins) this.plugins[name].init(worker)

return worker
}

Expand Down Expand Up @@ -146,18 +145,26 @@ namespace $ {
}

static install(event: ExtendableEvent) {
const promises = []
for (let name in this.plugins) {
this.plugins[name].install()
const result = this.plugins[name].install()
if (result) promises.push(result)
}

if (promises.length > 0) {
event.waitUntil(Promise.all(promises))
}
this.worker().skipWaiting()
}

static activate(event: ExtendableEvent) {
const promises = []
for (let name in this.plugins) {
this.plugins[name].activate()
const result = this.plugins[name].activate()
if (result) promises.push(result)
}

event.waitUntil( this.worker().clients.claim() )
event.waitUntil( Promise.all([ ...promises, this.worker().clients.claim() ]) )

this.$.$mol_log3_done({
place: `${this}.activate()`,
Expand Down

0 comments on commit 121c90e

Please sign in to comment.