feat(shell): uix: extend EventEmitter

This commit is contained in:
Il Harper 2024-08-23 12:37:16 +08:00
parent d645e264ff
commit 183bf7f911
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC

View File

@ -1,3 +1,5 @@
import { EventEmitter } from 'node:events'
const uinRegex = /\d+/ const uinRegex = /\d+/
const isUin = (uin: unknown) => const isUin = (uin: unknown) =>
@ -7,33 +9,33 @@ const isUin = (uin: unknown) =>
const isUid = (uid: unknown) => const isUid = (uid: unknown) =>
typeof uid === 'string' && uid.length === 24 && uid.startsWith('u_') typeof uid === 'string' && uid.length === 24 && uid.startsWith('u_')
const map: Record<string, string> = {} export class Uix extends EventEmitter {
map: Record<string, string> = {}
const add = (uid: string, uin: string) => { isUin = isUin
if (!uid || !uin) return isUid = isUid
map[uid] = uin
map[uin] = uid 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) => { export const uix = new Uix()
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,
}