Skip to content

Commit

Permalink
events: make eventTarget.removeAllListeners() return this
Browse files Browse the repository at this point in the history
Fixes: #35762

PR-URL: #35805
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
lpinca authored and BethGriggs committed Dec 15, 2020
1 parent 2984974 commit 8ef4557
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 2 additions & 0 deletions doc/api/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,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.
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,8 @@ class NodeEventTarget extends EventTarget {
} else {
this[kEvents].clear();
}

return this;
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-nodeeventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(), []);
Expand Down

0 comments on commit 8ef4557

Please sign in to comment.