1 [![build-status](https://www.codeship.io/projects/3ad58940-4c7d-0131-15d5-5a8cd3f550f8/status)](https://www.codeship.io/projects/11259)
5 EventEmitter2 is an implementation of the EventEmitter found in Node.js
10 - Namespaces/Wildcards.
11 - Times To Listen (TTL), extends the `once` concept with `many`.
12 - Browser environment compatibility.
13 - Demonstrates good performance in benchmarks
16 EventEmitterHeatUp x 3,728,965 ops/sec \302\2610.68% (60 runs sampled)
17 EventEmitter x 2,822,904 ops/sec \302\2610.74% (63 runs sampled)
18 EventEmitter2 x 7,251,227 ops/sec \302\2610.55% (58 runs sampled)
19 EventEmitter2 (wild) x 3,220,268 ops/sec \302\2610.44% (65 runs sampled)
20 Fastest is EventEmitter2
23 ### Differences (Non breaking, compatible with existing EventEmitter)
25 - The constructor takes a configuration object.
28 var EventEmitter2 = require('eventemitter2').EventEmitter2;
29 var server = new EventEmitter2({
37 // the delimiter used to segment namespaces, defaults to `.`.
42 // if you want to emit the newListener event set to true.
47 // max listeners that can be assigned to an event, default 10.
53 - Getting the actual event that fired.
56 server.on('foo.*', function(value1, value2) {
57 console.log(this.event, value1, value2);
61 - Fire an event N times and then remove it, an extension of the `once` concept.
64 server.many('foo', 4, function() {
69 - Pass in a namespaced event as an array rather than a delimited string.
72 server.many(['foo', 'bar', 'bazz'], function() {
80 When an `EventEmitter` instance experiences an error, the typical action is
81 to emit an `error` event. Error events are treated as a special case.
82 If there is no listener for it, then the default action is to print a stack
83 trace and exit the program.
85 All EventEmitters emit the event `newListener` when new listeners are
89 **Namespaces** with **Wildcards**
90 To use namespaces/wildcards, pass the `wildcard` option into the EventEmitter
91 constructor. When namespaces/wildcards are enabled, events can either be
92 strings (`foo.bar`) separated by a delimiter or arrays (`['foo', 'bar']`). The
93 delimiter is also configurable as a constructor option.
95 An event name passed to any event emitter method can contain a wild card (the
96 `*` character). If the event name is a string, a wildcard may appear as `foo.*`.
97 If the event name is an array, the wildcard may appear as `['foo', '*']`.
99 If either of the above described events were passed to the `on` method,
100 subsequent emits such as the following would be observed...
103 emitter.emit('foo.bazz');
104 emitter.emit(['foo', 'bar']);
108 ### emitter.addListener(event, listener)
109 ### emitter.on(event, listener)
111 Adds a listener to the end of the listeners array for the specified event.
114 server.on('data', function(value1, value2, value3, ...) {
115 console.log('The event was raised!');
120 server.on('data', function(value) {
121 console.log('The event was raised!');
125 ### emitter.onAny(listener)
127 Adds a listener that will be fired when any event is emitted.
130 server.onAny(function(value) {
131 console.log('All events trigger this.');
135 ### emitter.offAny(listener)
137 Removes the listener that will be fired when any event is emitted.
140 server.offAny(function(value) {
141 console.log('The event was raised!');
145 #### emitter.once(event, listener)
147 Adds a **one time** listener for the event. The listener is invoked
148 only the first time the event is fired, after which it is removed.
151 server.once('get', function (value) {
152 console.log('Ah, we have our first value!');
156 ### emitter.many(event, timesToListen, listener)
158 Adds a listener that will execute **n times** for the event before being
159 removed. The listener is invoked only the first **n times** the event is
160 fired, after which it is removed.
163 server.many('get', 4, function (value) {
164 console.log('This event will be listened to exactly four times.');
169 ### emitter.removeListener(event, listener)
170 ### emitter.off(event, listener)
172 Remove a listener from the listener array for the specified event.
173 **Caution**: changes array indices in the listener array behind the listener.
176 var callback = function(value) {
177 console.log('someone connected!');
179 server.on('get', callback);
181 server.removeListener('get', callback);
185 ### emitter.removeAllListeners([event])
187 Removes all listeners, or those of the specified event.
190 ### emitter.setMaxListeners(n)
192 By default EventEmitters will print a warning if more than 10 listeners
193 are added to it. This is a useful default which helps finding memory leaks.
194 Obviously not all Emitters should be limited to 10. This function allows
195 that to be increased. Set to zero for unlimited.
198 ### emitter.listeners(event)
200 Returns an array of listeners for the specified event. This array can be
201 manipulated, e.g. to remove listeners.
204 server.on('get', function(value) {
205 console.log('someone connected!');
207 console.log(server.listeners('get')); // [ [Function] ]
210 ### emitter.listenersAny()
212 Returns an array of listeners that are listening for any event that is
213 specified. This array can be manipulated, e.g. to remove listeners.
216 server.onAny(function(value) {
217 console.log('someone connected!');
219 console.log(server.listenersAny()[0]); // [ [Function] ]
222 ### emitter.emit(event, [arg1], [arg2], [...])
224 Execute each of the listeners that may be listening for the specified event
225 name in order with the list of arguments.
231 Copyright (c) 2011 hij1nx <http://www.twitter.com/hij1nx>
233 Permission is hereby granted, free of charge, to any person obtaining a copy
234 of this software and associated documentation files (the 'Software'), to deal
235 in the Software without restriction, including without limitation the rights
236 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
237 copies of the Software, and to permit persons to whom the Software is furnished
238 to do so, subject to the following conditions:
240 The above copyright notice and this permission notice shall be included in all
241 copies or substantial portions of the Software.
243 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
244 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
245 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
246 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
247 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
248 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.