|
@@ -14,7 +14,7 @@ interface PromiseCapability<T> {
|
|
|
|
|
|
class MessageHandler {
|
|
|
name: string
|
|
|
- comObj: Window | Worker
|
|
|
+ comObj: typeof globalThis & { reset?: Function}
|
|
|
callbackId: number
|
|
|
postMessageTransfers: boolean
|
|
|
callbacksCapabilities: CallbackCapabilities
|
|
@@ -23,7 +23,7 @@ class MessageHandler {
|
|
|
nextAsync: Function | null
|
|
|
msgHandler: (event: MessageEvent) => void
|
|
|
|
|
|
- constructor(name: string, comObj: Window | Worker) {
|
|
|
+ constructor(name: string, comObj: typeof globalThis) {
|
|
|
this.name = name
|
|
|
this.comObj = comObj
|
|
|
this.callbackId = 1
|
|
@@ -33,10 +33,6 @@ class MessageHandler {
|
|
|
this.actionHandlerAsync = {}
|
|
|
this.nextAsync = null
|
|
|
|
|
|
- this.actionHandler.console_log = [(k: any) => console.log(k)]
|
|
|
- this.actionHandler.console_error = [(k: any) => console.error(k)]
|
|
|
- this.actionHandler.workerLoaded = [() => {}]
|
|
|
-
|
|
|
this.msgHandler = this.handleMessage.bind(this)
|
|
|
comObj.addEventListener("message", this.msgHandler)
|
|
|
}
|
|
@@ -190,35 +186,8 @@ class MessageHandler {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- postMessage(message: any) {
|
|
|
- if (this.postMessageTransfers) {
|
|
|
- const transfers = this.getTransfersArray(message)
|
|
|
- this.comObj.postMessage(message, transfers)
|
|
|
- } else {
|
|
|
- this.comObj.postMessage(message)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- getObjectTransfers(obj: any, transfers: any[]) {
|
|
|
- if (obj !== null && typeof obj === "object") {
|
|
|
- if (obj instanceof Uint8Array) {
|
|
|
- transfers.push(obj.buffer)
|
|
|
- } else if (obj instanceof ArrayBuffer) {
|
|
|
- transfers.push(obj)
|
|
|
- } else {
|
|
|
- Object.keys(obj).forEach(key => {
|
|
|
- if (obj.hasOwnProperty(key)) {
|
|
|
- this.getObjectTransfers(obj[key], transfers)
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- getTransfersArray(message: any): any[] | undefined {
|
|
|
- const transfers: any[] = []
|
|
|
- this.getObjectTransfers(message, transfers)
|
|
|
- return transfers.length === 0 ? undefined : transfers
|
|
|
+ postMessage(message: any, transfers?: Transferable[]) {
|
|
|
+ this.comObj.postMessage(message, transfers || [])
|
|
|
}
|
|
|
|
|
|
handleMessage(event: MessageEvent) {
|
|
@@ -281,4 +250,4 @@ class MessageHandler {
|
|
|
return { promise, resolve, reject }
|
|
|
}
|
|
|
}
|
|
|
-export default MessageHandler
|
|
|
+export default MessageHandler
|