This commit is contained in:
2024-11-15 16:04:07 +08:00
parent e9cdb48d30
commit 6693de3f35
9 changed files with 222 additions and 16 deletions

View File

@ -7,7 +7,7 @@ export class ExtServer implements IServer {
constructor() {
this.EE = new EventEmitter();
chrome.runtime.onConnect.addListener((port) => {
this.EE.emit("connect", port.name, new ExtConnect(port));
this.EE.emit("connect", new ExtConnect(port));
});
}
@ -16,7 +16,7 @@ export class ExtServer implements IServer {
}
}
export function connect() {
export function extConnect() {
return new ExtConnect(chrome.runtime.connect());
}

View File

@ -1,7 +1,7 @@
// @vitest-environment jsdom
import { describe, expect, it, vi } from "vitest";
import { Server, Connect } from ".";
import { connect, WindowServer } from "./window";
import { windowConnect, WindowServer } from "./window";
describe("server", () => {
it("hello", async () => {
@ -13,7 +13,7 @@ describe("server", () => {
myFunc(message);
});
});
const client = connect(window, window);
const client = windowConnect(window, window);
client.postMessage("hello");
await new Promise((resolve) => setTimeout(resolve, 10));
expect(myFunc).toHaveBeenCalledTimes(2);
@ -33,7 +33,7 @@ describe("connect", async () => {
wrapCon.emit("world", "world");
});
});
const client = new Connect(connect(window, window));
const client = new Connect(windowConnect(window, window));
client.on("world", (message) => {
myFunc(message);
});
@ -53,7 +53,7 @@ describe("connect", async () => {
response("pong");
});
});
const client = new Connect(connect(window, window));
const client = new Connect(windowConnect(window, window));
client.emit("ping", "ping", (message: string) => {
myFunc(message);
});

View File

@ -9,7 +9,7 @@ export class WindowServer implements IServer {
this.EE = new EventEmitter();
win.addEventListener("message", (event) => {
if (event.data.type === "connect") {
this.EE.emit("connection", new WindowConnect(event.data.connectId, event.target as Window, win));
this.EE.emit("connection", new WindowConnect(event.data.connectId, win, event.source as Window));
}
});
}
@ -19,7 +19,7 @@ export class WindowServer implements IServer {
}
}
export function connect(source: Window, target: Window) {
export function windowConnect(source: Window, target: Window) {
const connectId = uuidv4();
target.postMessage({ type: "connect", connectId }, "*");
const con = new WindowConnect(connectId, source, target);