diff --git a/src/messaging/ExtensionMessage.ts b/src/messaging/ExtensionMessage.ts index 92e7538..d28af84 100644 --- a/src/messaging/ExtensionMessage.ts +++ b/src/messaging/ExtensionMessage.ts @@ -101,9 +101,6 @@ class ExtensionMessage { const portContext: PortContext = {id, name, port, portMessageHandler, ready: false} this.portIdToPort.set(id, portContext) - // 开始监听 - portMessageHandler.startListen() - // 监听断开连接 port.onDisconnect.addListener(() => { this.portIdToPort.delete(id) diff --git a/src/messaging/InjectMessage.ts b/src/messaging/InjectMessage.ts index a6cad9d..89cce1b 100644 --- a/src/messaging/InjectMessage.ts +++ b/src/messaging/InjectMessage.ts @@ -69,8 +69,7 @@ class InjectMessage { name: MESSAGE_TARGET_INJECT, }) this.portMessageHandler = new Layer1Protocol(this.messageHandler, this.port) - this.portMessageHandler!.startListen() - this.portMessageHandler!.sendMessage({ + this.portMessageHandler.sendMessage({ method: '_init', params: { type: 'inject', diff --git a/src/messaging/Layer1Protocol.ts b/src/messaging/Layer1Protocol.ts index a2433cf..03c8d55 100644 --- a/src/messaging/Layer1Protocol.ts +++ b/src/messaging/Layer1Protocol.ts @@ -20,17 +20,24 @@ type RespMsg = { class Layer1Protocol { private port: chrome.runtime.Port private timeout: number - private messageMap: Map void, timer: number }> + private requests: Map void, timer: number }> private handler: (value: L1Req) => Promise constructor(handler: (value: L1Req) => Promise, port: chrome.runtime.Port, timeout = 30000) { // 默认超时 30 秒 this.port = port; this.timeout = timeout; - this.messageMap = new Map(); + this.requests = new Map(); this.handler = handler + + this._startListen() } - startListen() { + // 生成唯一 ID(简单示例,可以使用更复杂的生成策略) + _generateUniqueId() { + return crypto.randomUUID() + } + + _startListen() { // 持久监听 port.onMessage this.port.onMessage.addListener((msg: ReqMsg) => { const { id, type, req, res } = msg; @@ -50,12 +57,12 @@ class Layer1Protocol { this.port.postMessage({ id, type: 'res', res: response }); }); } else if (type === 'res') { - if (this.messageMap.has(id)) { - const { resolve, timer } = this.messageMap.get(id)!; + if (this.requests.has(id)) { + const { resolve, timer } = this.requests.get(id)!; // 清除超时定时器 clearTimeout(timer); // 移除消息 ID - this.messageMap.delete(id); + this.requests.delete(id); // 通过 ID 找到对应的 Promise 并 resolve resolve(res!.data!); }else { @@ -75,22 +82,17 @@ class Layer1Protocol { // 设置一个超时定时器 const timer = setTimeout(() => { // 超时后执行 reject 并从 Map 中删除 - this.messageMap.delete(id); + this.requests.delete(id); reject(new Error(`Request timed out after ${this.timeout / 1000} seconds`)); }, this.timeout); // 将 resolve 和 timer 函数与消息 ID 绑定,存入 Map - this.messageMap.set(id, { resolve, timer }); + this.requests.set(id, { resolve, timer }); // 发送消息,并附带 ID this.port.postMessage({ id, type: 'req', req }); }); } - - // 生成唯一 ID(简单示例,可以使用更复杂的生成策略) - _generateUniqueId() { - return crypto.randomUUID() - } } export default Layer1Protocol \ No newline at end of file diff --git a/src/messaging/const.ts b/src/messaging/const.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/messaging/useMessageService.ts b/src/messaging/useMessageService.ts index b31fc43..cf92cad 100644 --- a/src/messaging/useMessageService.ts +++ b/src/messaging/useMessageService.ts @@ -77,7 +77,6 @@ const useMessageService = (methods?: { if (messageHandler && port) { const pmh = new Layer1Protocol(messageHandler, port) - pmh.startListen() //get tabId from url params let tabIdStr = window.location.search.split('tabId=')[1] let tabId = tabIdStr ? parseInt(tabIdStr) : undefined