index.d.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * An events map is an interface that maps event names to their value, which
  3. * represents the type of the `on` listener.
  4. */
  5. export interface EventsMap {
  6. [event: string]: any;
  7. }
  8. /**
  9. * The default events map, used if no EventsMap is given. Using this EventsMap
  10. * is equivalent to accepting all event names, and any data.
  11. */
  12. export interface DefaultEventsMap {
  13. [event: string]: (...args: any[]) => void;
  14. }
  15. /**
  16. * Returns a union type containing all the keys of an event map.
  17. */
  18. export type EventNames<Map extends EventsMap> = keyof Map & (string | symbol);
  19. /** The tuple type representing the parameters of an event listener */
  20. export type EventParams<
  21. Map extends EventsMap,
  22. Ev extends EventNames<Map>
  23. > = Parameters<Map[Ev]>;
  24. /**
  25. * The event names that are either in ReservedEvents or in UserEvents
  26. */
  27. export type ReservedOrUserEventNames<
  28. ReservedEventsMap extends EventsMap,
  29. UserEvents extends EventsMap
  30. > = EventNames<ReservedEventsMap> | EventNames<UserEvents>;
  31. /**
  32. * Type of a listener of a user event or a reserved event. If `Ev` is in
  33. * `ReservedEvents`, the reserved event listener is returned.
  34. */
  35. export type ReservedOrUserListener<
  36. ReservedEvents extends EventsMap,
  37. UserEvents extends EventsMap,
  38. Ev extends ReservedOrUserEventNames<ReservedEvents, UserEvents>
  39. > = FallbackToUntypedListener<
  40. Ev extends EventNames<ReservedEvents>
  41. ? ReservedEvents[Ev]
  42. : Ev extends EventNames<UserEvents>
  43. ? UserEvents[Ev]
  44. : never
  45. >;
  46. /**
  47. * Returns an untyped listener type if `T` is `never`; otherwise, returns `T`.
  48. *
  49. * This is a hack to mitigate https://github.com/socketio/socket.io/issues/3833.
  50. * Needed because of https://github.com/microsoft/TypeScript/issues/41778
  51. */
  52. type FallbackToUntypedListener<T> = [T] extends [never]
  53. ? (...args: any[]) => void | Promise<void>
  54. : T;
  55. /**
  56. * Strictly typed version of an `EventEmitter`. A `TypedEventEmitter` takes type
  57. * parameters for mappings of event names to event data types, and strictly
  58. * types method calls to the `EventEmitter` according to these event maps.
  59. *
  60. * @typeParam ListenEvents - `EventsMap` of user-defined events that can be
  61. * listened to with `on` or `once`
  62. * @typeParam EmitEvents - `EventsMap` of user-defined events that can be
  63. * emitted with `emit`
  64. * @typeParam ReservedEvents - `EventsMap` of reserved events, that can be
  65. * emitted by socket.io with `emitReserved`, and can be listened to with
  66. * `listen`.
  67. */
  68. export class Emitter<
  69. ListenEvents extends EventsMap,
  70. EmitEvents extends EventsMap,
  71. ReservedEvents extends EventsMap = {}
  72. > {
  73. /**
  74. * Adds the `listener` function as an event listener for `ev`.
  75. *
  76. * @param ev Name of the event
  77. * @param listener Callback function
  78. */
  79. on<Ev extends ReservedOrUserEventNames<ReservedEvents, ListenEvents>>(
  80. ev: Ev,
  81. listener: ReservedOrUserListener<ReservedEvents, ListenEvents, Ev>
  82. ): this;
  83. /**
  84. * Adds a one-time `listener` function as an event listener for `ev`.
  85. *
  86. * @param ev Name of the event
  87. * @param listener Callback function
  88. */
  89. once<Ev extends ReservedOrUserEventNames<ReservedEvents, ListenEvents>>(
  90. ev: Ev,
  91. listener: ReservedOrUserListener<ReservedEvents, ListenEvents, Ev>
  92. ): this;
  93. /**
  94. * Removes the `listener` function as an event listener for `ev`.
  95. *
  96. * @param ev Name of the event
  97. * @param listener Callback function
  98. */
  99. off<Ev extends ReservedOrUserEventNames<ReservedEvents, ListenEvents>>(
  100. ev?: Ev,
  101. listener?: ReservedOrUserListener<ReservedEvents, ListenEvents, Ev>
  102. ): this;
  103. /**
  104. * Emits an event.
  105. *
  106. * @param ev Name of the event
  107. * @param args Values to send to listeners of this event
  108. */
  109. emit<Ev extends EventNames<EmitEvents>>(
  110. ev: Ev,
  111. ...args: EventParams<EmitEvents, Ev>
  112. ): this;
  113. /**
  114. * Emits a reserved event.
  115. *
  116. * This method is `protected`, so that only a class extending
  117. * `StrictEventEmitter` can emit its own reserved events.
  118. *
  119. * @param ev Reserved event name
  120. * @param args Arguments to emit along with the event
  121. */
  122. protected emitReserved<Ev extends EventNames<ReservedEvents>>(
  123. ev: Ev,
  124. ...args: EventParams<ReservedEvents, Ev>
  125. ): this;
  126. /**
  127. * Returns the listeners listening to an event.
  128. *
  129. * @param event Event name
  130. * @returns Array of listeners subscribed to `event`
  131. */
  132. listeners<Ev extends ReservedOrUserEventNames<ReservedEvents, ListenEvents>>(
  133. event: Ev
  134. ): ReservedOrUserListener<ReservedEvents, ListenEvents, Ev>[];
  135. /**
  136. * Returns true if there is a listener for this event.
  137. *
  138. * @param event Event name
  139. * @returns boolean
  140. */
  141. hasListeners<
  142. Ev extends ReservedOrUserEventNames<ReservedEvents, ListenEvents>
  143. >(event: Ev): boolean;
  144. /**
  145. * Removes the `listener` function as an event listener for `ev`.
  146. *
  147. * @param ev Name of the event
  148. * @param listener Callback function
  149. */
  150. removeListener<
  151. Ev extends ReservedOrUserEventNames<ReservedEvents, ListenEvents>
  152. >(
  153. ev?: Ev,
  154. listener?: ReservedOrUserListener<ReservedEvents, ListenEvents, Ev>
  155. ): this;
  156. /**
  157. * Removes all `listener` function as an event listener for `ev`.
  158. *
  159. * @param ev Name of the event
  160. */
  161. removeAllListeners<
  162. Ev extends ReservedOrUserEventNames<ReservedEvents, ListenEvents>
  163. >(ev?: Ev): this;
  164. }