feat(api): implement emitter

This commit is contained in:
Il Harper 2024-03-05 21:42:35 +08:00
parent 73bdd9d780
commit cd69bb4fb4
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC
2 changed files with 27 additions and 0 deletions

View File

@ -0,0 +1,24 @@
/* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
import { EventEmitter } from 'node:events'
type EmittedEvents = Record<string | symbol, (...args: unknown[]) => unknown>
export interface TypedEventEmitter<ES extends EmittedEvents> {
on<E extends keyof ES>(event: E, listener: ES[E]): this
once<E extends keyof ES>(event: E, listener: ES[E]): this
emit<E extends keyof ES>(event: E, ...args: Parameters<ES[E]>): boolean
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export class TypedEventEmitter<ES extends EmittedEvents> extends EventEmitter {}
export type ChronoEvents = {
buddyListChange: () => void
groupListUpdate: () => void
}
export class ChronoEventEmitter extends TypedEventEmitter<ChronoEvents> {
emitBuddyListChange = () => this.emit('buddyListChange')
emitGroupListUpdate = () => this.emit('groupListUpdate')
}

View File

@ -1,4 +1,5 @@
import type { Group, Profile, RedIpcData, RedIpcEvent } from '@chronocat/red'
import { ChronoEventEmitter } from './emitter'
export const requestMethodMap: Record<string, string> = {}
export const requestCallbackMap: Record<
@ -10,3 +11,5 @@ export const groupMap: Record<string, Group> = {}
export const roleMap: Record<string, Record<string, number>> = {}
export const friendMap: Record<string, Profile> = {}
export const richMediaDownloadMap: Record<string, (path: string) => void> = {}
export const chronoEventEmitter = new ChronoEventEmitter()