Skip to content

Commit 4c08d83

Browse files
committed
small tweak to multi type mapping handling
1 parent 5bc06e0 commit 4c08d83

File tree

4 files changed

+37
-39
lines changed

4 files changed

+37
-39
lines changed

packages/client/lib/client/multi-command.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,16 @@ export default class RedisClientMultiCommand<REPLIES = []> {
188188
});
189189
}
190190

191-
readonly #multi = new RedisMultiCommand();
191+
readonly #multi: RedisMultiCommand
192192
readonly #executeMulti: ExecuteMulti;
193193
readonly #executePipeline: ExecuteMulti;
194-
readonly #typeMapping?: TypeMapping;
195194

196195
#selectedDB?: number;
197196

198197
constructor(executeMulti: ExecuteMulti, executePipeline: ExecuteMulti, typeMapping?: TypeMapping) {
198+
this.#multi = new RedisMultiCommand(typeMapping);
199199
this.#executeMulti = executeMulti;
200200
this.#executePipeline = executePipeline;
201-
this.#typeMapping = typeMapping;
202201
}
203202

204203
SELECT(db: number, transformReply?: TransformReply): this {
@@ -218,8 +217,7 @@ export default class RedisClientMultiCommand<REPLIES = []> {
218217
if (execAsPipeline) return this.execAsPipeline<T>();
219218

220219
return this.#multi.transformReplies(
221-
await this.#executeMulti(this.#multi.queue, this.#selectedDB),
222-
this.#typeMapping
220+
await this.#executeMulti(this.#multi.queue, this.#selectedDB)
223221
) as MultiReplyType<T, REPLIES>;
224222
}
225223

@@ -233,8 +231,7 @@ export default class RedisClientMultiCommand<REPLIES = []> {
233231
if (this.#multi.queue.length === 0) return [] as MultiReplyType<T, REPLIES>;
234232

235233
return this.#multi.transformReplies(
236-
await this.#executePipeline(this.#multi.queue, this.#selectedDB),
237-
this.#typeMapping
234+
await this.#executePipeline(this.#multi.queue, this.#selectedDB)
238235
) as MultiReplyType<T, REPLIES>;
239236
}
240237

packages/client/lib/cluster/multi-command.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,23 +206,23 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
206206
});
207207
}
208208

209-
readonly #multi = new RedisMultiCommand();
209+
readonly #multi: RedisMultiCommand
210+
210211
readonly #executeMulti: ClusterMultiExecute;
211212
readonly #executePipeline: ClusterMultiExecute;
212213
#firstKey: RedisArgument | undefined;
213214
#isReadonly: boolean | undefined = true;
214-
readonly #typeMapping?: TypeMapping;
215215

216216
constructor(
217217
executeMulti: ClusterMultiExecute,
218218
executePipeline: ClusterMultiExecute,
219219
routing: RedisArgument | undefined,
220220
typeMapping?: TypeMapping
221221
) {
222+
this.#multi = new RedisMultiCommand(typeMapping);
222223
this.#executeMulti = executeMulti;
223224
this.#executePipeline = executePipeline;
224225
this.#firstKey = routing;
225-
this.#typeMapping = typeMapping;
226226
}
227227

228228
#setState(
@@ -252,8 +252,7 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
252252
this.#firstKey,
253253
this.#isReadonly,
254254
this.#multi.queue
255-
),
256-
this.#typeMapping
255+
)
257256
) as MultiReplyType<T, REPLIES>;
258257
}
259258

@@ -271,8 +270,7 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
271270
this.#firstKey,
272271
this.#isReadonly,
273272
this.#multi.queue
274-
),
275-
this.#typeMapping
273+
)
276274
) as MultiReplyType<T, REPLIES>;
277275
}
278276

packages/client/lib/multi-command.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ export interface RedisMultiQueuedCommand {
1616
}
1717

1818
export default class RedisMultiCommand {
19+
readonly typeMapping?: TypeMapping;
20+
21+
constructor(typeMapping?: TypeMapping) {
22+
this.typeMapping = typeMapping;
23+
}
24+
1925
readonly queue: Array<RedisMultiQueuedCommand> = [];
2026

2127
readonly scriptsInUse = new Set<string>();
@@ -46,7 +52,7 @@ export default class RedisMultiCommand {
4652
this.addCommand(redisArgs, transformReply);
4753
}
4854

49-
transformReplies(rawReplies: Array<unknown>, typeMapping?: TypeMapping): Array<unknown> {
55+
transformReplies(rawReplies: Array<unknown>): Array<unknown> {
5056
const errorIndexes: Array<number> = [],
5157
replies = rawReplies.map((reply, i) => {
5258
if (reply instanceof ErrorReply) {
@@ -55,7 +61,7 @@ export default class RedisMultiCommand {
5561
}
5662

5763
const { transformReply, args } = this.queue[i];
58-
return transformReply ? transformReply(reply, args.preserve, typeMapping) : reply;
64+
return transformReply ? transformReply(reply, args.preserve, this.typeMapping) : reply;
5965
});
6066

6167
if (errorIndexes.length) throw new MultiErrorReply(replies, errorIndexes);

packages/client/lib/sentinel/multi-commands.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export default class RedisSentinelMultiCommand<REPLIES = []> {
162162
this._setState(
163163
script.IS_READ_ONLY
164164
);
165-
this._multi.addScript(
165+
this.#multi.addScript(
166166
script,
167167
scriptArgs,
168168
transformReply
@@ -189,20 +189,19 @@ export default class RedisSentinelMultiCommand<REPLIES = []> {
189189
});
190190
}
191191

192-
private readonly _multi = new RedisMultiCommand();
193-
private readonly _sentinel: RedisSentinelType
194-
private _isReadonly: boolean | undefined = true;
195-
private readonly _typeMapping?: TypeMapping;
192+
readonly #multi = new RedisMultiCommand();
193+
readonly #sentinel: RedisSentinelType
194+
#isReadonly: boolean | undefined = true;
196195

197196
constructor(sentinel: RedisSentinelType, typeMapping: TypeMapping) {
198-
this._sentinel = sentinel;
199-
this._typeMapping = typeMapping;
197+
this.#multi = new RedisMultiCommand(typeMapping);
198+
this.#sentinel = sentinel;
200199
}
201200

202201
private _setState(
203202
isReadonly: boolean | undefined,
204203
) {
205-
this._isReadonly &&= isReadonly;
204+
this.#isReadonly &&= isReadonly;
206205
}
207206

208207
addCommand(
@@ -211,19 +210,18 @@ export default class RedisSentinelMultiCommand<REPLIES = []> {
211210
transformReply?: TransformReply
212211
) {
213212
this._setState(isReadonly);
214-
this._multi.addCommand(args, transformReply);
213+
this.#multi.addCommand(args, transformReply);
215214
return this;
216215
}
217216

218217
async exec<T extends MultiReply = MULTI_REPLY['GENERIC']>(execAsPipeline = false) {
219218
if (execAsPipeline) return this.execAsPipeline<T>();
220219

221-
return this._multi.transformReplies(
222-
await this._sentinel._executeMulti(
223-
this._isReadonly,
224-
this._multi.queue
225-
),
226-
this._typeMapping
220+
return this.#multi.transformReplies(
221+
await this.#sentinel._executeMulti(
222+
this.#isReadonly,
223+
this.#multi.queue
224+
)
227225
) as MultiReplyType<T, REPLIES>;
228226
}
229227

@@ -234,14 +232,13 @@ export default class RedisSentinelMultiCommand<REPLIES = []> {
234232
}
235233

236234
async execAsPipeline<T extends MultiReply = MULTI_REPLY['GENERIC']>() {
237-
if (this._multi.queue.length === 0) return [] as MultiReplyType<T, REPLIES>;
238-
239-
return this._multi.transformReplies(
240-
await this._sentinel._executePipeline(
241-
this._isReadonly,
242-
this._multi.queue
243-
),
244-
this._typeMapping
235+
if (this.#multi.queue.length === 0) return [] as MultiReplyType<T, REPLIES>;
236+
237+
return this.#multi.transformReplies(
238+
await this.#sentinel._executePipeline(
239+
this.#isReadonly,
240+
this.#multi.queue
241+
)
245242
) as MultiReplyType<T, REPLIES>;
246243
}
247244

0 commit comments

Comments
 (0)