Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mirror modifications from node core PR #40433 #3

Merged
merged 1 commit into from Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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' });