王一之 1e8b5e6453
Some checks failed
test / Run tests (push) Failing after 15s
build / Build (push) Failing after 23s
优化
2025-02-08 17:59:18 +08:00

34 lines
992 B
TypeScript

import LoggerCore from "@App/app/logger/core";
import { Message, MessageConnect, MessageSend } from "./server";
import { ExtensionMessageSend } from "./extension_message";
export async function sendMessage(msg: MessageSend, action: string, data?: any): Promise<any> {
const res = await msg.sendMessage({ action, data });
LoggerCore.getInstance().logger().trace("sendMessage", { action, data, response: res });
if (res && res.code) {
console.error(res);
return Promise.reject(res.message);
} else {
return Promise.resolve(res.data);
}
}
export function connect(msg: Message, action: string, data?: any): Promise<MessageConnect> {
return msg.connect({ action, data });
}
export class Client {
constructor(
private msg: MessageSend,
private prefix: string
) {
if (!this.prefix.endsWith("/")) {
this.prefix += "/";
}
}
do(action: string, params?: any): Promise<any> {
return sendMessage(this.msg, this.prefix + action, params);
}
}