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 <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
  • Loading branch information
lpinca authored and targos committed Nov 3, 2020
1 parent 8074f69 commit 438d9de
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 @@ -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.
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 @@ -476,6 +476,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 438d9de

Please sign in to comment.