优化
Some checks failed
test / Run tests (push) Failing after 15s
build / Build (push) Failing after 23s
Some checks failed
test / Run tests (push) Failing after 15s
build / Build (push) Failing after 23s
This commit is contained in:
@ -19,7 +19,7 @@ export function connect(msg: Message, action: string, data?: any): Promise<Messa
|
||||
|
||||
export class Client {
|
||||
constructor(
|
||||
private msg: ExtensionMessageSend,
|
||||
private msg: MessageSend,
|
||||
private prefix: string
|
||||
) {
|
||||
if (!this.prefix.endsWith("/")) {
|
||||
|
@ -1,6 +1,10 @@
|
||||
import { Message, MessageConnect, MessageSend } from "./server";
|
||||
|
||||
export class ExtensionMessageSend implements MessageSend {
|
||||
constructor(private serverEnv: string) {
|
||||
// 由于service_worker和offscren同时监听消息的话,都会同时收到,用serverEnv于区分不同的环墨
|
||||
}
|
||||
|
||||
connect(data: any): Promise<MessageConnect> {
|
||||
return new Promise((resolve) => {
|
||||
const con = chrome.runtime.connect();
|
||||
@ -12,9 +16,14 @@ export class ExtensionMessageSend implements MessageSend {
|
||||
// 发送消息 注意不进行回调的内存泄漏
|
||||
sendMessage(data: any): Promise<any> {
|
||||
return new Promise((resolve) => {
|
||||
chrome.runtime.sendMessage(data, (resp) => {
|
||||
resolve(resp);
|
||||
});
|
||||
chrome.runtime.sendMessage(
|
||||
Object.assign(data, {
|
||||
serverEnv: this.serverEnv,
|
||||
}),
|
||||
(resp) => {
|
||||
resolve(resp);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ export type MessageSender = {
|
||||
tabId: number;
|
||||
};
|
||||
|
||||
export type ApiFunction = (params: any, con: MessageConnect | null) => any;
|
||||
export type ApiFunction = (params: any, con: MessageConnect | null) => Promise<any> | void;
|
||||
|
||||
export class Server {
|
||||
private apiFunctionMap: Map<string, ApiFunction> = new Map();
|
||||
@ -32,10 +32,17 @@ export class Server {
|
||||
message: Message
|
||||
) {
|
||||
message.onConnect((msg: any, con: MessageConnect) => {
|
||||
if (msg.serverEnv !== this.env) {
|
||||
con.disconnect();
|
||||
return;
|
||||
}
|
||||
this.connectHandle(msg.action, msg.data, con);
|
||||
});
|
||||
|
||||
message.onMessage((msg, sendResponse) => {
|
||||
if (msg.serverEnv !== this.env) {
|
||||
return;
|
||||
}
|
||||
return this.messageHandle(msg.action, msg.data, sendResponse);
|
||||
});
|
||||
}
|
||||
@ -100,7 +107,7 @@ export class Group {
|
||||
}
|
||||
|
||||
// 转发消息
|
||||
export function forwardMessage(path: string, from: Server, to: ExtensionMessageSend) {
|
||||
export function forwardMessage(path: string, from: Server, to: MessageSend) {
|
||||
from.on(path, (params, fromCon) => {
|
||||
if (fromCon) {
|
||||
to.connect({ action: path, data: params }).then((toCon) => {
|
||||
|
Reference in New Issue
Block a user