Browse Source

API: All EventEmitters emit "newListener" when listeners are added.

The "newListener" event will also be emitted for listeners to "newListener".
Maybe useful?
v0.7.4-release
Ryan 16 years ago
parent
commit
db42ad959d
  1. 3
      src/events.js
  2. 31
      test/mjsunit/test-event-emitter-add-listeners.js
  3. 23
      website/api.txt

3
src/events.js

@ -6,6 +6,9 @@ node.EventEmitter.prototype.addListener = function (type, listener) {
if (listener instanceof Function) { if (listener instanceof Function) {
if (!this._events) this._events = {}; if (!this._events) this._events = {};
if (!this._events.hasOwnProperty(type)) this._events[type] = []; if (!this._events.hasOwnProperty(type)) this._events[type] = [];
// To avoid recursion in the case that type == "newListeners"! Before
// adding it to the listeners, first emit "newListeners".
this.emit("newListener", [type, listener]);
this._events[type].push(listener); this._events[type].push(listener);
} }
return this; return this;

31
test/mjsunit/test-event-emitter-add-listeners.js

@ -0,0 +1,31 @@
include("mjsunit.js");
var e = new node.EventEmitter();
var events_new_listener_emited = [];
var times_hello_emited = 0;
function onLoad () {
e.addListener("newListener", function (event, listener) {
puts("newListener: " + event);
events_new_listener_emited.push(event);
});
e.addListener("hello", function (a, b) {
puts("hello");
times_hello_emited += 1
assertEquals("a", a);
assertEquals("b", b);
});
puts("start");
e.emit("hello", ["a", "b"]);
}
function onExit () {
assertArrayEquals(["hello"], events_new_listener_emited);
assertEquals(1, times_hello_emited);
}

23
website/api.txt

@ -110,6 +110,18 @@ complete.
==== +node.EventEmitter+ ==== +node.EventEmitter+
All EventEmitters emit the event +"newListener"+ when new listeners are
added.
[cols="1,2,10",options="header"]
|=========================================================
| Event | Parameters | Notes
| +"newListener"+ | +event, listener+| This event is made
any time someone adds
a new listener.
|=========================================================
+emitter.addListener(event, listener)+ :: +emitter.addListener(event, listener)+ ::
Adds a listener to the end of the listeners array for the specified event. Adds a listener to the end of the listeners array for the specified event.
+ +
@ -129,6 +141,15 @@ Execute each of the listeners in order with the array +args+ as arguments.
==== +node.Promise+ ==== +node.Promise+
Promises emit two special events +"success"+ and +"error"+.
[cols="1,2,10",options="header"]
|=========================================================
| Event | Parameters | Notes
| +"success"+ | (depends) |
| +"error"+ | (depends) |
|=========================================================
+node.Promise+ inherits from +node.eventEmitter+. A promise emits one of two +node.Promise+ inherits from +node.eventEmitter+. A promise emits one of two
events: +"success"+ or +"error"+. After emitting its event, it will not events: +"success"+ or +"error"+. After emitting its event, it will not
emit anymore events. emit anymore events.
@ -998,7 +1019,7 @@ resolution.addErrback(function (code, msg) {
+node.dns.resolve4(domain)+:: +node.dns.resolve4(domain)+::
Resolves a domain (e.g. +"google.com"+) into an array of IPv4 addresses (e.g. Resolves a domain (e.g. +"google.com"+) into an array of IPv4 addresses (e.g.
+["74.125.79.104","74.125.79.105","74.125.79.106","74.125.79.147","74.125.79.99","74.125.79.103"]+). +["74.125.79.104", "74.125.79.105", "74.125.79.106"]+).
This function returns a promise. This function returns a promise.
- on success: returns +addresses, ttl, cname+. +ttl+ (time-to-live) is an integer - on success: returns +addresses, ttl, cname+. +ttl+ (time-to-live) is an integer
specifying the number of seconds this result is valid for. +cname+ is the specifying the number of seconds this result is valid for. +cname+ is the

Loading…
Cancel
Save