mirror of
https://github.com/chrononeko/chronocat.git
synced 2024-11-22 07:07:53 +00:00
feat(api): implement emitter
This commit is contained in:
parent
73bdd9d780
commit
cd69bb4fb4
24
packages/engine-chronocat-api/src/emitter.ts
Normal file
24
packages/engine-chronocat-api/src/emitter.ts
Normal 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')
|
||||
}
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user