You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.5 KiB

4 years ago
  1. type EventNames<T extends string | symbol | { [K in string | symbol]: any[] }> = T extends string | symbol ? T : keyof T;
  2. type EventArgs<T extends string | symbol | { [K in string | symbol]: any[] }, K extends EventNames<T>> = T extends string | symbol ? any[] : K extends keyof T ? T[K] : never;
  3. /**
  4. * Minimal `EventEmitter` interface that is molded against the Node.js
  5. * `EventEmitter` interface.
  6. */
  7. declare class EventEmitter<EventTypes extends string | symbol | { [K in keyof EventTypes]: any[] } = string | symbol> {
  8. static prefixed: string | boolean;
  9. /**
  10. * Return an array listing the events for which the emitter has registered
  11. * listeners.
  12. */
  13. eventNames(): Array<EventNames<EventTypes>>;
  14. /**
  15. * Return the listeners registered for a given event.
  16. */
  17. listeners<T extends EventNames<EventTypes>>(event: T): Array<EventEmitter.ListenerFn<EventArgs<EventTypes, T>>>;
  18. /**
  19. * Return the number of listeners listening to a given event.
  20. */
  21. listenerCount(event: EventNames<EventTypes>): number;
  22. /**
  23. * Calls each of the listeners registered for a given event.
  24. */
  25. emit<T extends EventNames<EventTypes>>(event: T, ...args: EventArgs<EventTypes, T>): boolean;
  26. /**
  27. * Add a listener for a given event.
  28. */
  29. on<T extends EventNames<EventTypes>>(event: T, fn: EventEmitter.ListenerFn<EventArgs<EventTypes, T>>, context?: any): this;
  30. addListener<T extends EventNames<EventTypes>>(event: T, fn: EventEmitter.ListenerFn<EventArgs<EventTypes, T>>, context?: any): this;
  31. /**
  32. * Add a one-time listener for a given event.
  33. */
  34. once<T extends EventNames<EventTypes>>(event: T, fn: EventEmitter.ListenerFn<EventArgs<EventTypes, T>>, context?: any): this;
  35. /**
  36. * Remove the listeners of a given event.
  37. */
  38. removeListener<T extends EventNames<EventTypes>>(event: T, fn?: EventEmitter.ListenerFn<EventArgs<EventTypes, T>>, context?: any, once?: boolean): this;
  39. off<T extends EventNames<EventTypes>>(event: T, fn?: EventEmitter.ListenerFn<EventArgs<EventTypes, T>>, context?: any, once?: boolean): this;
  40. /**
  41. * Remove all listeners, or those of the specified event.
  42. */
  43. removeAllListeners(event?: EventNames<EventTypes>): this;
  44. }
  45. declare namespace EventEmitter {
  46. export interface ListenerFn<Args extends any[] = any[]> {
  47. (...args: Args): void;
  48. }
  49. export interface EventEmitterStatic {
  50. new<EventTypes extends string | symbol | { [K in keyof EventTypes]: any[] } = string | symbol>(): EventEmitter<EventTypes>;
  51. }
  52. export const EventEmitter: EventEmitterStatic;
  53. }
  54. export = EventEmitter;