From 83fba1ebf26d766a4631ec4121ac6b387098da9b Mon Sep 17 00:00:00 2001 From: himself65 Date: Wed, 30 Jan 2019 22:34:55 +0800 Subject: [PATCH] events: onceWrapper returns target value PR-URL: https://github.com/nodejs/node/pull/25818 Reviewed-By: Luigi Pinca Reviewed-By: Ruben Bridgewater Reviewed-By: Colin Ihrig --- lib/events.js | 2 +- test/parallel/test-event-emitter-listeners.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/events.js b/lib/events.js index 62fa33463d678e..ec5e0870aa50a0 100644 --- a/lib/events.js +++ b/lib/events.js @@ -282,7 +282,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); } } diff --git a/test/parallel/test-event-emitter-listeners.js b/test/parallel/test-event-emitter-listeners.js index 9d2892d2e891ae..52a108ad0e29b6 100644 --- a/test/parallel/test-event-emitter-listeners.js +++ b/test/parallel/test-event-emitter-listeners.js @@ -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(); @@ -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); +}