From 438d9debfd935cc53c0bc3a377eb1346ace7eddd 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 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: https://github.com/nodejs/node/issues/35762 PR-URL: https://github.com/nodejs/node/pull/35805 Reviewed-By: Michaƫl Zasso Reviewed-By: Benjamin Gruenbaum Reviewed-By: Rich Trott Reviewed-By: Anna Henningsen --- 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(), []);