Skip to content

Commit

Permalink
events: onceWrapper returns target value
Browse files Browse the repository at this point in the history
PR-URL: #25818
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
himself65 authored and BethGriggs committed Apr 16, 2019
1 parent 32b83ea commit cb0fc65
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ function onceWrapper(...args) {
if (!this.fired) {
this.target.removeListener(this.type, this.wrapFn);
this.fired = true;
Reflect.apply(this.listener, this.target, args);
return Reflect.apply(this.listener, this.target, args);
}
}

Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-event-emitter-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ const events = require('events');

function listener() {}
function listener2() {}
function listener3() {
return 0;
}
function listener4() {
return 1;
}

{
const ee = new events.EventEmitter();
Expand Down Expand Up @@ -101,3 +107,15 @@ function listener2() {}
assert.strictEqual(wrappedListeners.length, 2);
assert.strictEqual(wrappedListeners[1].listener, listener);
}

{
const ee = new events.EventEmitter();
ee.once('foo', listener3);
ee.on('foo', listener4);
const rawListeners = ee.rawListeners('foo');
assert.strictEqual(rawListeners.length, 2);
assert.strictEqual(rawListeners[0](), 0);
const rawListener = ee.rawListeners('foo');
assert.strictEqual(rawListener.length, 1);
assert.strictEqual(rawListener[0](), 1);
}

0 comments on commit cb0fc65

Please sign in to comment.