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 }