diff --git a/index.js b/index.js index 1d3e035..c93caef 100644 --- a/index.js +++ b/index.js @@ -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() { @@ -104,6 +106,10 @@ class Channel { this.subscribe(subscription); } + unsubscribe() { + return false; + } + get hasSubscribers() { return false; } diff --git a/test/core/test-diagnostics-channel-object-channel-pub-sub.js b/test/core/test-diagnostics-channel-object-channel-pub-sub.js index cbc5b4d..9498419 100644 --- a/test/core/test-diagnostics-channel-object-channel-pub-sub.js +++ b/test/core/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' });