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