Skip to content

Commit

Permalink
$mol_service moved to wires, better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Nov 2, 2024
1 parent 61fb6db commit 5b68802
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 159 deletions.
3 changes: 2 additions & 1 deletion notify/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ namespace $ {
static override message_data(data: {}) {
if ('uri' in data && 'message' in data) {
this.show(data as $mol_notify_info)
return true
}
return null
}

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

}

Expand Down
21 changes: 10 additions & 11 deletions notify/notify.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ namespace $ {
$.$mol_notify = $mol_notify_web

export class $mol_notify_service_web extends $mol_notify_service {
static override async show({ context: title, message: body, uri: data }: $mol_notify_info) {
static override show({ context: title, message: body, uri: data }: $mol_notify_info) {
const registration = this.$.$mol_service_worker_web.registration()
const tag = data
const existen = await registration.getNotifications({ tag })
const existen = registration.getNotifications({ tag })

for( const not of existen ) {

Expand All @@ -45,24 +45,23 @@ namespace $ {

// const vibrate = [ 100, 200, 300, 400, 500 ]

await registration.showNotification( title, { body, data, /*vibrate,*/ tag } )
registration.showNotification( title, { body, data, /*vibrate,*/ tag } )

}

static override async notification( notification: Notification ) {
const clients = this.$.$mol_service_worker_web.clients()

const matched = await clients.matchAll({ includeUncontrolled: true, type: 'window' })
static override notification( notification: Notification ) {
const matched = this.$.$mol_service_worker_web.clients_filter({ includeUncontrolled: true, type: 'window' })
const last = matched.at(-1)

if( last ) {
await last.focus()
await last.navigate( notification.data )
last.focus()
last.navigate( notification.data )

return
return null
}

await clients.openWindow( notification.data )
this.$.$mol_service_worker_web.window_open( notification.data )
return null
}
}

Expand Down
13 changes: 7 additions & 6 deletions offline/offline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ namespace $ {
return this.respond(request)
}

protected static fetch(request: Request): Promise<Response> {
throw new Error('implement')
protected static fetch(request: Request) {
return null as null | Response
}

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

const url = new URL(request.url)
Expand All @@ -41,7 +41,8 @@ namespace $ {

// fetch with fallback to cache if statuses not match
try {
const actual = await this.fetch(request)
const actual = this.fetch(request)
if (! actual) return null
if (actual.status < 400) return actual

throw new Error(
Expand All @@ -59,8 +60,8 @@ namespace $ {
request = new Request(request, { cache: 'force-cache' })
}

const cached = await this.fetch(request)

const cached = this.fetch(request)
if (! cached) return null
if (! fallback_header || cached.headers.get('$mol_offline_remote_status')) return cached

const clone = new Response(cached.body, cached)
Expand Down
6 changes: 5 additions & 1 deletion offline/offline.web.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
namespace $ {
export class $mol_offline_web extends $mol_offline {
protected static override fetch(request: Request) {
return $mol_wire_sync(this).fetch_async(request)
}

static fetch_async(request: Request) {
return fetch(request)
}

static override activate() {
return this.$.$mol_service_worker_web.clients().claim()
return this.$.$mol_service_worker_web.claim()
}
}

Expand Down
11 changes: 11 additions & 0 deletions service/message/event/event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace $ {
export class $mol_service_message_event extends $mol_object {
data() {
return null as null | Record<string, unknown>
}

result(result: {} | null) {}

error(error: Error) {}
}
}
25 changes: 25 additions & 0 deletions service/message/event/event.web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace $ {
export class $mol_service_message_event_web extends $mol_service_message_event {
event!: ExtendableMessageEvent

override data() {
const data = this.event.data as string | null | {
[k: string]: unknown
}
if ( ! data || typeof data !== 'object' ) return null
return data
}

@ $mol_action
override result(result: {} | null) {
this.event.ports[0].postMessage({ error: null, result })
}

@ $mol_action
override error(error: Error) {
this.event.ports[0].postMessage({ error: error.toString(), result: null })
}
}

$.$mol_service_message_event = $mol_service_message_event_web
}
10 changes: 5 additions & 5 deletions service/plugin/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
namespace $ {
export class $mol_service_plugin extends $mol_object {
static install() { return null as undefined | null | Promise<unknown> }
static activate() { return null as undefined | null | Promise<unknown> }
static message_data(data: {}) { return null as null | undefined | Promise<unknown> }
static install() { return null as unknown }
static activate() { return null as unknown }
static message_data(data: {}) { return null as unknown }

static service() { return this.$.$mol_service_worker }
}

export class $mol_service_plugin_cache extends $mol_service_plugin {
static blocked(request: Request) { return false }
static modify(request: Request, waitUntil: (promise: Promise<unknown>) => void) {
return null as null | Response | PromiseLike<Response>
return null as null | Response
}
}

export class $mol_service_plugin_notify extends $mol_service_plugin {
static notification(e: unknown) { return null as null | undefined | Promise<unknown> }
static notification(e: unknown) { return null as unknown }
}
}
4 changes: 3 additions & 1 deletion service/worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ namespace $ {
export class $mol_service_worker extends $mol_object {
static path() { return 'web.js' }

static send_timeout() { return 20000 }

@ $mol_action
static send(data: {}) {}
static send(data: {}) { return null as unknown }

@ $mol_mem
static prepare(next?: $mol_service_prepare_event) {
Expand Down
Loading

0 comments on commit 5b68802

Please sign in to comment.