Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ability to removeListener when in the same emit loop #65

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function EE(fn, context, once) {
this.fn = fn;
this.context = context;
this.once = once || false;
this.skip = false;
}

/**
Expand Down Expand Up @@ -144,6 +145,7 @@ EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {

for (i = 0; i < length; i++) {
if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true);
if (listeners[i].skip) continue;

switch (len) {
case 1: listeners[i].fn.call(listeners[i].context); break;
Expand Down Expand Up @@ -236,6 +238,9 @@ EventEmitter.prototype.removeListener = function removeListener(event, fn, conte
}
} else {
for (var i = 0, events = [], length = listeners.length; i < length; i++) {
if (!once && listeners[i].fn === fn) {
listeners[i].skip = true;
}
if (
listeners[i].fn !== fn
|| (once && !listeners[i].once)
Expand Down
24 changes: 24 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,30 @@ describe('EventEmitter', function tests() {
assume(e.listeners('foo')).eql([]);
assume(e._eventsCount).equals(0);
});

it('removes a listener when removed as part handler callback', function() {
var e = new EventEmitter()
, foo = { bar: true }
, calledThird = 0;

function first() {
foo = null;
e.removeListener('update', second);
}
function second() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldnt second never be called? we should test that this is the case

Copy link
Author

@bigtimebuddy bigtimebuddy Aug 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copying this test to what's in master (no change from PR), this test fails with TypeError: Cannot read property 'bar' of null. Current master attempts to call second in the same emit call. It's not removed until the next stack.

assume(foo.bar).equals(true);
}
function third() {
calledThird++;
}
e.on('update', first);
e.on('update', second);
e.once('update', second);
e.on('update', third);
e.once('update', third);
e.emit('update');
assume(calledThird).equals(2);
});
});

describe('EventEmitter#removeAllListeners', function () {
Expand Down