diff --git a/lib/client.spec.ts b/lib/client.spec.ts index 9f18e184c8..7f1a534352 100644 --- a/lib/client.spec.ts +++ b/lib/client.spec.ts @@ -516,6 +516,9 @@ describe('Client', () => { assert.ok(channelListener1.calledOnce); assert.ok(channelListener2.calledTwice); assert.ok(patternListener.calledThrice); + + // should be able to send commands when unsubsribed from all channels (see #1652) + await assert.doesNotReject(subscriber.ping()); } finally { await subscriber.disconnect(); } diff --git a/lib/commands-queue.ts b/lib/commands-queue.ts index 27c8396552..ef87184193 100644 --- a/lib/commands-queue.ts +++ b/lib/commands-queue.ts @@ -171,8 +171,9 @@ export default class RedisCommandsQueue { unsubscribe(command: PubSubUnsubscribeCommands, channels?: string | Array, listener?: PubSubListener): Promise { const listeners = command === PubSubUnsubscribeCommands.UNSUBSCRIBE ? this.#pubSubListeners.channels : this.#pubSubListeners.patterns; if (!channels) { + const size = listeners.size; listeners.clear(); - return this.#pushPubSubCommand(command); + return this.#pushPubSubCommand(command, size); } const channelsToUnsubscribe = []; @@ -199,22 +200,18 @@ export default class RedisCommandsQueue { return this.#pushPubSubCommand(command, channelsToUnsubscribe); } - #pushPubSubCommand(command: PubSubSubscribeCommands | PubSubUnsubscribeCommands, channels?: Array): Promise { + #pushPubSubCommand(command: PubSubSubscribeCommands | PubSubUnsubscribeCommands, channels: number | Array): Promise { return new Promise((resolve, reject) => { const isSubscribe = command === PubSubSubscribeCommands.SUBSCRIBE || command === PubSubSubscribeCommands.PSUBSCRIBE, inProgressKey = isSubscribe ? 'subscribing' : 'unsubscribing', commandArgs: Array = [command]; + let channelsCounter: number; - if (channels?.length) { + if (typeof channels === 'number') { // unsubscribe only + channelsCounter = channels; + } else { commandArgs.push(...channels); channelsCounter = channels.length; - } else { - // unsubscribe only - channelsCounter = ( - command[0] === 'P' ? - this.#pubSubListeners.patterns : - this.#pubSubListeners.channels - ).size; } this.#pubSubState[inProgressKey] += channelsCounter;