Skip to content

Commit

Permalink
$mol_worker async
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Nov 2, 2024
1 parent 33b1fc6 commit 3ee7e45
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 141 deletions.
2 changes: 1 addition & 1 deletion fail/catch/catch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace $ {

const catched = new WeakMap< any , boolean >()

export function $mol_fail_catch( error: unknown ) {
export function $mol_fail_catch( error: unknown ): error is Error {

if( typeof error !== 'object' ) return false
if( $mol_promise_like( error ) ) $mol_fail_hidden( error )
Expand Down
5 changes: 5 additions & 0 deletions fetch/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ namespace $ {
super()
}

@ $mol_action
clone() {
return new this.$.$mol_fetch_response(new Response(this.native.body, this.native))
}

status() {
const types = [ 'unknown', 'inform', 'success', 'redirect', 'wrong', 'failed' ] as const
return types[ Math.floor( this.native.status / 100 ) ]
Expand Down
2 changes: 1 addition & 1 deletion notify/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace $ {
}

static show( info: $mol_notify_info ) {
this.$.$mol_service.send(info)
this.$.$mol_service_worker.send(info)
}

}
Expand Down
6 changes: 3 additions & 3 deletions notify/notify.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace $ {

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

Expand All @@ -50,7 +50,7 @@ namespace $ {
}

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

if( last ) {
Expand All @@ -60,7 +60,7 @@ namespace $ {
return null
}

this.$.$mol_service_web.window_open( notification.data )
this.$.$mol_service_worker_web.window_open( notification.data )
return null
}
}
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
$mol_offline
}
55 changes: 24 additions & 31 deletions offline/offline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,56 @@ namespace $ {
return this.blocked_urls.includes(normalized_url)
}

static override modify(request: Request) {

if( request.method !== 'GET' ) return null
if( !/^https?:/.test( request.url ) ) return null
if( /\?/.test( request.url ) ) return null
if( request.cache === 'no-store' ) return null

return this.respond(request)
static override activate() {
return this.$.$mol_service_worker.claim()
}

protected static fetch(request: Request) {
return null as null | Response
static override need_modify(request: Request) {
if( request.method !== 'GET' ) return false
if( !/^https?:/.test( request.url ) ) return false
if( /\?/.test( request.url ) ) return false
if( request.cache === 'no-store' ) return false

return true
}

protected static respond(request: Request) {
static override modify(request: Request) {
let fallback_header

const url = new URL(request.url)
const html = url.pathname.endsWith('.html')

const html = request.mode === 'navigate'
const cache = request.cache

if (cache === 'reload' || ( cache === 'no-cache' && ! html ) ) {
if (cache === 'reload') {
// F5 + Disable cache
request = new Request(request, { cache: 'no-cache' })
request = this.request_clone(request, { cache: 'no-cache' })
}

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

throw new Error(
`${actual.status}${actual.statusText ? ` ${actual.statusText}` : ''}`,
{ cause: actual }
)
const actual = this.$.$mol_fetch.response(request)
if (actual.code() < 400) return actual.native

fallback_header = actual.message()
} catch (err) {
fallback_header = `${(err as Error).cause instanceof Response ? '' : '500 '}${
(err as Error).message} $mol_offline fallback to cache`
if ( $mol_promise_like(err) ) $mol_fail_hidden(err)
fallback_header = (err as Error).message || 'Fetch error'
}
}

if (cache !== 'force-cache') {
request = new Request(request, { cache: 'force-cache' })
request = this.request_clone(request, { cache: 'force-cache' })
}

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

if (! fallback_header ) return cached.native

const clone = new Response(cached.body, cached)
clone.headers.set( '$mol_offline_remote_status', fallback_header ?? '')
const clone = cached.clone()
clone.headers().set( '$mol_offline_remote_status', `${fallback_header} $mol_offline fallback to cache`)

return clone
return clone.native
}
}

Expand Down
18 changes: 0 additions & 18 deletions offline/offline.web.ts

This file was deleted.

18 changes: 13 additions & 5 deletions service/channel/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,23 @@ namespace $ {
channel.port1.onmessage = event => {
clearTimeout(handler)
const data = event.data
const message = data?.error ?? (data?.result ? null : 'empty data')
if (message) return reject(new Error(message, { cause: event }))

resolve(event.data.result)
const result = data?.result
const error = data?.error

if (result) {
if (error) console.warn('Message result+error:', error)
resolve(result)
return
}

if (! error) return resolve(result ?? null)

reject(new Error(error, { cause: event }))
}

channel.port1.onmessageerror = event => {
clearTimeout(handler)
reject(new Error('Can\'t be deserialized: ' + event.data, { cause: event }))
reject(new Error('Message fatal error: ' + event.data, { cause: event }))
}
})
}
Expand Down
4 changes: 1 addition & 3 deletions service/message/event/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ namespace $ {
return null as null | Record<string, unknown>
}

result(result: {} | null) {}

error(error: Error) {}
result(result: {} | null, errors?: readonly Error[]) {}
}
}
9 changes: 3 additions & 6 deletions service/message/event/event.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ namespace $ {
}

@ $mol_action
override result(result: {} | null) {
this.event.ports[0].postMessage({ error: null, result })
override result(result: {} | null, errors?: readonly Error[]) {
const error = errors?.length ? errors[0].toString() || errors[0].message : null
this.event.ports[0].postMessage({ error, 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
Expand Down
26 changes: 21 additions & 5 deletions service/plugin/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
namespace $ {
$mol_service_worker.init()

export namespace $mol_service_plugin {
let _
}

export class $mol_service_plugin_base extends $mol_object {
static install() { return null as unknown }
static activate() { return null as unknown }
static data(data: {}) { return null as unknown }

static is<This extends typeof $mol_object>(
this: This,
some: { prototype: unknown }
): some is This {
return some.prototype instanceof this
}

static install() { }
static activate() { }
static data(data: {}) { return null as null | unknown }
}

export class $mol_service_plugin_cache extends $mol_service_plugin_base {
static blocked(request: Request) { return false }
static modify(request: Request) { return null as null | Response }
static need_modify(request: Request) { return false }
static modify(request: Request) { return new Response }
@ $mol_action
static request_clone(original: Request, options?: RequestInit) {
return new Request(original, options)
}
}

export class $mol_service_plugin_notify extends $mol_service_plugin_base {
static notification(e: unknown) { return null as unknown }
static notification(e: unknown) { }
}

}
6 changes: 5 additions & 1 deletion service/service.ts → service/worker/worker.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
namespace $ {
export class $mol_service extends $mol_object {
export class $mol_service_worker extends $mol_object {
static path() { return 'web.js' }

static send_timeout() { return 20000 }

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

static init() {}

@ $mol_mem
static prepare(next?: $mol_service_prepare_event) {
return next ? next : null
}

static claim() {}

@ $mol_mem
static prepare_choise() { return $mol_wire_sync(this).choise_promise()?.outcome ?? null }

Expand Down
Loading

0 comments on commit 3ee7e45

Please sign in to comment.