Permalink
Browse files

Add removeAllListeners

  • Loading branch information...
1 parent b90d63b commit f8eb16372835f1f17deeb2b169c6d5a14a50cd40 @aheckmann aheckmann committed with ry Mar 9, 2010
Showing with 15 additions and 0 deletions.
  1. +3 −0 doc/api.txt
  2. +6 −0 src/node.js
  3. +6 −0 test/simple/test-event-emitter-modify-in-emit.js
View
@@ -263,6 +263,9 @@ server.addListener("connection", function (socket) {
Remove a listener from the listener array for the specified event.
*Caution*: changes array indices in the listener array behind the listener.
++emitter.removeAllListeners(event)+ ::
+Removes all listeners from the listener array for the specified event.
+
+emitter.listeners(event)+ ::
Returns an array of listeners for the specified event. This array can be
manipulated, e.g. to remove listeners.
View
@@ -186,6 +186,12 @@ var eventsModule = createInternalModule('events', function (exports) {
return this;
};
+ process.EventEmitter.prototype.removeAllListeners = function (type) {
+ // does not use listeners(), so no side effect of creating _events[type]
+ if (!type || !this._events || !this._events.hasOwnProperty(type)) return this;
+ this._events[type].length = 0;
+ };
+
process.EventEmitter.prototype.listeners = function (type) {
if (!this._events) this._events = {};
if (!this._events.hasOwnProperty(type)) this._events[type] = [];
@@ -30,3 +30,9 @@ assert.deepEqual(["callback1", "callback2"], callbacks_called);
e.emit("foo");
assert.equal(0, e.listeners("foo").length);
assert.deepEqual(["callback1", "callback2"], callbacks_called);
+
+e.addListener("foo", callback1);
+e.addListener("foo", callback2);
+assert.equal(2, e.listeners("foo").length)
+e.removeAllListeners("foo")
+assert.equal(0, e.listeners("foo").length)

0 comments on commit f8eb163

Please sign in to comment.