MCPcopy Index your code
hub / github.com/scriptscat/scriptcat / createWiredPair

Function createWiredPair

packages/message/window_message.test.ts:202–219  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

200describe("ServiceWorkerMessageSend ↔ ServiceWorkerClientMessage 双向通信", () => {
201 // 辅助函数: 将两端连接起来,模拟 postMessage 通道
202 function createWiredPair() {
203 const swSend = new ServiceWorkerMessageSend();
204 const clientMsg = new ServiceWorkerClientMessage();
205
206 // 模拟 offscreen client(SW 发送给 offscreen 时的 target)
207 const offscreenPostMessage = vi.fn((data: WindowMessageBody) => {
208 // SW → Offscreen: 投递到 clientMsg 的 messageHandle
209 clientMessageHandler?.({ data });
210 });
211
212 // client.postToServiceWorker → 投递到 SW 的 messageHandle
213 swPostMessageMock.mockImplementation((data: WindowMessageBody) => {
214 const source = { postMessage: offscreenPostMessage };
215 swMessageHandler?.({ data, source } as any);
216 });
217
218 return { swSend, clientMsg };
219 }
220
221 it("sendMessage: client→SW 请求并收到响应", async () => {
222 const { swSend, clientMsg } = createWiredPair();

Callers 1

Calls 1

fnMethod · 0.80

Tested by

no test coverage detected