From 7686a9b7d14dcc72292cb923075ae51fda3d1338 Mon Sep 17 00:00:00 2001 From: CrescentLeaf Date: Sun, 21 Sep 2025 02:16:01 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=BD=BF=20Client.invoke=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E7=AD=89=E5=BE=85=E5=90=8E=E8=AB=8B=E6=B1=82,=20?= =?UTF-8?q?=E8=A7=A3=E8=80=A6=20updateCachedProfile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/api/Client.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/client/api/Client.ts b/client/api/Client.ts index 7a0143c..e26a9c9 100644 --- a/client/api/Client.ts +++ b/client/api/Client.ts @@ -2,6 +2,7 @@ import { io, Socket } from 'socket.io-client' import { CallMethod, ClientEvent } from './ApiDeclare.ts' import ApiCallbackMessage from './ApiCallbackMessage.ts' import User from "./client_data/User.ts" +import data from "../Data.ts"; type UnknownObject = { [key: string]: unknown } @@ -26,7 +27,11 @@ class Client { }) } static invoke(method: CallMethod, args: UnknownObject = {}, timeout: number = 5000): Promise { - if (this.socket == null) throw new Error("客戶端未與伺服器端建立連接!") + if (this.socket == null) { + return new Promise((reslove) => { + setTimeout(async () => reslove(await this.invoke(method, args, timeout)), 500) + }) + } return new Promise((resolve, reject) => { this.socket!.timeout(timeout).emit("The_White_Silk", method, args, (err: string, res: ApiCallbackMessage) => { if (err) return reject(err) @@ -39,11 +44,14 @@ class Client { access_token: token }, timeout) if (re.code == 200) - this.myUserProfile = (await Client.invoke("User.getMyInfo", { - token: token - })).data as unknown as User + await this.updateCachedProfile() return re } + static async updateCachedProfile() { + this.myUserProfile = (await Client.invoke("User.getMyInfo", { + token: data.access_token + })).data as unknown as User + } static on(eventName: ClientEvent, func: (data: UnknownObject) => UnknownObject) { this.events[eventName] = func }