diff --git a/packages/shell/src/services/uix.ts b/packages/shell/src/services/uix.ts index 0a7d05a..5cf4c47 100644 --- a/packages/shell/src/services/uix.ts +++ b/packages/shell/src/services/uix.ts @@ -1,3 +1,5 @@ +import { EventEmitter } from 'node:events' + const uinRegex = /\d+/ const isUin = (uin: unknown) => @@ -7,33 +9,33 @@ const isUin = (uin: unknown) => const isUid = (uid: unknown) => typeof uid === 'string' && uid.length === 24 && uid.startsWith('u_') -const map: Record = {} +export class Uix extends EventEmitter { + map: Record = {} -const add = (uid: string, uin: string) => { - if (!uid || !uin) return - map[uid] = uin - map[uin] = uid + isUin = isUin + isUid = isUid + + add = (uid: string, uin: string) => { + if (!isUid(uid) || !isUin(uin)) return + this.map[uid] = uin + this.map[uin] = uid + this.emit(uin, uid) + this.emit(uid, uin) + } + + getUin = (uid: string) => { + if (!isUid(uid)) return undefined + const uin = this.map[uid] + if (!isUin(uin)) return undefined + return uin + } + + getUid = (uin: string) => { + if (!isUin(uin)) return undefined + const uid = this.map[uin] + if (!isUid(uid)) return undefined + return uid + } } -const getUin = (uid: string) => { - if (!isUid(uid)) return undefined - const uin = map[uid] - if (!isUin(uin)) return undefined - return uin -} - -const getUid = (uin: string) => { - if (!isUin(uin)) return undefined - const uid = map[uin] - if (!isUid(uid)) return undefined - return uid -} - -export const uix = { - map, - add, - isUin, - isUid, - getUin, - getUid, -} +export const uix = new Uix()