From 594fe5502dd20326c60fe0b7842304a725d53d1b Mon Sep 17 00:00:00 2001 From: simon-id Date: Tue, 19 Oct 2021 11:56:42 +0200 Subject: [PATCH] lib: add return value for DC channel.unsubscribe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/40433 Reviewed-By: Vladimir de Turckheim Reviewed-By: Stephen Belanger Reviewed-By: Gerhard Stöbich Reviewed-By: Michael Dawson Reviewed-By: Bryan English Reviewed-By: Zijian Liu --- doc/api/diagnostics_channel.md | 5 +++++ lib/diagnostics_channel.js | 16 +++++++++------- ...diagnostics-channel-object-channel-pub-sub.js | 5 ++++- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/doc/api/diagnostics_channel.md b/doc/api/diagnostics_channel.md index 6c4ace3bbd615e..52a9ac9808414a 100644 --- a/doc/api/diagnostics_channel.md +++ b/doc/api/diagnostics_channel.md @@ -264,9 +264,14 @@ channel.subscribe((message, name) => { added: - v15.1.0 - v14.17.0 +changes: + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/40433 + description: Added return value. --> * `onMessage` {Function} The previous subscribed handler to remove +* Returns: {boolean} `true` if the handler was found, `false` otherwise. Remove a message handler previously registered to this channel with [`channel.subscribe(onMessage)`][]. diff --git a/lib/diagnostics_channel.js b/lib/diagnostics_channel.js index 1792980ec1b958..813cda5bb3e83a 100644 --- a/lib/diagnostics_channel.js +++ b/lib/diagnostics_channel.js @@ -32,15 +32,17 @@ class ActiveChannel { unsubscribe(subscription) { const index = ArrayPrototypeIndexOf(this._subscribers, subscription); - if (index >= 0) { - ArrayPrototypeSplice(this._subscribers, index, 1); + if (index === -1) return false; - // When there are no more active subscribers, restore to fast prototype. - if (!this._subscribers.length) { - // eslint-disable-next-line no-use-before-define - ObjectSetPrototypeOf(this, Channel.prototype); - } + ArrayPrototypeSplice(this._subscribers, index, 1); + + // When there are no more active subscribers, restore to fast prototype. + if (!this._subscribers.length) { + // eslint-disable-next-line no-use-before-define + ObjectSetPrototypeOf(this, Channel.prototype); } + + return true; } get hasSubscribers() { diff --git a/test/parallel/test-diagnostics-channel-object-channel-pub-sub.js b/test/parallel/test-diagnostics-channel-object-channel-pub-sub.js index cbc5b4d2e9a953..9498419b806ca8 100644 --- a/test/parallel/test-diagnostics-channel-object-channel-pub-sub.js +++ b/test/parallel/test-diagnostics-channel-object-channel-pub-sub.js @@ -35,9 +35,12 @@ assert.ok(channel instanceof Channel); channel.publish(input); // Should not publish after subscriber is unsubscribed -channel.unsubscribe(subscriber); +assert.ok(channel.unsubscribe(subscriber)); assert.ok(!channel.hasSubscribers); +// unsubscribe() should return false when subscriber is not found +assert.ok(!channel.unsubscribe(subscriber)); + assert.throws(() => { channel.subscribe(null); }, { code: 'ERR_INVALID_ARG_TYPE' });