diff options
author | simon-id <simon.id@protonmail.com> | 2021-10-19 12:56:42 +0300 |
---|---|---|
committer | Richard Lau <rlau@redhat.com> | 2022-01-25 20:49:24 +0300 |
commit | 625be7585dba1f6f8ae8fce484c641a019286696 (patch) | |
tree | dca30e7e6b804a9c44be512b788607aab4b9b1e3 | |
parent | 5389b8ab05229029062cae55bdba8f7f6204cbcb (diff) |
lib: add return value for DC channel.unsubscribe
PR-URL: https://github.com/nodejs/node/pull/40433
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Bryan English <bryan@bryanenglish.com>
Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
-rw-r--r-- | doc/api/diagnostics_channel.md | 10 | ||||
-rw-r--r-- | lib/diagnostics_channel.js | 16 | ||||
-rw-r--r-- | test/parallel/test-diagnostics-channel-object-channel-pub-sub.js | 5 |
3 files changed, 23 insertions, 8 deletions
diff --git a/doc/api/diagnostics_channel.md b/doc/api/diagnostics_channel.md index 16b47db3bf6..90a3b1fb6ba 100644 --- a/doc/api/diagnostics_channel.md +++ b/doc/api/diagnostics_channel.md @@ -156,7 +156,17 @@ channel.subscribe((message, name) => { #### `channel.unsubscribe(onMessage)` +<!-- YAML +added: + - 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 c29c9ff0052..7860e893449 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 cbc5b4d2e9a..9498419b806 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' }); |