Skip to content

Commit

Permalink
mirror modifications from node core pr #40433 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-id committed Oct 19, 2021
1 parent 800921c commit 3bc8315
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
20 changes: 13 additions & 7 deletions index.js
Expand Up @@ -57,15 +57,17 @@ class ActiveChannel {

unsubscribe(subscription) {
const index = this._subscribers.indexOf(subscription);
if (index >= 0) {
this._subscribers.splice(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
Object.setPrototypeOf(this, Channel.prototype);
}
this._subscribers.splice(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
Object.setPrototypeOf(this, Channel.prototype);
}

return true;
}

get hasSubscribers() {
Expand Down Expand Up @@ -104,6 +106,10 @@ class Channel {
this.subscribe(subscription);
}

unsubscribe() {
return false;
}

get hasSubscribers() {
return false;
}
Expand Down
5 changes: 4 additions & 1 deletion test/core/test-diagnostics-channel-object-channel-pub-sub.js
Expand Up @@ -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' });

0 comments on commit 3bc8315

Please sign in to comment.