fix(web): worker fixes

This commit is contained in:
alina 🌸 2024-03-04 06:32:54 +03:00
parent fea8d93dcb
commit 56b2fe70d3
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
2 changed files with 13 additions and 9 deletions

View file

@ -3,6 +3,9 @@ const callbacks = new Set<() => void>()
let registered = false
export function beforeExit(fn: () => void): () => void {
if (typeof window === 'undefined') {
return () => {}
}
if (!registered) {
registered = true

View file

@ -26,15 +26,6 @@ export class TelegramWorker<T extends WorkerCustomMethods> extends TelegramWorke
_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) {
const connections: MessagePort[] = []
@ -90,11 +81,21 @@ export class TelegramWorker<T extends WorkerCustomMethods> extends TelegramWorke
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
handler(message.data, respond)
})
port.start()
}
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')
}
}