0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

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

The "newListener" event will also be emitted for listeners to "newListener".
Maybe useful?
This commit is contained in:
Ryan 2009-08-25 16:59:06 +02:00
parent b5a1585470
commit db42ad959d
3 changed files with 56 additions and 1 deletions

View File

@ -6,6 +6,9 @@ node.EventEmitter.prototype.addListener = function (type, listener) {
if (listener instanceof Function) {
if (!this._events) this._events = {};
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);
}
return this;

View File

@ -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);
}

View File

@ -110,6 +110,18 @@ complete.
==== +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)+ ::
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+
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
events: +"success"+ or +"error"+. After emitting its event, it will not
emit anymore events.
@ -998,7 +1019,7 @@ resolution.addErrback(function (code, msg) {
+node.dns.resolve4(domain)+::
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.
- 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