import { EventArgs } from './eventArgs'; export type EventType = { readonly callback: EventCallback; readonly once: boolean; }; export type EventsType = { [k in T]?: EventType[]; }; export type CallbackArgs = T extends keyof EventArgs ? EventArgs[T] : any; export type EventCallback = (args: CallbackArgs) => void; export default class EventEmitter { private _events; /** * 监听一个事件 * @param evt 事件名称 * @param callback 回调函数 * @param once 是否只监听一次 */ on(evt: T, callback: EventCallback, once?: boolean): void; on(evt: T, callback: EventCallback, once?: boolean): void; /** * 监听一个事件一次 * @param evt 事件名称 * @param callback 回调函数 */ once(evt: T, callback: (args: EventArgs[T]) => void): void; once(evt: T, callback: EventCallback): void; /** * 触发一个事件 * @param evts * @param eventArgs */ emit(evts: T, eventArgs: CallbackArgs): void; emit(evts: T, eventArgs: CallbackArgs): void; /** * 取消事件监听 * @param evts 事件名称 * @param callback 回调函数 * * - evts 为空时,清除所有事件的监听器 * - evts 非空,callback 为空时,清除指定事件的所有监听器 * - evts 非空,callback 非空,进行对象比较,清除指定事件的指定监听器 */ off(evts: T, callback?: (args: EventArgs[T]) => void): void; off(evts: T, callback?: EventCallback): void; getEvents(): EventsType; destroy(): void; } export { EventEmitter, EventArgs };