From 906e9b6258f32277a5beea4f9d48de04ff4d27cb Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Mon, 26 Oct 2020 09:03:16 +0100 Subject: [PATCH] events: make eventTarget.removeAllListeners() return this Fixes: https://github.com/nodejs/node/issues/35762 --- doc/api/events.md | 2 ++ lib/internal/event_target.js | 2 ++ test/parallel/test-nodeeventtarget.js | 4 ++-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/doc/api/events.md b/doc/api/events.md index b6403e812ec3a6..a46fa06042cc2b 100644 --- a/doc/api/events.md +++ b/doc/api/events.md @@ -1525,6 +1525,8 @@ added: v14.5.0 * `type` {string} +* Returns: {EventTarget} this + Node.js-specific extension to the `EventTarget` class. If `type` is specified, removes all registered listeners for `type`, otherwise removes all registered listeners. diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 4db6af26e39a64..3e0ae460d22905 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -476,6 +476,8 @@ class NodeEventTarget extends EventTarget { } else { this[kEvents].clear(); } + + return this; } } diff --git a/test/parallel/test-nodeeventtarget.js b/test/parallel/test-nodeeventtarget.js index f4116f72a34d75..0f9218f540860d 100644 --- a/test/parallel/test-nodeeventtarget.js +++ b/test/parallel/test-nodeeventtarget.js @@ -117,11 +117,11 @@ const { on } = require('events'); strictEqual(eventTarget.listenerCount('foo'), 2); strictEqual(eventTarget.listenerCount('bar'), 1); deepStrictEqual(eventTarget.eventNames(), ['foo', 'bar']); - eventTarget.removeAllListeners('foo'); + strictEqual(eventTarget.removeAllListeners('foo'), eventTarget); strictEqual(eventTarget.listenerCount('foo'), 0); strictEqual(eventTarget.listenerCount('bar'), 1); deepStrictEqual(eventTarget.eventNames(), ['bar']); - eventTarget.removeAllListeners(); + strictEqual(eventTarget.removeAllListeners(), eventTarget); strictEqual(eventTarget.listenerCount('foo'), 0); strictEqual(eventTarget.listenerCount('bar'), 0); deepStrictEqual(eventTarget.eventNames(), []);