mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
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>
This commit is contained in:
1 parent
0f78d2600b
commit
d0670e7559
3 files changed
+18
-8
No files matched your search
@@ -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)`][].
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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' });
|
||||
Reference in new issue
Block a user