index.d.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * Minimal `EventEmitter` interface that is molded against the Node.js
  3. * `EventEmitter` interface.
  4. */
  5. declare class EventEmitter<
  6. EventTypes extends EventEmitter.ValidEventTypes = string | symbol,
  7. Context extends any = any
  8. > {
  9. static prefixed: string | boolean;
  10. /**
  11. * Return an array listing the events for which the emitter has registered
  12. * listeners.
  13. */
  14. eventNames(): Array<EventEmitter.EventNames<EventTypes>>;
  15. /**
  16. * Return the listeners registered for a given event.
  17. */
  18. listeners<T extends EventEmitter.EventNames<EventTypes>>(
  19. event: T
  20. ): Array<EventEmitter.EventListener<EventTypes, T>>;
  21. /**
  22. * Return the number of listeners listening to a given event.
  23. */
  24. listenerCount(event: EventEmitter.EventNames<EventTypes>): number;
  25. /**
  26. * Calls each of the listeners registered for a given event.
  27. */
  28. emit<T extends EventEmitter.EventNames<EventTypes>>(
  29. event: T,
  30. ...args: EventEmitter.EventArgs<EventTypes, T>
  31. ): boolean;
  32. /**
  33. * Add a listener for a given event.
  34. */
  35. on<T extends EventEmitter.EventNames<EventTypes>>(
  36. event: T,
  37. fn: EventEmitter.EventListener<EventTypes, T>,
  38. context?: Context
  39. ): this;
  40. addListener<T extends EventEmitter.EventNames<EventTypes>>(
  41. event: T,
  42. fn: EventEmitter.EventListener<EventTypes, T>,
  43. context?: Context
  44. ): this;
  45. /**
  46. * Add a one-time listener for a given event.
  47. */
  48. once<T extends EventEmitter.EventNames<EventTypes>>(
  49. event: T,
  50. fn: EventEmitter.EventListener<EventTypes, T>,
  51. context?: Context
  52. ): this;
  53. /**
  54. * Remove the listeners of a given event.
  55. */
  56. removeListener<T extends EventEmitter.EventNames<EventTypes>>(
  57. event: T,
  58. fn?: EventEmitter.EventListener<EventTypes, T>,
  59. context?: Context,
  60. once?: boolean
  61. ): this;
  62. off<T extends EventEmitter.EventNames<EventTypes>>(
  63. event: T,
  64. fn?: EventEmitter.EventListener<EventTypes, T>,
  65. context?: Context,
  66. once?: boolean
  67. ): this;
  68. /**
  69. * Remove all listeners, or those of the specified event.
  70. */
  71. removeAllListeners(event?: EventEmitter.EventNames<EventTypes>): this;
  72. }
  73. declare namespace EventEmitter {
  74. export interface ListenerFn<Args extends any[] = any[]> {
  75. (...args: Args): void;
  76. }
  77. export interface EventEmitterStatic {
  78. new <
  79. EventTypes extends ValidEventTypes = string | symbol,
  80. Context = any
  81. >(): EventEmitter<EventTypes, Context>;
  82. }
  83. /**
  84. * `object` should be in either of the following forms:
  85. * ```
  86. * interface EventTypes {
  87. * 'event-with-parameters': any[]
  88. * 'event-with-example-handler': (...args: any[]) => void
  89. * }
  90. * ```
  91. */
  92. export type ValidEventTypes = string | symbol | object;
  93. export type EventNames<T extends ValidEventTypes> = T extends string | symbol
  94. ? T
  95. : keyof T;
  96. export type ArgumentMap<T extends object> = {
  97. [K in keyof T]: T[K] extends (...args: any[]) => void
  98. ? Parameters<T[K]>
  99. : T[K] extends any[]
  100. ? T[K]
  101. : any[];
  102. };
  103. export type EventListener<
  104. T extends ValidEventTypes,
  105. K extends EventNames<T>
  106. > = T extends string | symbol
  107. ? (...args: any[]) => void
  108. : (
  109. ...args: ArgumentMap<Exclude<T, string | symbol>>[Extract<K, keyof T>]
  110. ) => void;
  111. export type EventArgs<
  112. T extends ValidEventTypes,
  113. K extends EventNames<T>
  114. > = Parameters<EventListener<T, K>>;
  115. export const EventEmitter: EventEmitterStatic;
  116. }
  117. export = EventEmitter;