mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-01 09:01:45 +08:00
add eventemitter2
This commit is contained in:
@@ -91,6 +91,7 @@ All definitions files include a header with the author and editors, so at some p
|
||||
* [ES6-Promises](https://github.com/jakearchibald/ES6-Promises) (by [François de Campredon](https://github.com/fdecampredon/))
|
||||
* [Esprima](http://esprima.org/) (by [Teppei Sato](https://github.com/teppeis))
|
||||
* [expect.js](https://github.com/LearnBoost/expect.js) (by [Teppei Sato](https://github.com/teppeis))
|
||||
* [EventEmitter2](https://github.com/asyncly/EventEmitter2) (by [Ryo Iwamoto](https://github.com/ryiwamoto))
|
||||
* [expectations](https://github.com/spmason/expectations) (by [vvakame](https://github.com/vvakame))
|
||||
* [Express](http://expressjs.com/) (by [Boris Yankov](https://github.com/borisyankov))
|
||||
* [express-session](https://www.npmjs.org/package/express-session) (by [Hiroki Horiuchi](https://github.com/horiuchi/))
|
||||
|
||||
92
eventemitter2/eventemitter2-tests.ts
Normal file
92
eventemitter2/eventemitter2-tests.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
///<reference path="eventemitter2.d.ts"/>
|
||||
|
||||
// import eventemitter2 = require("eventemitter2");
|
||||
// var EventEmitter2 = eventemitter2.EventEmitter2;
|
||||
|
||||
function testConfiguration() {
|
||||
var foo = new EventEmitter2({
|
||||
wildcard: true,
|
||||
delimiter: '::',
|
||||
newListener: false,
|
||||
maxListeners: 20
|
||||
});
|
||||
var bar = new EventEmitter2({});
|
||||
var bazz = new EventEmitter2();
|
||||
}
|
||||
|
||||
var server = new EventEmitter2();
|
||||
|
||||
function testAddListener() {
|
||||
server.addListener('data', function (value1: any, value2: any, value3: any) {
|
||||
console.log('The event was raised!');
|
||||
});
|
||||
|
||||
server.addListener('data', function (value: any) {
|
||||
console.log('The event was raised!');
|
||||
});
|
||||
}
|
||||
|
||||
function testOn() {
|
||||
server.on('data', function (value1: any, value2: any, value3: any) {
|
||||
console.log('The event was raised!');
|
||||
});
|
||||
|
||||
server.on('data', function (value: any) {
|
||||
console.log('The event was raised!');
|
||||
});
|
||||
}
|
||||
|
||||
function testOnAny() {
|
||||
server.onAny(function (value: any) {
|
||||
console.log('All events trigger this.');
|
||||
});
|
||||
}
|
||||
|
||||
function testOffAny() {
|
||||
server.offAny(function (value: any) {
|
||||
console.log('The event was raised!');
|
||||
});
|
||||
}
|
||||
|
||||
function testOnce() {
|
||||
server.once('get', function (value: any) {
|
||||
console.log('Ah, we have our first value!');
|
||||
});
|
||||
}
|
||||
|
||||
function testMany() {
|
||||
server.many('get', 4, function (value: any) {
|
||||
console.log('This event will be listened to exactly four times.');
|
||||
});
|
||||
}
|
||||
|
||||
function testRemoveListener() {
|
||||
var callback = function (value: any) {
|
||||
console.log('someone connected!');
|
||||
};
|
||||
server.on('get', callback);
|
||||
server.removeListener('get', callback);
|
||||
}
|
||||
|
||||
function testRemoveAllListeners() {
|
||||
server.removeAllListeners(["test::event", "another::test::event"]);
|
||||
server.removeAllListeners("test");
|
||||
server.removeAllListeners();
|
||||
}
|
||||
|
||||
function testSetMaxListeners() {
|
||||
server.setMaxListeners(40);
|
||||
}
|
||||
|
||||
function testListeners() {
|
||||
console.log(server.listeners('get'));
|
||||
}
|
||||
|
||||
function testListenersAny() {
|
||||
console.log(server.listenersAny()[0]);
|
||||
}
|
||||
|
||||
function testEmit() {
|
||||
server.emit('foo.bazz');
|
||||
server.emit(['foo', 'bar']);
|
||||
}
|
||||
146
eventemitter2/eventemitter2.d.ts
vendored
Normal file
146
eventemitter2/eventemitter2.d.ts
vendored
Normal file
@@ -0,0 +1,146 @@
|
||||
// Type definitions for EventEmitter2 v0.14.4
|
||||
// Project: https://github.com/asyncly/EventEmitter2
|
||||
// Definitions by: ryiwamoto <https://github.com/ryiwamoto/>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
declare module eventemitter2 {
|
||||
interface Configuration {
|
||||
/**
|
||||
* use wildcards
|
||||
*/
|
||||
wildcard?: boolean;
|
||||
|
||||
/**
|
||||
* the delimiter used to segment namespaces, defaults to `.`.
|
||||
*/
|
||||
delimiter?: string;
|
||||
|
||||
/**
|
||||
* if you want to emit the newListener event set to true.
|
||||
*/
|
||||
newListener?: boolean;
|
||||
|
||||
/**
|
||||
* max listeners that can be assigned to an event, default 10.
|
||||
*/
|
||||
maxListeners?: number;
|
||||
}
|
||||
|
||||
export class EventEmitter2 {
|
||||
/**
|
||||
* @param conf
|
||||
*/
|
||||
constructor(conf?: Configuration);
|
||||
|
||||
/**
|
||||
* Adds a listener to the end of the listeners array for the specified event.
|
||||
* @param event
|
||||
* @param listener
|
||||
*/
|
||||
addListener(event: string, listener: Function): EventEmitter2;
|
||||
|
||||
/**
|
||||
* Adds a listener to the end of the listeners array for the specified event.
|
||||
* @param event
|
||||
* @param listener
|
||||
*/
|
||||
on(event: string, listener: Function): EventEmitter2;
|
||||
|
||||
/**
|
||||
* Adds a listener that will be fired when any event is emitted.
|
||||
* @param listener
|
||||
*/
|
||||
onAny(listener: Function): EventEmitter2;
|
||||
|
||||
/**
|
||||
* Removes the listener that will be fired when any event is emitted.
|
||||
* @param listener
|
||||
*/
|
||||
offAny(listener: Function): EventEmitter2;
|
||||
|
||||
/**
|
||||
* Adds a one time listener for the event.
|
||||
* The listener is invoked only the first time the event is fired, after which it is removed.
|
||||
* @param event
|
||||
* @param listener
|
||||
*/
|
||||
once(event: string, listener: Function): EventEmitter2;
|
||||
|
||||
/**
|
||||
* Adds a listener that will execute n times for the event before being removed.
|
||||
* The listener is invoked only the first n times the event is fired, after which it is removed.
|
||||
* @param event
|
||||
* @param timesToListen
|
||||
* @param listener
|
||||
*/
|
||||
many(event: string, timesToListen: number, listener: Function): EventEmitter2;
|
||||
|
||||
/**
|
||||
* Remove a listener from the listener array for the specified event.
|
||||
* Caution: changes array indices in the listener array behind the listener.
|
||||
* @param event
|
||||
* @param listener
|
||||
*/
|
||||
removeListener(event: string, listener: Function): EventEmitter2;
|
||||
|
||||
/**
|
||||
* Remove a listener from the listener array for the specified event.
|
||||
* Caution: changes array indices in the listener array behind the listener.
|
||||
* @param event
|
||||
* @param listener
|
||||
*/
|
||||
off(event: string, listener: Function): EventEmitter2;
|
||||
|
||||
/**
|
||||
* Removes all listeners, or those of the specified event.
|
||||
* @param event
|
||||
*/
|
||||
removeAllListeners(event?: string): EventEmitter2;
|
||||
|
||||
/**
|
||||
* Removes all listeners, or those of the specified event.
|
||||
* @param events
|
||||
*/
|
||||
removeAllListeners(events: string[]): EventEmitter2;
|
||||
|
||||
/**
|
||||
* By default EventEmitters will print a warning if more than 10 listeners are added to it.
|
||||
* This is a useful default which helps finding memory leaks.
|
||||
* Obviously not all Emitters should be limited to 10. This function allows that to be increased.
|
||||
* Set to zero for unlimited.
|
||||
* @param n
|
||||
*/
|
||||
setMaxListeners(n: number): void;
|
||||
|
||||
/**
|
||||
* Returns an array of listeners for the specified event. This array can be manipulated, e.g. to remove listeners.
|
||||
* @param event
|
||||
*/
|
||||
listeners(event: string): Function[];
|
||||
|
||||
/**
|
||||
* Returns an array of listeners that are listening for any event that is specified.
|
||||
* This array can be manipulated, e.g. to remove listeners.
|
||||
*/
|
||||
listenersAny(): Function[];
|
||||
|
||||
/**
|
||||
* Execute each of the listeners that may be listening for the specified event name in order with the list of arguments.
|
||||
* @param event
|
||||
* @param args
|
||||
*/
|
||||
emit(event: string, ...args: string[]): boolean;
|
||||
|
||||
/**
|
||||
* Execute each of the listeners that may be listening for the specified event name in order with the list of arguments.
|
||||
* @param event
|
||||
*/
|
||||
emit(event: string[]): boolean;
|
||||
}
|
||||
}
|
||||
|
||||
declare module "eventemitter2" {
|
||||
export = eventemitter2;
|
||||
}
|
||||
|
||||
declare var EventEmitter2: typeof eventemitter2.EventEmitter2;
|
||||
Reference in New Issue
Block a user