fix(web): worker fixes
This commit is contained in:
parent
fea8d93dcb
commit
56b2fe70d3
2 changed files with 13 additions and 9 deletions
|
@ -3,6 +3,9 @@ const callbacks = new Set<() => void>()
|
||||||
let registered = false
|
let registered = false
|
||||||
|
|
||||||
export function beforeExit(fn: () => void): () => void {
|
export function beforeExit(fn: () => void): () => void {
|
||||||
|
if (typeof window === 'undefined') {
|
||||||
|
return () => {}
|
||||||
|
}
|
||||||
if (!registered) {
|
if (!registered) {
|
||||||
registered = true
|
registered = true
|
||||||
|
|
||||||
|
|
|
@ -26,15 +26,6 @@ export class TelegramWorker<T extends WorkerCustomMethods> extends TelegramWorke
|
||||||
|
|
||||||
_registered = true
|
_registered = true
|
||||||
|
|
||||||
if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {
|
|
||||||
const respond: RespondFn = self.postMessage.bind(self)
|
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
||||||
self.addEventListener('message', (message) => handler(message.data, respond))
|
|
||||||
|
|
||||||
return respond
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof SharedWorkerGlobalScope !== 'undefined' && self instanceof SharedWorkerGlobalScope) {
|
if (typeof SharedWorkerGlobalScope !== 'undefined' && self instanceof SharedWorkerGlobalScope) {
|
||||||
const connections: MessagePort[] = []
|
const connections: MessagePort[] = []
|
||||||
|
|
||||||
|
@ -90,11 +81,21 @@ export class TelegramWorker<T extends WorkerCustomMethods> extends TelegramWorke
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||||
handler(message.data, respond)
|
handler(message.data, respond)
|
||||||
})
|
})
|
||||||
|
port.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
return broadcast
|
return broadcast
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {
|
||||||
|
const respond: RespondFn = self.postMessage.bind(self)
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||||
|
self.addEventListener('message', (message) => handler(message.data, respond))
|
||||||
|
|
||||||
|
return respond
|
||||||
|
}
|
||||||
|
|
||||||
throw new Error('TelegramWorker must be created from a worker')
|
throw new Error('TelegramWorker must be created from a worker')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue