-
Notifications
You must be signed in to change notification settings - Fork 445
/
Copy pathindex.d.ts
389 lines (357 loc) · 16 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
type Callback0 = () => any;
type Callback1<T1> = (arg1: T1) => any;
type Callback2<T1, T2> = (arg1: T1, arg2: T2) => any;
type Callback3<T1, T2, T3> = (arg1: T1, arg2: T2, arg3: T3) => any;
type Callback4<T1, T2, T3, T4> = (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => any;
type Callback5<T1, T2, T3, T4, T5> = (arg1: T1, arg2: T2, arg3: T3,
arg4: T4, arg5: T5) => any;
type Callback6Rest<T1, T2, T3, T4, T5, T6> = (arg1: T1, arg2: T2, arg3: T3,
arg4: T4, arg5: T5, arg6: T6,
...rest: any[]) => any;
type ErrorHandler<T> = (error: Error, arg: T) => any
type ErrorHandler2<T1, T2> = (error: Error, arg1: T1, arg2: T2) => any
type ErrorHandler3<T1, T2, T3> = (error: Error, arg1: T1, arg2: T2, arg3: T3) => any
type ErrorHandler4<T1, T2, T3, T4> = (error: Error, arg1: T1, arg2: T2, arg3: T3,
arg4: T4) => any
type ErrorHandler5<T1, T2, T3, T4, T5> = (error: Error, arg1: T1, arg2: T2, arg3: T3,
arg4: T4, arg5: T5) => any
type ErrorHandler6Rest<T1, T2, T3, T4, T5, T6> = (error: Error, arg1: T1, arg2: T2, arg3: T3,
arg4: T4, arg5: T5, arg6: T6, ...rest: any[]) => any
export class EventProxy {
/**
* Create a new EventProxy
* Examples:
* ```js
* var ep = EventProxy.create();
* ep.assign('user', 'articles', function(user, articles) {
* // do something...
* });
* // or one line ways: Create EventProxy and Assign
* var ep = EventProxy.create('user', 'articles', function(user, articles) {
* // do something...
* });
* ```
* @return {EventProxy} EventProxy instance
*/
static create(): EventProxy
static create<T1>(ev1: string, callback: Callback1<T1>,
errorHandler?: ErrorHandler<T1>): EventProxy
static create<T1, T2>(ev1: string, ev2: string, callback: Callback2<T1, T2>,
errorHandler?: ErrorHandler<T1 | T2>): EventProxy
static create<T1, T2, T3>(ev1: string, ev2: string, ev3: string,
callback: Callback3<T1, T2, T3>, errorHandler?: ErrorHandler<T1 | T2 | T3>): EventProxy
static create<T1, T2, T3, T4>(ev1: string, ev2: string, ev3: string, ev4: string,
callback: Callback4<T1, T2, T3, T4>, errorHandler?: ErrorHandler<T1 | T2 | T3 | T4>): EventProxy
static create<T1, T2, T3, T4, T5>(ev1: string, ev2: string, ev3: string, ev4: string, ev5: string,
callback: Callback5<T1, T2, T3, T4, T5>, errorHandler?: ErrorHandler<T1 | T2 | T3 | T4 | T5>): EventProxy
static create<T1, T2, T3, T4, T5, T6>(ev1: string, ev2: string, ev3: string, ev4: string, ev5: string, ev6: string,
...rest: (string | ErrorHandler<any> | Callback6Rest<T1, T2, T3, T4, T5, T6>)[]): EventProxy
/**
* Bind an event, specified by a string name, `ev`, to a `callback` function.
* Passing __ALL_EVENT__ will bind the callback to all events fired.
* Examples:
* ```js
* var proxy = new EventProxy();
* proxy.addListener("template", function (event) {
* // TODO
* });
* ```
* @param {string} eventname Event name.
* @param {Function} callback Callback.
*/
addListener<T>(event: string, callback: Callback1<T>): this
addListener<T1, T2>(event: string, callback: Callback2<T1, T2>): this
addListener<T1, T2, T3>(event: string, callback: Callback3<T1, T2, T3>): this
addListener<T1, T2, T3, T4>(event: string, callback: Callback4<T1, T2, T3, T4>): this
addListener<T1, T2, T3, T4, T5>(event: string, callback: Callback5<T1, T2, T3, T4, T5>): this
addListener<T1, T2, T3, T4, T5, T6>(event: string, callback: Callback6Rest<T1, T2, T3, T4, T5, T6>): this
/**
* `addListener` alias, `bind`
*/
bind<T>(event: string, callback: Callback1<T>): this
bind<T1, T2>(event: string, callback: Callback2<T1, T2>): this
bind<T1, T2, T3>(event: string, callback: Callback3<T1, T2, T3>): this
bind<T1, T2, T3, T4>(event: string, callback: Callback4<T1, T2, T3, T4>): this
bind<T1, T2, T3, T4, T5>(event: string, callback: Callback5<T1, T2, T3, T4, T5>): this
bind<T1, T2, T3, T4, T5, T6>(event: string, callback: Callback6Rest<T1, T2, T3, T4, T5, T6>): this
/**
* `addListener` alias, `on`
*/
on<T>(event: string, callback: Callback1<T>): this
on<T1, T2>(event: string, callback: Callback2<T1, T2>): this
on<T1, T2, T3>(event: string, callback: Callback3<T1, T2, T3>): this
on<T1, T2, T3, T4>(event: string, callback: Callback4<T1, T2, T3, T4>): this
on<T1, T2, T3, T4, T5>(event: string, callback: Callback5<T1, T2, T3, T4, T5>): this
on<T1, T2, T3, T4, T5, T6>(event: string, callback: Callback6Rest<T1, T2, T3, T4, T5, T6>): this
/**
* `addListener` alias, `subscribe`
*/
subscribe<T>(event: string, callback: Callback1<T>): this
subscribe<T1, T2>(event: string, callback: Callback2<T1, T2>): this
subscribe<T1, T2, T3>(event: string, callback: Callback3<T1, T2, T3>): this
subscribe<T1, T2, T3, T4>(event: string, callback: Callback4<T1, T2, T3, T4>): this
subscribe<T1, T2, T3, T4, T5>(event: string, callback: Callback5<T1, T2, T3, T4, T5>): this
subscribe<T1, T2, T3, T4, T5, T6>(event: string, callback: Callback6Rest<T1, T2, T3, T4, T5, T6>): this
/**
* Bind an event, but put the callback into head of all callbacks.
* @param {String} eventname Event name.
* @param {Function} callback Callback.
*/
headbind<T>(event: string, callback: Callback1<T>): this
headbind<T1, T2>(event: string, callback: Callback2<T1, T2>): this
headbind<T1, T2, T3>(event: string, callback: Callback3<T1, T2, T3>): this
headbind<T1, T2, T3, T4>(event: string, callback: Callback4<T1, T2, T3, T4>): this
headbind<T1, T2, T3, T4, T5>(event: string, callback: Callback5<T1, T2, T3, T4, T5>): this
headbind<T1, T2, T3, T4, T5, T6>(event: string, callback: Callback6Rest<T1, T2, T3, T4, T5, T6>): this
/**
* Remove one or many callbacks.
*
* - If `callback` is null, removes all callbacks for the event.
* - If `eventname` is null, removes all bound callbacks for all events.
* @param {String} eventname Event name.
* @param {Function} callback Callback.
*/
removeEventListener(event?: string, callback?: Function): this
/**
* `removeListener` alias, unbind
*/
unbind(event?: string, callback?: Function): this
/**
* Remove all listeners. It equals unbind()
* Just add this API for as same as Event.Emitter.
* @param {String} event Event name.
*/
removeAllListeners(event: string)
/**
* Bind the ALL_EVENT event
*/
bindForAll(callback: Function)
/**
* Unbind the ALL_EVENT event
*/
unbindForAll(callback: Function)
/**
* Trigger an event, firing all bound callbacks. Callbacks are passed the
* same arguments as `trigger` is, apart from the event name.
* Listening for `"all"` passes the true event name as the first argument.
* @param {String} eventname Event name
* @param {Mix} data Pass in data
*/
trigger(event: string)
trigger<T1>(event: string, arg: T1)
trigger<T1, T2>(event: string, arg1: T1, arg2: T2)
trigger<T1, T2, T3>(event: string, arg1: T1, arg2: T2, arg3: T3)
trigger<T1, T2, T3, T4>(event: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4)
trigger<T1, T2, T3, T4, T5>(event: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5)
trigger<T1, T2, T3, T4, T5, T6>(event: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5,
arg6: T6, ...rest: any[])
/**
* `trigger` alias, `emit`
*/
emit(event: string)
emit<T1>(event: string, arg: T1)
emit<T1, T2>(event: string, arg1: T1, arg2: T2)
emit<T1, T2, T3>(event: string, arg1: T1, arg2: T2, arg3: T3)
emit<T1, T2, T3, T4>(event: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4)
emit<T1, T2, T3, T4, T5>(event: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5)
emit<T1, T2, T3, T4, T5, T6>(event: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5,
arg6: T6, ...rest: any[])
/**
* `trigger` alias, `fire`
*/
fire(event: string)
fire<T1>(event: string, arg: T1)
fire<T1, T2>(event: string, arg1: T1, arg2: T2)
fire<T1, T2, T3>(event: string, arg1: T1, arg2: T2, arg3: T3)
fire<T1, T2, T3, T4>(event: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4)
fire<T1, T2, T3, T4, T5>(event: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5)
fire<T1, T2, T3, T4, T5, T6>(event: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5,
arg6: T6, ...rest: any[])
/**
* Bind an event like the bind method, but will remove the listener after it was fired.
* @param {String} ev Event name
* @param {Function} callback Callback
*/
once<T>(event: string, callback: Callback1<T>): this
once<T1, T2>(event: string, callback: Callback2<T1, T2>): this
once<T1, T2, T3>(event: string, callback: Callback3<T1, T2, T3>): this
once<T1, T2, T3, T4>(event: string, callback: Callback4<T1, T2, T3, T4>): this
once<T1, T2, T3, T4, T5>(event: string, callback: Callback5<T1, T2, T3, T4, T5>): this
once<T1, T2, T3, T4, T5, T6>(event: string, callback: Callback6Rest<T1, T2, T3, T4, T5, T6>): this
/**
* emitLater
* make emit async
*/
emitLater<T>(event: string, callback: Callback1<T>): void
emitLater<T1, T2>(event: string, callback: Callback2<T1, T2>): void
emitLater<T1, T2, T3>(event: string, callback: Callback3<T1, T2, T3>): void
emitLater<T1, T2, T3, T4>(event: string, callback: Callback4<T1, T2, T3, T4>): void
emitLater<T1, T2, T3, T4, T5>(event: string, callback: Callback5<T1, T2, T3, T4, T5>): void
emitLater<T1, T2, T3, T4, T5, T6>(event: string, callback: Callback6Rest<T1, T2, T3, T4, T5, T6>): void
/**
* Bind an event, and trigger it immediately.
* @param {String} ev Event name.
* @param {Function} callback Callback.
* @param {Mix} data The data that will be passed to calback as arguments.
*/
immediate<T>(event: string, callback: Callback1<T>, data: T): this
/**
* `immediate` alias, `asap`
*/
asap<T>(event: string, callback: Callback1<T>, data: T): this
/**
* Assign some events, after all events were fired, the callback will be executed once.
*
* Examples:
* ```js
* proxy.all(ev1, ev2, callback);
* proxy.all([ev1, ev2], callback);
* proxy.all(ev1, [ev2, ev3], callback);
* ```
* @param {String} eventname1 First event name.
* @param {String} eventname2 Second event name.
* @param {Function} callback Callback, that will be called after predefined events were fired.
*/
all(event1: string | string[], event2: string | string[], callback: Function)
all(event: string[] | string, callback: Function)
/**
* `all` alias
*/
assign(event1: string | string[], event2: string | string[], callback: Function)
assign(event: string[] | string, callback: Function)
/**
* Assign the only one 'error' event handler.
* @param {Function(err)} callback
*/
fail<T>(callback: ErrorHandler<T>)
fail<T1, T2>(callback: ErrorHandler2<T1, T2>)
fail<T1, T2, T3>(callback: ErrorHandler3<T1, T2, T3>)
fail<T1, T2, T3, T4>(callback: ErrorHandler4<T1, T2, T3, T4>)
fail<T1, T2, T3, T4, T5>(callback: ErrorHandler5<T1, T2, T3, T4, T5>)
fail<T1, T2, T3, T4, T5, T6>(callback: ErrorHandler6Rest<T1, T2, T3, T4, T5, T6>)
/**
* A shortcut of ep#emit('error', err)
*/
throw(...args: any[])
/**
* Assign some events, after all events were fired, the callback will be executed first time.
* Then any event that predefined be fired again, the callback will executed with the newest data.
* Examples:
* ```js
* proxy.tail(ev1, ev2, callback);
* proxy.tail([ev1, ev2], callback);
* proxy.tail(ev1, [ev2, ev3], callback);
* ```
* @param {String} eventname1 First event name.
* @param {String} eventname2 Second event name.
* @param {Function} callback Callback, that will be called after predefined events were fired.
*/
tail(event1: string | string[], event2: string | string[], callback: Function)
tail(event: string[] | string, callback: Function)
/**
* `tail` alias, assignAll
*/
assignAll(event1: string | string[], event2: string | string[], callback: Function)
assignAll(event: string[] | string, callback: Function)
/**
* `tail` alias, assignAlways
*/
assignAlways(event1: string | string[], event2: string | string[], callback: Function)
assignAlways(event: string[] | string, callback: Function)
/**
* The callback will be executed after the event be fired N times.
* @param {String} eventname Event name.
* @param {Number} times N times.
* @param {Function} callback Callback, that will be called after event was fired N times.
*/
after<T>(event: string, times: number, callback: Callback1<T>): this
after<T1, T2>(event: string, times: number, callback: Callback2<T1, T2>): this
after<T1, T2, T3>(event: string, times: number, callback: Callback3<T1, T2, T3>): this
after<T1, T2, T3, T4>(event: string, times: number, callback: Callback4<T1, T2, T3, T4>): this
after<T1, T2, T3, T4, T5>(event: string, times: number, callback: Callback5<T1, T2, T3, T4, T5>): this
after<T1, T2, T3, T4, T5, T6>(event: string, times: number, callback: Callback6Rest<T1, T2, T3, T4, T5, T6>): this
/**
* The `after` method's helper. Use it will return ordered results.
* If you need manipulate result, you need callback
* Examples:
* ```js
* var ep = new EventProxy();
* ep.after('file', files.length, function (list) {
* // Ordered results
* });
* for (var i = 0; i < files.length; i++) {
* fs.readFile(files[i], 'utf-8', ep.group('file'));
* }
* ```
* @param {String} eventname Event name, shoule keep consistent with `after`.
* @param {Function} callback Callback function, should return the final result.
*/
group(event: string)
group<T1>(event: string, callback: Callback1<T1>): ErrorHandler<T1>
group<T1, T2>(event: string, callback: Callback2<T1, T2>): ErrorHandler2<T1, T2>
group<T1, T2, T3>(event: string, callback: Callback3<T1, T2, T3>): ErrorHandler3<T1, T2, T3>
group<T1, T2, T3, T4>(event: string, callback: Callback4<T1, T2, T3, T4>): ErrorHandler4<T1, T2, T3, T4>
group<T1, T2, T3, T4, T5>(event: string, callback: Callback5<T1, T2, T3, T4, T5>): ErrorHandler5<T1, T2, T3, T4, T5>
group<T1, T2, T3, T4, T5, T6>(event: string, callback: Callback6Rest<T1, T2, T3, T4, T5, T6>)
: ErrorHandler6Rest<T1, T2, T3, T4, T5, T6>
/**
* The callback will be executed after any registered event was fired. It only executed once.
* @param {String} eventname1 Event name.
* @param {String} eventname2 Event name.
* @param {Function} callback The callback will get a map that has data and eventname attributes.
*/
any<T1>(ev1: string, callback: Callback1<T1>)
any<T1, T2>(ev1: string, ev2: string, callback: Callback2<T1, T2>)
any<T1, T2, T3>(ev1: string, ev2: string, ev3: string, callback: Callback3<T1, T2, T3>)
any<T1, T2, T3, T4>(ev1: string, ev2: string, ev3: string, ev4: string, callback: Callback4<T1, T2, T3, T4>)
any<T1, T2, T3, T4, T5>(ev1: string, ev2: string, ev3: string, ev4: string, ev5: string,
callback: Callback5<T1, T2, T3, T4, T5>)
any<T1, T2, T3, T4, T5, T6>(ev1: string, ev2: string, ev3: string, ev4: string, ev5: string, ev6: string,
...rest: (string | Callback6Rest<T1, T2, T3, T4, T5, T6>)[])
/**
* The callback will be executed when the event name not equals with assigned event.
* @param {String} eventname Event name.
* @param {Function} callback Callback.
*/
not(event: string, callback: Function)
/**
* Success callback wrapper, will handler err for you.
*
* ```js
* fs.readFile('foo.txt', ep.done('content'));
*
* // equal to =>
*
* fs.readFile('foo.txt', function (err, content) {
* if (err) {
* return ep.emit('error', err);
* }
* ep.emit('content', content);
* });
* ```
*
* ```js
* fs.readFile('foo.txt', ep.done('content', function (content) {
* return content.trim();
* }));
*
* // equal to =>
*
* fs.readFile('foo.txt', function (err, content) {
* if (err) {
* return ep.emit('error', err);
* }
* ep.emit('content', content.trim());
* });
* ```
* @param {Function|String} handler, success callback or event name will be emit after callback.
* @return {Function}
*/
done(handler: Function | string, callback?: Function)
/**
* make done async
* @return {Function} delay done
*/
doneLater(handler: Function | string, callback?: Function)
}
export default EventProxy