Skip to content

Commit 0cd6915

Browse files
committed
fix client.reset
1 parent baef348 commit 0cd6915

File tree

3 files changed

+70
-57
lines changed

3 files changed

+70
-57
lines changed

packages/client/lib/client/commands-queue.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SinglyLinkedList, DoublyLinkedNode, DoublyLinkedList } from './linked-list';
22
import encodeCommand from '../RESP/encoder';
33
import { Decoder, PUSH_TYPE_MAPPING, RESP_TYPES } from '../RESP/decoder';
4-
import { CommandArguments, TypeMapping, ReplyUnion, RespVersions, SimpleStringReply, ReplyWithTypeMapping } from '../RESP/types';
4+
import { CommandArguments, TypeMapping, ReplyUnion, RespVersions } from '../RESP/types';
55
import { ChannelListeners, PubSub, PubSubCommand, PubSubListener, PubSubType, PubSubTypeListeners } from './pub-sub';
66
import { AbortError, ErrorReply } from '../errors';
77
import { MonitorCallback } from '.';
@@ -154,11 +154,11 @@ export default class RedisCommandsQueue {
154154
});
155155
}
156156

157-
#addPubSubCommand(command: PubSubCommand, asap = false) {
157+
#addPubSubCommand(command: PubSubCommand, asap = false, chainId?: symbol) {
158158
return new Promise<void>((resolve, reject) => {
159159
this.#toWrite.add({
160160
args: command.args,
161-
chainId: undefined,
161+
chainId,
162162
abort: undefined,
163163
resolve() {
164164
command.resolve();
@@ -237,13 +237,13 @@ export default class RedisCommandsQueue {
237237
return this.#addPubSubCommand(command);
238238
}
239239

240-
resubscribe() {
240+
resubscribe(chainId?: symbol) {
241241
const commands = this.#pubSub.resubscribe();
242242
if (!commands.length) return;
243243

244244
this.#setupPubSubHandler();
245245
return Promise.all(
246-
commands.map(command => this.#addPubSubCommand(command, true))
246+
commands.map(command => this.#addPubSubCommand(command, true, chainId))
247247
);
248248
}
249249

@@ -271,11 +271,12 @@ export default class RedisCommandsQueue {
271271
return this.#pubSub.getTypeListeners(type);
272272
}
273273

274-
monitor(callback: MonitorCallback, typeMapping: TypeMapping = {}, asap = false) {
274+
monitor(callback: MonitorCallback, options?: CommandOptions) {
275275
return new Promise<void>((resolve, reject) => {
276+
const typeMapping = options?.typeMapping ?? {};
276277
this.#toWrite.add({
277278
args: ['MONITOR'],
278-
chainId: undefined,
279+
chainId: options?.chainId,
279280
abort: undefined,
280281
// using `resolve` instead of using `.then`/`await` to make sure it'll be called before processing the next reply
281282
resolve: () => {
@@ -295,7 +296,7 @@ export default class RedisCommandsQueue {
295296
reject,
296297
channelsCounter: undefined,
297298
typeMapping
298-
}, asap);
299+
}, options?.asap);
299300
});
300301
}
301302

@@ -306,7 +307,7 @@ export default class RedisCommandsQueue {
306307

307308
#resetFallbackOnReply?: Decoder['onReply'];
308309

309-
async reset<T extends TypeMapping>(typeMapping?: T) {
310+
async reset<T extends TypeMapping>(chainId: symbol, typeMapping?: T) {
310311
return new Promise((resolve, reject) => {
311312
// overriding onReply to handle `RESET` while in `MONITOR` or PubSub mode
312313
this.#resetFallbackOnReply = this.decoder.onReply;
@@ -328,7 +329,7 @@ export default class RedisCommandsQueue {
328329

329330
this.#toWrite.push({
330331
args: ['RESET'],
331-
chainId: undefined,
332+
chainId,
332333
abort: undefined,
333334
resolve,
334335
reject,

packages/client/lib/client/index.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,9 @@ describe('Client', () => {
732732
skipMe: true
733733
})
734734
]);
735+
736+
await once(duplicate, 'ready');
737+
735738
await Promise.all([
736739
waitTillBeenCalled(listener),
737740
client.ping()

packages/client/lib/client/index.ts

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -332,24 +332,8 @@ export default class RedisClient<
332332
);
333333
}
334334

335-
#handshake(asap = false, promises: Array<Promise<unknown>> = []) {
336-
if (this.#selectedDB !== 0) {
337-
promises.push(
338-
this.#queue.addCommand(
339-
['SELECT', this.#selectedDB.toString()],
340-
{ asap }
341-
)
342-
);
343-
}
344-
345-
if (this.#options?.readonly) {
346-
promises.push(
347-
this.#queue.addCommand(
348-
COMMANDS.READONLY.transformArguments(),
349-
{ asap }
350-
)
351-
);
352-
}
335+
#handshake(selectedDB: number) {
336+
const commands = [];
353337

354338
if (this.#options?.RESP) {
355339
const hello: HelloOptions = {};
@@ -365,43 +349,45 @@ export default class RedisClient<
365349
hello.SETNAME = this.#options.name;
366350
}
367351

368-
promises.push(
369-
this.#queue.addCommand(
370-
HELLO.transformArguments(this.#options.RESP, hello),
371-
{ asap }
372-
)
352+
commands.push(
353+
HELLO.transformArguments(this.#options.RESP, hello)
373354
);
374355
} else {
375-
if (this.#options?.name) {
376-
promises.push(
377-
this.#queue.addCommand(
378-
COMMANDS.CLIENT_SETNAME.transformArguments(this.#options.name),
379-
{ asap }
380-
)
356+
if (this.#options?.username || this.#options?.password) {
357+
commands.push(
358+
COMMANDS.AUTH.transformArguments({
359+
username: this.#options.username,
360+
password: this.#options.password ?? ''
361+
})
381362
);
382363
}
383364

384-
if (this.#options?.username || this.#options?.password) {
385-
promises.push(
386-
this.#queue.addCommand(
387-
COMMANDS.AUTH.transformArguments({
388-
username: this.#options.username,
389-
password: this.#options.password ?? ''
390-
}),
391-
{ asap }
392-
)
365+
if (this.#options?.name) {
366+
commands.push(
367+
COMMANDS.CLIENT_SETNAME.transformArguments(this.#options.name)
393368
);
394369
}
395370
}
396371

397-
return promises;
372+
if (selectedDB !== 0) {
373+
commands.push(['SELECT', this.#selectedDB.toString()]);
374+
}
375+
376+
if (this.#options?.readonly) {
377+
commands.push(
378+
COMMANDS.READONLY.transformArguments()
379+
);
380+
}
381+
382+
return commands;
398383
}
399384

400385
#initiateSocket(): RedisSocket {
401386
const socketInitiator = () => {
402-
const promises: Array<Promise<unknown>> = [];
387+
const promises = [],
388+
chainId = Symbol('Socket Initiator');
403389

404-
const resubscribePromise = this.#queue.resubscribe();
390+
const resubscribePromise = this.#queue.resubscribe(chainId);
405391
if (resubscribePromise) {
406392
promises.push(resubscribePromise);
407393
}
@@ -410,13 +396,24 @@ export default class RedisClient<
410396
promises.push(
411397
this.#queue.monitor(
412398
this.#monitorCallback,
413-
this._commandOptions?.typeMapping,
414-
true
399+
{
400+
typeMapping: this._commandOptions?.typeMapping,
401+
chainId,
402+
asap: true
403+
}
415404
)
416405
);
417406
}
418407

419-
this.#handshake(true, promises);
408+
const commands = this.#handshake(this.#selectedDB);
409+
for (let i = commands.length - 1; i >= 0; --i) {
410+
promises.push(
411+
this.#queue.addCommand(commands[i], {
412+
chainId,
413+
asap: true
414+
})
415+
);
416+
}
420417

421418
if (promises.length) {
422419
this.#write();
@@ -885,7 +882,9 @@ export default class RedisClient<
885882
}
886883

887884
async MONITOR(callback: MonitorCallback<TYPE_MAPPING>) {
888-
const promise = this._self.#queue.monitor(callback, this._commandOptions?.typeMapping);
885+
const promise = this._self.#queue.monitor(callback, {
886+
typeMapping: this._commandOptions?.typeMapping
887+
});
889888
this._self.#scheduleWrite();
890889
await promise;
891890
this._self.#monitorCallback = callback;
@@ -897,10 +896,20 @@ export default class RedisClient<
897896
* Reset the client to its default state (i.e. stop PubSub, stop monitoring, select default DB, etc.)
898897
*/
899898
async reset() {
900-
const promises = [this._self.#queue.reset()];
901-
this._self.#handshake(false, promises);
899+
const chainId = Symbol('Reset Chain'),
900+
promises = [this._self.#queue.reset(chainId)],
901+
selectedDB = this._self.#options?.database ?? 0;
902+
for (const command of this._self.#handshake(selectedDB)) {
903+
promises.push(
904+
this._self.#queue.addCommand(command, {
905+
chainId
906+
})
907+
);
908+
}
902909
this._self.#scheduleWrite();
903910
await Promise.all(promises);
911+
this._self.#selectedDB = selectedDB;
912+
this._self.#monitorCallback = undefined;
904913
}
905914

906915
/**

0 commit comments

Comments
 (0)