From ca565e3c3ec1f3d77bb17ae066bd07f3bf494729 Mon Sep 17 00:00:00 2001 From: CrescentLeaf Date: Fri, 28 Nov 2025 23:36:47 +0800 Subject: [PATCH] =?UTF-8?q?refactor(cp):=20=E5=AE=A2=E6=88=B7=E7=AB=AF?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E6=94=AF=E6=8C=81=E6=95=B0=E6=8D=AE=E5=AF=B9?= =?UTF-8?q?=E5=BA=94=E4=BA=8B=E4=BB=B6=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client-protocol/ApiDeclare.ts | 10 +++++++ client-protocol/LingChairClient.ts | 44 +++++++++++++++++------------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/client-protocol/ApiDeclare.ts b/client-protocol/ApiDeclare.ts index e9868c2..df29a44 100644 --- a/client-protocol/ApiDeclare.ts +++ b/client-protocol/ApiDeclare.ts @@ -1 +1,11 @@ export * from 'lingchair-internal-shared' + +import { ClientEvent } from "lingchair-internal-shared" + +import Message from "./Message.ts" + +export type ClientEventData = + T extends "Client.onMessage" ? { message: Message } : + never + +export type ClientEventCallback = (data: ClientEventData) => void diff --git a/client-protocol/LingChairClient.ts b/client-protocol/LingChairClient.ts index 80790ab..61a21a8 100644 --- a/client-protocol/LingChairClient.ts +++ b/client-protocol/LingChairClient.ts @@ -1,13 +1,14 @@ // deno-lint-ignore-file no-explicit-any import { io, ManagerOptions, Socket, SocketOptions } from 'socket.io-client' import crypto from 'node:crypto' -import { CallMethod, ClientEvent } from './ApiDeclare.ts' +import { CallMethod, ClientEvent, ClientEventCallback } from './ApiDeclare.ts' import ApiCallbackMessage from './ApiCallbackMessage.ts' import User from "./User.ts" import UserMySelf from "./UserMySelf.ts" import CallbackError from "./CallbackError.ts" import Chat from "./Chat.ts" import { CallableMethodBeforeAuth } from "lingchair-internal-shared" +import Message from "./Message.ts"; export { User, @@ -46,15 +47,34 @@ export default class LingChairClient { session_id: crypto.randomUUID(), }, }) - this.client.on("The_White_Silk", (name: string, data: unknown, _callback: (ret: unknown) => void) => { + this.client.on("The_White_Silk", (name: ClientEvent, data: any, _callback: (ret: unknown) => void) => { try { if (name == null || data == null) return - this.events[name]?.forEach((v) => v(data)) + for (const v of (this.events[name] || [])) + v(({ + "Client.onMessage": { + message: new Message(this, data) + } + })[name]) } catch (e) { console.error(e) } }) } + events: { [K in ClientEvent]?: ClientEventCallback[] } = {} + on(eventName: K, func: ClientEventCallback) { + if (this.events[eventName] == null) + this.events[eventName] = [] + if (this.events[eventName].indexOf(func) == -1) + this.events[eventName].push(func) + } + off(eventName: K, func: ClientEventCallback) { + if (this.events[eventName] == null) + this.events[eventName] = [] + const index = this.events[eventName].indexOf(func) + if (index != -1) + this.events[eventName].splice(index, 1) + } connect() { this.client.connect() } @@ -86,20 +106,6 @@ export default class LingChairClient { }) }) } - events: { [key: string]: ((data: any) => void)[] } = {} - on(eventName: ClientEvent, func: (data: any) => void) { - if (this.events[eventName] == null) - this.events[eventName] = [] - if (this.events[eventName].indexOf(func) == -1) - this.events[eventName].push(func) - } - off(eventName: ClientEvent, func: (data: any) => void) { - if (this.events[eventName] == null) - this.events[eventName] = [] - const index = this.events[eventName].indexOf(func) - if (index != -1) - this.events[eventName].splice(index, 1) - } async auth(args: { refresh_token?: string, access_token?: string, @@ -213,7 +219,7 @@ export default class LingChairClient { ) form.append('file_name', fileName) chatId && form.append('chat_id', chatId) - + const re = await fetch(this.getBaseHttpUrl() + '/upload_file', { method: 'POST', headers: { @@ -227,7 +233,7 @@ export default class LingChairClient { let json try { json = JSON.parse(text) - // deno-lint-ignore no-empty + // deno-lint-ignore no-empty } catch (_) { } if (!re.ok) throw new CallbackError({ ...(json == null ? {