import { contextBridge, ipcRenderer } from "electron"; import { CH, } from "../shared/ipc"; let handler = null; let port = null; let streamId = ""; ipcRenderer.on(CH.portChannel, (event, payload) => { const next = event.ports[0]; if (!next) return; // 新流到达时关闭旧端口,避免旧消息排队后混进新流。 if (port) { port.onmessage = null; try { port.close(); } catch { // 已关闭。 } } port = next; streamId = payload?.streamId ?? ""; port.onmessage = (messageEvent) => { handler?.(messageEvent.data); }; port.start(); }); const api = { startStream: (options) => ipcRenderer.invoke(CH.start, options), cancelStream: (id) => ipcRenderer.invoke(CH.cancel, id), slow: (delayMs, timeoutMs) => ipcRenderer.invoke(CH.slow, delayMs, timeoutMs), readLarge: () => ipcRenderer.invoke(CH.large), readWhole: () => ipcRenderer.invoke(CH.whole), resetLarge: () => ipcRenderer.invoke(CH.reset), onStream: (cb) => { handler = cb; return () => { if (handler === cb) handler = null; }; }, ack: (seq) => { const ack = { type: "ack", seq }; port?.postMessage(ack); }, currentStream: () => streamId, }; contextBridge.exposeInMainWorld("api", api); //# sourceMappingURL=index.js.map