Skip to content

Commit aa61551

Browse files
simon-idtargos
authored andcommitted
lib: add return value for DC channel.unsubscribe
PR-URL: #40433 Reviewed-By: Vladimir de Turckheim <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Bryan English <[email protected]> Reviewed-By: Zijian Liu <[email protected]>
1 parent e4c7406 commit aa61551

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

doc/api/diagnostics_channel.md

+5
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,14 @@ channel.subscribe((message, name) => {
264264
added:
265265
- v15.1.0
266266
- v14.17.0
267+
changes:
268+
- version: REPLACEME
269+
pr-url: https://github.com/nodejs/node/pull/40433
270+
description: Added return value.
267271
-->
268272

269273
* `onMessage` {Function} The previous subscribed handler to remove
274+
* Returns: {boolean} `true` if the handler was found, `false` otherwise.
270275

271276
Remove a message handler previously registered to this channel with
272277
[`channel.subscribe(onMessage)`][].

lib/diagnostics_channel.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,17 @@ class ActiveChannel {
3232

3333
unsubscribe(subscription) {
3434
const index = ArrayPrototypeIndexOf(this._subscribers, subscription);
35-
if (index >= 0) {
36-
ArrayPrototypeSplice(this._subscribers, index, 1);
35+
if (index === -1) return false;
3736

38-
// When there are no more active subscribers, restore to fast prototype.
39-
if (!this._subscribers.length) {
40-
// eslint-disable-next-line no-use-before-define
41-
ObjectSetPrototypeOf(this, Channel.prototype);
42-
}
37+
ArrayPrototypeSplice(this._subscribers, index, 1);
38+
39+
// When there are no more active subscribers, restore to fast prototype.
40+
if (!this._subscribers.length) {
41+
// eslint-disable-next-line no-use-before-define
42+
ObjectSetPrototypeOf(this, Channel.prototype);
4343
}
44+
45+
return true;
4446
}
4547

4648
get hasSubscribers() {

test/parallel/test-diagnostics-channel-object-channel-pub-sub.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ assert.ok(channel instanceof Channel);
3535
channel.publish(input);
3636

3737
// Should not publish after subscriber is unsubscribed
38-
channel.unsubscribe(subscriber);
38+
assert.ok(channel.unsubscribe(subscriber));
3939
assert.ok(!channel.hasSubscribers);
4040

41+
// unsubscribe() should return false when subscriber is not found
42+
assert.ok(!channel.unsubscribe(subscriber));
43+
4144
assert.throws(() => {
4245
channel.subscribe(null);
4346
}, { code: 'ERR_INVALID_ARG_TYPE' });

0 commit comments

Comments
 (0)