type Callback0 = () => any; type Callback1 = (arg1: T1) => any; type Callback2 = (arg1: T1, arg2: T2) => any; type Callback3 = (arg1: T1, arg2: T2, arg3: T3) => any; type Callback4 = (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => any; type Callback5 = (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => any; type Callback6Rest = (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, ...rest: any[]) => any; type ErrorHandler = (error: Error, arg: T) => any type ErrorHandler2 = (error: Error, arg1: T1, arg2: T2) => any type ErrorHandler3 = (error: Error, arg1: T1, arg2: T2, arg3: T3) => any type ErrorHandler4 = (error: Error, arg1: T1, arg2: T2, arg3: T3, arg4: T4) => any type ErrorHandler5 = (error: Error, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => any type ErrorHandler6Rest = (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(ev1: string, callback: Callback1, errorHandler?: ErrorHandler): EventProxy static create(ev1: string, ev2: string, callback: Callback2, errorHandler?: ErrorHandler): EventProxy static create(ev1: string, ev2: string, ev3: string, callback: Callback3, errorHandler?: ErrorHandler): EventProxy static create(ev1: string, ev2: string, ev3: string, ev4: string, callback: Callback4, errorHandler?: ErrorHandler): EventProxy static create(ev1: string, ev2: string, ev3: string, ev4: string, ev5: string, callback: Callback5, errorHandler?: ErrorHandler): EventProxy static create(ev1: string, ev2: string, ev3: string, ev4: string, ev5: string, ev6: string, ...rest: (string | ErrorHandler | Callback6Rest)[]): 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(event: string, callback: Callback1): this addListener(event: string, callback: Callback2): this addListener(event: string, callback: Callback3): this addListener(event: string, callback: Callback4): this addListener(event: string, callback: Callback5): this addListener(event: string, callback: Callback6Rest): this /** * `addListener` alias, `bind` */ bind(event: string, callback: Callback1): this bind(event: string, callback: Callback2): this bind(event: string, callback: Callback3): this bind(event: string, callback: Callback4): this bind(event: string, callback: Callback5): this bind(event: string, callback: Callback6Rest): this /** * `addListener` alias, `on` */ on(event: string, callback: Callback1): this on(event: string, callback: Callback2): this on(event: string, callback: Callback3): this on(event: string, callback: Callback4): this on(event: string, callback: Callback5): this on(event: string, callback: Callback6Rest): this /** * `addListener` alias, `subscribe` */ subscribe(event: string, callback: Callback1): this subscribe(event: string, callback: Callback2): this subscribe(event: string, callback: Callback3): this subscribe(event: string, callback: Callback4): this subscribe(event: string, callback: Callback5): this subscribe(event: string, callback: Callback6Rest): this /** * Bind an event, but put the callback into head of all callbacks. * @param {String} eventname Event name. * @param {Function} callback Callback. */ headbind(event: string, callback: Callback1): this headbind(event: string, callback: Callback2): this headbind(event: string, callback: Callback3): this headbind(event: string, callback: Callback4): this headbind(event: string, callback: Callback5): this headbind(event: string, callback: Callback6Rest): 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(event: string, arg: T1) trigger(event: string, arg1: T1, arg2: T2) trigger(event: string, arg1: T1, arg2: T2, arg3: T3) trigger(event: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4) trigger(event: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) trigger(event: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, ...rest: any[]) /** * `trigger` alias, `emit` */ emit(event: string) emit(event: string, arg: T1) emit(event: string, arg1: T1, arg2: T2) emit(event: string, arg1: T1, arg2: T2, arg3: T3) emit(event: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4) emit(event: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) emit(event: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, ...rest: any[]) /** * `trigger` alias, `fire` */ fire(event: string) fire(event: string, arg: T1) fire(event: string, arg1: T1, arg2: T2) fire(event: string, arg1: T1, arg2: T2, arg3: T3) fire(event: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4) fire(event: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) fire(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(event: string, callback: Callback1): this once(event: string, callback: Callback2): this once(event: string, callback: Callback3): this once(event: string, callback: Callback4): this once(event: string, callback: Callback5): this once(event: string, callback: Callback6Rest): this /** * emitLater * make emit async */ emitLater(event: string, callback: Callback1): void emitLater(event: string, callback: Callback2): void emitLater(event: string, callback: Callback3): void emitLater(event: string, callback: Callback4): void emitLater(event: string, callback: Callback5): void emitLater(event: string, callback: Callback6Rest): 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(event: string, callback: Callback1, data: T): this /** * `immediate` alias, `asap` */ asap(event: string, callback: Callback1, 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(callback: ErrorHandler) fail(callback: ErrorHandler2) fail(callback: ErrorHandler3) fail(callback: ErrorHandler4) fail(callback: ErrorHandler5) fail(callback: ErrorHandler6Rest) /** * 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(event: string, times: number, callback: Callback1): this after(event: string, times: number, callback: Callback2): this after(event: string, times: number, callback: Callback3): this after(event: string, times: number, callback: Callback4): this after(event: string, times: number, callback: Callback5): this after(event: string, times: number, callback: Callback6Rest): 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(event: string, callback: Callback1): ErrorHandler group(event: string, callback: Callback2): ErrorHandler2 group(event: string, callback: Callback3): ErrorHandler3 group(event: string, callback: Callback4): ErrorHandler4 group(event: string, callback: Callback5): ErrorHandler5 group(event: string, callback: Callback6Rest) : ErrorHandler6Rest /** * 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(ev1: string, callback: Callback1) any(ev1: string, ev2: string, callback: Callback2) any(ev1: string, ev2: string, ev3: string, callback: Callback3) any(ev1: string, ev2: string, ev3: string, ev4: string, callback: Callback4) any(ev1: string, ev2: string, ev3: string, ev4: string, ev5: string, callback: Callback5) any(ev1: string, ev2: string, ev3: string, ev4: string, ev5: string, ev6: string, ...rest: (string | Callback6Rest)[]) /** * 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