Skip to content

Commit e70c12b

Browse files
committed
refator(client): Extract socket event listener setup into helper method
1 parent 20ab341 commit e70c12b

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

packages/client/lib/client/index.ts

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,35 @@ export default class RedisClient<
694694
return commands;
695695
}
696696

697+
#attachListeners(socket: RedisSocket) {
698+
socket.on('data', chunk => {
699+
try {
700+
this.#queue.decoder.write(chunk);
701+
} catch (err) {
702+
this.#queue.resetDecoder();
703+
this.emit('error', err);
704+
}
705+
})
706+
.on('error', err => {
707+
this.emit('error', err);
708+
this.#clientSideCache?.onError();
709+
if (this.#socket.isOpen && !this.#options?.disableOfflineQueue) {
710+
this.#queue.flushWaitingForReply(err);
711+
} else {
712+
this.#queue.flushAll(err);
713+
}
714+
})
715+
.on('connect', () => this.emit('connect'))
716+
.on('ready', () => {
717+
this.emit('ready');
718+
this.#setPingTimer();
719+
this.#maybeScheduleWrite();
720+
})
721+
.on('reconnecting', () => this.emit('reconnecting'))
722+
.on('drain', () => this.#maybeScheduleWrite())
723+
.on('end', () => this.emit('end'));
724+
}
725+
697726
#initiateSocket(): RedisSocket {
698727
const socketInitiator = async () => {
699728
const promises = [],
@@ -725,33 +754,9 @@ export default class RedisClient<
725754
}
726755
};
727756

728-
return new RedisSocket(socketInitiator, this.#options?.socket)
729-
.on('data', chunk => {
730-
try {
731-
this.#queue.decoder.write(chunk);
732-
} catch (err) {
733-
this.#queue.resetDecoder();
734-
this.emit('error', err);
735-
}
736-
})
737-
.on('error', err => {
738-
this.emit('error', err);
739-
this.#clientSideCache?.onError();
740-
if (this.#socket.isOpen && !this.#options?.disableOfflineQueue) {
741-
this.#queue.flushWaitingForReply(err);
742-
} else {
743-
this.#queue.flushAll(err);
744-
}
745-
})
746-
.on('connect', () => this.emit('connect'))
747-
.on('ready', () => {
748-
this.emit('ready');
749-
this.#setPingTimer();
750-
this.#maybeScheduleWrite();
751-
})
752-
.on('reconnecting', () => this.emit('reconnecting'))
753-
.on('drain', () => this.#maybeScheduleWrite())
754-
.on('end', () => this.emit('end'));
757+
const socket = new RedisSocket(socketInitiator, this.#options?.socket);
758+
this.#attachListeners(socket);
759+
return socket;
755760
}
756761

757762
#pingTimer?: NodeJS.Timeout;

0 commit comments

Comments
 (0)