1 | import { EventEmitter } from "events";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | export interface EventsMap {
|
7 | [event: string]: any;
|
8 | }
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | export interface DefaultEventsMap {
|
14 | [event: string]: (...args: any[]) => void;
|
15 | }
|
16 |
|
17 |
|
18 |
|
19 | export type EventNames<Map extends EventsMap> = keyof Map & (string | symbol);
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | export type EventNamesWithAck<Map extends EventsMap, K extends EventNames<Map> = EventNames<Map>> = IfAny<Last<Parameters<Map[K]>> | Map[K], K, K extends (Last<Parameters<Map[K]>> extends (...args: any[]) => any ? FirstNonErrorArg<Last<Parameters<Map[K]>>> extends void ? never : K : never) ? K : never>;
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | export type EventNamesWithoutAck<Map extends EventsMap, K extends EventNames<Map> = EventNames<Map>> = IfAny<Last<Parameters<Map[K]>> | Map[K], K, K extends (Parameters<Map[K]> extends never[] ? K : never) ? K : K extends (Last<Parameters<Map[K]>> extends (...args: any[]) => any ? never : K) ? K : never>;
|
32 | export type RemoveAcknowledgements<E extends EventsMap> = {
|
33 | [K in EventNamesWithoutAck<E>]: E[K];
|
34 | };
|
35 | export type EventNamesWithError<Map extends EventsMap, K extends EventNamesWithAck<Map> = EventNamesWithAck<Map>> = IfAny<Last<Parameters<Map[K]>> | Map[K], K, K extends (LooseParameters<Last<Parameters<Map[K]>>>[0] extends Error ? K : never) ? K : never>;
|
36 |
|
37 | export type EventParams<Map extends EventsMap, Ev extends EventNames<Map>> = Parameters<Map[Ev]>;
|
38 |
|
39 |
|
40 |
|
41 | export type ReservedOrUserEventNames<ReservedEventsMap extends EventsMap, UserEvents extends EventsMap> = EventNames<ReservedEventsMap> | EventNames<UserEvents>;
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | export type ReservedOrUserListener<ReservedEvents extends EventsMap, UserEvents extends EventsMap, Ev extends ReservedOrUserEventNames<ReservedEvents, UserEvents>> = FallbackToUntypedListener<Ev extends EventNames<ReservedEvents> ? ReservedEvents[Ev] : Ev extends EventNames<UserEvents> ? UserEvents[Ev] : never>;
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | type FallbackToUntypedListener<T> = [T] extends [never] ? (...args: any[]) => void | Promise<void> : T;
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | export interface TypedEventBroadcaster<EmitEvents extends EventsMap> {
|
59 | emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): boolean;
|
60 | }
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | export declare abstract class StrictEventEmitter<ListenEvents extends EventsMap, EmitEvents extends EventsMap, ReservedEvents extends EventsMap = {}> extends EventEmitter implements TypedEventBroadcaster<EmitEvents> {
|
75 | |
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | on<Ev extends ReservedOrUserEventNames<ReservedEvents, ListenEvents>>(ev: Ev, listener: ReservedOrUserListener<ReservedEvents, ListenEvents, Ev>): this;
|
82 | |
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | once<Ev extends ReservedOrUserEventNames<ReservedEvents, ListenEvents>>(ev: Ev, listener: ReservedOrUserListener<ReservedEvents, ListenEvents, Ev>): this;
|
89 | |
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): boolean;
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | protected emitReserved<Ev extends EventNames<ReservedEvents>>(ev: Ev, ...args: EventParams<ReservedEvents, Ev>): boolean;
|
106 | |
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | protected emitUntyped(ev: string, ...args: any[]): boolean;
|
117 | |
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | listeners<Ev extends ReservedOrUserEventNames<ReservedEvents, ListenEvents>>(event: Ev): ReservedOrUserListener<ReservedEvents, ListenEvents, Ev>[];
|
124 | }
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | type IsAny<T> = 0 extends 1 & T ? true : false;
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | type IfAny<T, TypeIfAny = true, TypeIfNotAny = false> = IsAny<T> extends true ? TypeIfAny : TypeIfNotAny;
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | export type Last<ValueType extends readonly unknown[]> = ValueType extends readonly [infer ElementType] ? ElementType : ValueType extends readonly [infer _, ...infer Tail] ? Last<Tail> : ValueType extends ReadonlyArray<infer ElementType> ? ElementType : never;
|
154 | export type FirstNonErrorTuple<T extends unknown[]> = T[0] extends Error ? T[1] : T[0];
|
155 | export type AllButLast<T extends any[]> = T extends [...infer H, infer L] ? H : any[];
|
156 |
|
157 |
|
158 |
|
159 | type LooseParameters<T> = T extends (...args: infer P) => any ? P : never;
|
160 | export type FirstNonErrorArg<T> = T extends (...args: infer Params) => any ? FirstNonErrorTuple<Params> : any;
|
161 | type PrependTimeoutError<T extends any[]> = {
|
162 | [K in keyof T]: T[K] extends (...args: infer Params) => infer Result ? Params[0] extends Error ? T[K] : (err: Error, ...args: Params) => Result : T[K];
|
163 | };
|
164 | export type MultiplyArray<T extends unknown[]> = {
|
165 | [K in keyof T]: T[K][];
|
166 | };
|
167 | type InferFirstAndPreserveLabel<T extends any[]> = T extends [any, ...infer R] ? T extends [...infer H, ...R] ? H : never : never;
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | type ExpectMultipleResponses<T extends any[]> = {
|
173 | [K in keyof T]: T[K] extends (...args: infer Params) => infer Result ? Params extends [Error] ? (err: Error) => Result : Params extends [Error, ...infer Rest] ? (err: Error, ...args: InferFirstAndPreserveLabel<MultiplyArray<Rest>>) => Result : Params extends [] ? () => Result : (...args: InferFirstAndPreserveLabel<MultiplyArray<Params>>) => Result : T[K];
|
174 | };
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | export type DecorateAcknowledgements<E> = {
|
195 | [K in keyof E]: E[K] extends (...args: infer Params) => infer Result ? (...args: PrependTimeoutError<Params>) => Result : E[K];
|
196 | };
|
197 | export type DecorateAcknowledgementsWithTimeoutAndMultipleResponses<E> = {
|
198 | [K in keyof E]: E[K] extends (...args: infer Params) => infer Result ? (...args: ExpectMultipleResponses<PrependTimeoutError<Params>>) => Result : E[K];
|
199 | };
|
200 | export type DecorateAcknowledgementsWithMultipleResponses<E> = {
|
201 | [K in keyof E]: E[K] extends (...args: infer Params) => infer Result ? (...args: ExpectMultipleResponses<Params>) => Result : E[K];
|
202 | };
|
203 | export {};
|