From db42ad959d5fa077e05015d56b0b374faf1d51cd Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 25 Aug 2009 16:59:06 +0200 Subject: [PATCH] API: All EventEmitters emit "newListener" when listeners are added. The "newListener" event will also be emitted for listeners to "newListener". Maybe useful? --- src/events.js | 3 ++ .../test-event-emitter-add-listeners.js | 31 +++++++++++++++++++ website/api.txt | 23 +++++++++++++- 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 test/mjsunit/test-event-emitter-add-listeners.js diff --git a/src/events.js b/src/events.js index ef4dc393377..3b7ddc59889 100644 --- a/src/events.js +++ b/src/events.js @@ -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; diff --git a/test/mjsunit/test-event-emitter-add-listeners.js b/test/mjsunit/test-event-emitter-add-listeners.js new file mode 100644 index 00000000000..21a140f4d66 --- /dev/null +++ b/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); +} + + diff --git a/website/api.txt b/website/api.txt index f8566ae054c..d50aa1864e2 100644 --- a/website/api.txt +++ b/website/api.txt @@ -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