From bd6c08a984edaec6c10ba5e68409aa2a2cecc7f8 Mon Sep 17 00:00:00 2001 From: fwg Date: Fri, 13 Nov 2009 17:12:41 +0100 Subject: [PATCH] Add EventEmitter.removeListener --- doc/api.txt | 3 +++ src/node.js | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/doc/api.txt b/doc/api.txt index fe50a816e8..5b1888ce3c 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -207,6 +207,9 @@ server.addListener("connection", function (socket) { }); ---------------------------------------- ++emitter.removeListener(event, listener)+ :: +Remove a listener from the listener array for the specified event. +*Caution*: changes array indices in the listener array behind the listener. +emitter.listeners(event)+ :: Returns an array of listeners for the specified event. This array can be diff --git a/src/node.js b/src/node.js index b247ee54cc..48c3841de0 100644 --- a/src/node.js +++ b/src/node.js @@ -201,6 +201,17 @@ process.EventEmitter.prototype.addListener = function (type, listener) { return this; }; +process.EventEmitter.prototype.removeListener = function (type, listener) { + if (listener instanceof Function) { + // does not use listeners(), so no side effect of creating _events[type] + if (!this._events || !this._events.hasOwnProperty(type)) return; + var list = this._events[type]; + if (list.indexOf(listener) < 0) return; + list.splice(list.indexOf(listener), 1); + } + return this; +}; + process.EventEmitter.prototype.listeners = function (type) { if (!this._events) this._events = {}; if (!this._events.hasOwnProperty(type)) this._events[type] = [];