Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for removing .once listeners

Closes GH-806.
  • Loading branch information...
commit 53bec1c8625c38888a15f7eb278cc19a212c154a 1 parent 0fb44a2
@3rd-Eden 3rd-Eden authored ry committed
Showing with 28 additions and 6 deletions.
  1. +20 −6 lib/events.js
  2. +8 −0 test/simple/test-event-emitter-once.js
View
26 lib/events.js
@@ -138,10 +138,13 @@ EventEmitter.prototype.on = EventEmitter.prototype.addListener;
EventEmitter.prototype.once = function(type, listener) {
var self = this;
- self.on(type, function g() {
+ function g() {
self.removeListener(type, g);
listener.apply(this, arguments);
- });
+ };
+
+ g.listener = listener;
+ self.on(type, g);
return this;
};
@@ -157,12 +160,23 @@ EventEmitter.prototype.removeListener = function(type, listener) {
var list = this._events[type];
if (isArray(list)) {
- var i = list.indexOf(listener);
- if (i < 0) return this;
- list.splice(i, 1);
+ var position = -1;
+ for (var i = 0, length = list.length; i < length; i++) {
+ if (list[i] === listener ||
+ (list[i].listener && list[i].listener === listener))
+ {
+ position = i;
+ break;
+ }
+ }
+
+ if (position < 0) return this;
+ list.splice(position, 1);
if (list.length == 0)
delete this._events[type];
- } else if (this._events[type] === listener) {
+ } else if (list === listener ||
+ (list.listener && list.listener === listener))
+ {
delete this._events[type];
}
View
8 test/simple/test-event-emitter-once.js
@@ -35,6 +35,14 @@ e.emit('hello', 'a', 'b');
e.emit('hello', 'a', 'b');
e.emit('hello', 'a', 'b');
+var remove = function() {
+ assert.fail(1,0, 'once->foo should not be emitted', '!');
+};
+
+e.once('foo', remove);
+e.removeListener('foo', remove);
+e.emit('foo');
+
process.addListener('exit', function() {
assert.equal(1, times_hello_emited);
});
Please sign in to comment.
Something went wrong with that request. Please try again.