chore: 規範化 client event listener 寫法

This commit is contained in:
CrescentLeaf
2025-09-25 17:14:37 +08:00
parent 0b1a4a53a5
commit 151dc31f2c
2 changed files with 20 additions and 14 deletions

View File

@@ -6,13 +6,11 @@ import data from "../Data.ts"
import { checkApiSuccessOrSncakbar } from "../ui/snackbar.ts" import { checkApiSuccessOrSncakbar } from "../ui/snackbar.ts"
import randomUUID from "../randomUUID.ts" import randomUUID from "../randomUUID.ts"
type UnknownObject = { [key: string]: unknown }
class Client { class Client {
static sessionId = randomUUID() static sessionId = randomUUID()
static myUserProfile?: User static myUserProfile?: User
static socket?: Socket static socket?: Socket
static events: { [key: string]: (data: UnknownObject) => UnknownObject | void } = {} static events: { [key: string]: ((data: unknown) => void)[] } = {}
static connected = false static connected = false
static connect() { static connect() {
if (data.device_id == null) if (data.device_id == null)
@@ -37,17 +35,16 @@ class Client {
this.socket!.on("disconnect", () => { this.socket!.on("disconnect", () => {
this.connected = false this.connected = false
}) })
this.socket!.on("The_White_Silk", (name: string, data: UnknownObject, callback: (ret: UnknownObject) => void) => { this.socket!.on("The_White_Silk", (name: string, data: unknown, callback: (ret: unknown) => void) => {
try { try {
if (name == null || data == null) return if (name == null || data == null) return
const re = this.events[name]?.(data) this.events[name]?.forEach((v) => v(data))
re && callback(re)
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
}) })
} }
static invoke(method: CallMethod, args: UnknownObject = {}, timeout: number = 5000): Promise<ApiCallbackMessage> { static invoke(method: CallMethod, args: unknown = {}, timeout: number = 5000): Promise<ApiCallbackMessage> {
if (this.socket == null || (!this.connected && !CallableMethodBeforeAuth.includes(method))) { if (this.socket == null || (!this.connected && !CallableMethodBeforeAuth.includes(method))) {
return new Promise((reslove) => { return new Promise((reslove) => {
setTimeout(async () => reslove(await this.invoke(method, args, timeout)), 500) setTimeout(async () => reslove(await this.invoke(method, args, timeout)), 500)
@@ -79,11 +76,18 @@ class Client {
token: data.access_token token: data.access_token
})).data as unknown as User })).data as unknown as User
} }
static on(eventName: ClientEvent, func: (data: UnknownObject) => UnknownObject | void) { static on(eventName: ClientEvent, func: (data: unknown) => void) {
this.events[eventName] = func if (this.events[eventName] == null)
this.events[eventName] = []
if (this.events[eventName].indexOf(func) == -1)
this.events[eventName].push(func)
} }
static off(eventName: ClientEvent) { static off(eventName: ClientEvent, func: (data: unknown) => void) {
delete this.events[eventName] if (this.events[eventName] == null)
this.events[eventName] = []
const index = this.events[eventName].indexOf(func)
if (index != -1)
this.events[eventName].splice(index, 1)
} }
} }

View File

@@ -100,7 +100,7 @@ export default function ChatFragment({ target, showReturnButton, onReturnButtonC
chat: string chat: string
msg: Message msg: Message
} }
Client.on('Client.onMessage', (data: unknown) => { function callback(data: unknown) {
const { chat, msg } = (data as OnMessageData) const { chat, msg } = (data as OnMessageData)
if (target == chat) { if (target == chat) {
setMessagesList(messagesList.concat([msg])) setMessagesList(messagesList.concat([msg]))
@@ -110,9 +110,11 @@ export default function ChatFragment({ target, showReturnButton, onReturnButtonC
behavior: "smooth", behavior: "smooth",
}), 100) }), 100)
} }
}) }
Client.on('Client.onMessage', callback)
return () => { return () => {
Client.off('Client.onMessage') Client.off('Client.onMessage', callback)
} }
}) })