Skip to content

Commit 1fa34a5

Browse files
committed
Merge pull request DefinitelyTyped#3065 from ryiwamoto/eventemitter2
Add eventemitter2
2 parents 06b7f3a + 0e1e696 commit 1fa34a5

File tree

3 files changed

+239
-0
lines changed

3 files changed

+239
-0
lines changed

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ All definitions files include a header with the author and editors, so at some p
9191
* [ES6-Promises](https://github.com/jakearchibald/ES6-Promises) (by [François de Campredon](https://github.com/fdecampredon/))
9292
* [Esprima](http://esprima.org/) (by [Teppei Sato](https://github.com/teppeis))
9393
* [expect.js](https://github.com/LearnBoost/expect.js) (by [Teppei Sato](https://github.com/teppeis))
94+
* [EventEmitter2](https://github.com/asyncly/EventEmitter2) (by [Ryo Iwamoto](https://github.com/ryiwamoto))
9495
* [expectations](https://github.com/spmason/expectations) (by [vvakame](https://github.com/vvakame))
9596
* [Express](http://expressjs.com/) (by [Boris Yankov](https://github.com/borisyankov))
9697
* [express-session](https://www.npmjs.org/package/express-session) (by [Hiroki Horiuchi](https://github.com/horiuchi/))

eventemitter2/eventemitter2-tests.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
///<reference path="eventemitter2.d.ts"/>
2+
3+
// import eventemitter2 = require("eventemitter2");
4+
// var EventEmitter2 = eventemitter2.EventEmitter2;
5+
6+
function testConfiguration() {
7+
var foo = new EventEmitter2({
8+
wildcard: true,
9+
delimiter: '::',
10+
newListener: false,
11+
maxListeners: 20
12+
});
13+
var bar = new EventEmitter2({});
14+
var bazz = new EventEmitter2();
15+
}
16+
17+
var server = new EventEmitter2();
18+
19+
function testAddListener() {
20+
server.addListener('data', function (value1: any, value2: any, value3: any) {
21+
console.log('The event was raised!');
22+
});
23+
24+
server.addListener('data', function (value: any) {
25+
console.log('The event was raised!');
26+
});
27+
}
28+
29+
function testOn() {
30+
server.on('data', function (value1: any, value2: any, value3: any) {
31+
console.log('The event was raised!');
32+
});
33+
34+
server.on('data', function (value: any) {
35+
console.log('The event was raised!');
36+
});
37+
}
38+
39+
function testOnAny() {
40+
server.onAny(function (value: any) {
41+
console.log('All events trigger this.');
42+
});
43+
}
44+
45+
function testOffAny() {
46+
server.offAny(function (value: any) {
47+
console.log('The event was raised!');
48+
});
49+
}
50+
51+
function testOnce() {
52+
server.once('get', function (value: any) {
53+
console.log('Ah, we have our first value!');
54+
});
55+
}
56+
57+
function testMany() {
58+
server.many('get', 4, function (value: any) {
59+
console.log('This event will be listened to exactly four times.');
60+
});
61+
}
62+
63+
function testRemoveListener() {
64+
var callback = function (value: any) {
65+
console.log('someone connected!');
66+
};
67+
server.on('get', callback);
68+
server.removeListener('get', callback);
69+
}
70+
71+
function testRemoveAllListeners() {
72+
server.removeAllListeners(["test::event", "another::test::event"]);
73+
server.removeAllListeners("test");
74+
server.removeAllListeners();
75+
}
76+
77+
function testSetMaxListeners() {
78+
server.setMaxListeners(40);
79+
}
80+
81+
function testListeners() {
82+
console.log(server.listeners('get'));
83+
}
84+
85+
function testListenersAny() {
86+
console.log(server.listenersAny()[0]);
87+
}
88+
89+
function testEmit() {
90+
server.emit('foo.bazz');
91+
server.emit(['foo', 'bar']);
92+
}

eventemitter2/eventemitter2.d.ts

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// Type definitions for EventEmitter2 v0.14.4
2+
// Project: https://github.com/asyncly/EventEmitter2
3+
// Definitions by: ryiwamoto <https://github.com/ryiwamoto/>
4+
// Definitions: https://github.com/borisyankov/DefinitelyTyped
5+
6+
declare module eventemitter2 {
7+
interface Configuration {
8+
/**
9+
* use wildcards
10+
*/
11+
wildcard?: boolean;
12+
13+
/**
14+
* the delimiter used to segment namespaces, defaults to `.`.
15+
*/
16+
delimiter?: string;
17+
18+
/**
19+
* if you want to emit the newListener event set to true.
20+
*/
21+
newListener?: boolean;
22+
23+
/**
24+
* max listeners that can be assigned to an event, default 10.
25+
*/
26+
maxListeners?: number;
27+
}
28+
29+
export class EventEmitter2 {
30+
/**
31+
* @param conf
32+
*/
33+
constructor(conf?: Configuration);
34+
35+
/**
36+
* Adds a listener to the end of the listeners array for the specified event.
37+
* @param event
38+
* @param listener
39+
*/
40+
addListener(event: string, listener: Function): EventEmitter2;
41+
42+
/**
43+
* Adds a listener to the end of the listeners array for the specified event.
44+
* @param event
45+
* @param listener
46+
*/
47+
on(event: string, listener: Function): EventEmitter2;
48+
49+
/**
50+
* Adds a listener that will be fired when any event is emitted.
51+
* @param listener
52+
*/
53+
onAny(listener: Function): EventEmitter2;
54+
55+
/**
56+
* Removes the listener that will be fired when any event is emitted.
57+
* @param listener
58+
*/
59+
offAny(listener: Function): EventEmitter2;
60+
61+
/**
62+
* Adds a one time listener for the event.
63+
* The listener is invoked only the first time the event is fired, after which it is removed.
64+
* @param event
65+
* @param listener
66+
*/
67+
once(event: string, listener: Function): EventEmitter2;
68+
69+
/**
70+
* Adds a listener that will execute n times for the event before being removed.
71+
* The listener is invoked only the first n times the event is fired, after which it is removed.
72+
* @param event
73+
* @param timesToListen
74+
* @param listener
75+
*/
76+
many(event: string, timesToListen: number, listener: Function): EventEmitter2;
77+
78+
/**
79+
* Remove a listener from the listener array for the specified event.
80+
* Caution: changes array indices in the listener array behind the listener.
81+
* @param event
82+
* @param listener
83+
*/
84+
removeListener(event: string, listener: Function): EventEmitter2;
85+
86+
/**
87+
* Remove a listener from the listener array for the specified event.
88+
* Caution: changes array indices in the listener array behind the listener.
89+
* @param event
90+
* @param listener
91+
*/
92+
off(event: string, listener: Function): EventEmitter2;
93+
94+
/**
95+
* Removes all listeners, or those of the specified event.
96+
* @param event
97+
*/
98+
removeAllListeners(event?: string): EventEmitter2;
99+
100+
/**
101+
* Removes all listeners, or those of the specified event.
102+
* @param events
103+
*/
104+
removeAllListeners(events: string[]): EventEmitter2;
105+
106+
/**
107+
* By default EventEmitters will print a warning if more than 10 listeners are added to it.
108+
* This is a useful default which helps finding memory leaks.
109+
* Obviously not all Emitters should be limited to 10. This function allows that to be increased.
110+
* Set to zero for unlimited.
111+
* @param n
112+
*/
113+
setMaxListeners(n: number): void;
114+
115+
/**
116+
* Returns an array of listeners for the specified event. This array can be manipulated, e.g. to remove listeners.
117+
* @param event
118+
*/
119+
listeners(event: string): Function[];
120+
121+
/**
122+
* Returns an array of listeners that are listening for any event that is specified.
123+
* This array can be manipulated, e.g. to remove listeners.
124+
*/
125+
listenersAny(): Function[];
126+
127+
/**
128+
* Execute each of the listeners that may be listening for the specified event name in order with the list of arguments.
129+
* @param event
130+
* @param args
131+
*/
132+
emit(event: string, ...args: string[]): boolean;
133+
134+
/**
135+
* Execute each of the listeners that may be listening for the specified event name in order with the list of arguments.
136+
* @param event
137+
*/
138+
emit(event: string[]): boolean;
139+
}
140+
}
141+
142+
declare module "eventemitter2" {
143+
export = eventemitter2;
144+
}
145+
146+
declare var EventEmitter2: typeof eventemitter2.EventEmitter2;

0 commit comments

Comments
 (0)