mirror of
https://github.com/chrononeko/chronocat.git
synced 2024-11-25 09:37:35 +00:00
feat(shell): uix: extend EventEmitter
This commit is contained in:
parent
d645e264ff
commit
183bf7f911
@ -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
|
|
||||||
}
|
|
||||||
|
|
||||||
const getUin = (uid: string) => {
|
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
|
if (!isUid(uid)) return undefined
|
||||||
const uin = map[uid]
|
const uin = this.map[uid]
|
||||||
if (!isUin(uin)) return undefined
|
if (!isUin(uin)) return undefined
|
||||||
return uin
|
return uin
|
||||||
}
|
}
|
||||||
|
|
||||||
const getUid = (uin: string) => {
|
getUid = (uin: string) => {
|
||||||
if (!isUin(uin)) return undefined
|
if (!isUin(uin)) return undefined
|
||||||
const uid = map[uin]
|
const uid = this.map[uin]
|
||||||
if (!isUid(uid)) return undefined
|
if (!isUid(uid)) return undefined
|
||||||
return uid
|
return uid
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const uix = {
|
export const uix = new Uix()
|
||||||
map,
|
|
||||||
add,
|
|
||||||
isUin,
|
|
||||||
isUid,
|
|
||||||
getUin,
|
|
||||||
getUid,
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user