Skip to content

Commit 7f52e7c

Browse files
committed
revert addCommand/addScript changes to multi-commands
addCommand needs to be there for sendCommand like ability within a multi. If its there, it might as well be used by createCommand() et al, to avoid repeating code. addScript is there (even though only used once), but now made private to keep the logic for bookkeeping near each other.
1 parent c1d69ae commit 7f52e7c

File tree

3 files changed

+126
-59
lines changed

3 files changed

+126
-59
lines changed

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

+22-15
Original file line numberDiff line numberDiff line change
@@ -91,59 +91,53 @@ export default class RedisClientMultiCommand<REPLIES = []> {
9191
static #createCommand(command: Command, resp: RespVersions) {
9292
const transformReply = getTransformReply(command, resp);
9393

94-
return function (this: RedisClientMultiCommand, ...args: Array<unknown>): RedisClientMultiCommand {
94+
return function (this: RedisClientMultiCommand, ...args: Array<unknown>) {
9595
const parser = new BasicCommandParser();
9696
command.parseCommand(parser, ...args);
9797

9898
const redisArgs: CommandArguments = parser.redisArgs;
9999
redisArgs.preserve = parser.preserve;
100100

101-
this.#multi.addCommand(
101+
return this.addCommand(
102102
redisArgs,
103103
transformReply
104104
);
105-
106-
return this;
107105
};
108106
}
109107

110108
static #createModuleCommand(command: Command, resp: RespVersions) {
111109
const transformReply = getTransformReply(command, resp);
112110

113-
return function (this: { _self: RedisClientMultiCommand }, ...args: Array<unknown>): RedisClientMultiCommand {
111+
return function (this: { _self: RedisClientMultiCommand }, ...args: Array<unknown>) {
114112
const parser = new BasicCommandParser();
115113
command.parseCommand(parser, ...args);
116114

117115
const redisArgs: CommandArguments = parser.redisArgs;
118116
redisArgs.preserve = parser.preserve;
119117

120-
this._self.#multi.addCommand(
118+
return this._self.addCommand(
121119
redisArgs,
122120
transformReply
123121
);
124-
125-
return this._self;
126122
};
127123
}
128124

129125
static #createFunctionCommand(name: string, fn: RedisFunction, resp: RespVersions) {
130126
const prefix = functionArgumentsPrefix(name, fn);
131127
const transformReply = getTransformReply(fn, resp);
132128

133-
return function (this: { _self: RedisClientMultiCommand }, ...args: Array<unknown>): RedisClientMultiCommand {
129+
return function (this: { _self: RedisClientMultiCommand }, ...args: Array<unknown>) {
134130
const parser = new BasicCommandParser();
135131
parser.push(...prefix);
136132
fn.parseCommand(parser, ...args);
137133

138134
const redisArgs: CommandArguments = parser.redisArgs;
139135
redisArgs.preserve = parser.preserve;
140136

141-
this._self.#multi.addCommand(
137+
return this._self.addCommand(
142138
redisArgs,
143139
transformReply
144140
);
145-
146-
return this._self;
147141
};
148142
}
149143

@@ -157,13 +151,11 @@ export default class RedisClientMultiCommand<REPLIES = []> {
157151
const redisArgs: CommandArguments = parser.redisArgs;
158152
redisArgs.preserve = parser.preserve;
159153

160-
this.#multi.addScript(
154+
return this.#addScript(
161155
script,
162156
redisArgs,
163157
transformReply
164158
);
165-
166-
return this;
167159
};
168160
}
169161

@@ -204,6 +196,21 @@ export default class RedisClientMultiCommand<REPLIES = []> {
204196

205197
select = this.SELECT;
206198

199+
addCommand(args: CommandArguments, transformReply?: TransformReply) {
200+
this.#multi.addCommand(args, transformReply);
201+
return this;
202+
}
203+
204+
#addScript(
205+
script: RedisScript,
206+
args: CommandArguments,
207+
transformReply?: TransformReply
208+
) {
209+
this.#multi.addScript(script, args, transformReply);
210+
211+
return this;
212+
}
213+
207214
async exec<T extends MultiReply = MULTI_REPLY['GENERIC']>(execAsPipeline = false): Promise<MultiReplyType<T, REPLIES>> {
208215
if (execAsPipeline) return this.execAsPipeline<T>();
209216

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

+56-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import COMMANDS from '../commands';
22
import RedisMultiCommand, { MULTI_REPLY, MultiReply, MultiReplyType, RedisMultiQueuedCommand } from '../multi-command';
3-
import { ReplyWithTypeMapping, CommandReply, Command, CommandArguments, CommanderConfig, RedisFunctions, RedisModules, RedisScripts, RespVersions, RedisScript, RedisFunction, TypeMapping, RedisArgument } from '../RESP/types';
3+
import { ReplyWithTypeMapping, CommandReply, Command, CommandArguments, CommanderConfig, RedisFunctions, RedisModules, RedisScripts, RespVersions, TransformReply, RedisScript, RedisFunction, TypeMapping, RedisArgument } from '../RESP/types';
44
import { attachConfig, functionArgumentsPrefix, getTransformReply } from '../commander';
55
import { BasicCommandParser } from '../client/parser';
66
import { Tail } from '../commands/generic-transformers';
@@ -95,44 +95,48 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
9595
static #createCommand(command: Command, resp: RespVersions) {
9696
const transformReply = getTransformReply(command, resp);
9797

98-
return function (this: RedisClusterMultiCommand, ...args: Array<unknown>): RedisClusterMultiCommand {
98+
return function (this: RedisClusterMultiCommand, ...args: Array<unknown>) {
9999
const parser = new BasicCommandParser();
100100
command.parseCommand(parser, ...args);
101101

102102
const redisArgs: CommandArguments = parser.redisArgs;
103103
redisArgs.preserve = parser.preserve;
104104
const firstKey = parser.firstKey;
105-
106-
this.#setState(firstKey, command.IS_READ_ONLY);
107-
this.#multi.addCommand(redisArgs, transformReply);
108-
109-
return this;
105+
106+
return this.addCommand(
107+
firstKey,
108+
command.IS_READ_ONLY,
109+
redisArgs,
110+
transformReply
111+
);
110112
};
111113
}
112114

113115
static #createModuleCommand(command: Command, resp: RespVersions) {
114116
const transformReply = getTransformReply(command, resp);
115117

116-
return function (this: { _self: RedisClusterMultiCommand }, ...args: Array<unknown>): RedisClusterMultiCommand {
118+
return function (this: { _self: RedisClusterMultiCommand }, ...args: Array<unknown>) {
117119
const parser = new BasicCommandParser();
118120
command.parseCommand(parser, ...args);
119121

120122
const redisArgs: CommandArguments = parser.redisArgs;
121123
redisArgs.preserve = parser.preserve;
122124
const firstKey = parser.firstKey;
123125

124-
this._self.#setState(firstKey, command.IS_READ_ONLY);
125-
this._self.#multi.addCommand(redisArgs, transformReply);
126-
127-
return this._self;
126+
return this._self.addCommand(
127+
firstKey,
128+
command.IS_READ_ONLY,
129+
redisArgs,
130+
transformReply
131+
);
128132
};
129133
}
130134

131135
static #createFunctionCommand(name: string, fn: RedisFunction, resp: RespVersions) {
132136
const prefix = functionArgumentsPrefix(name, fn);
133137
const transformReply = getTransformReply(fn, resp);
134138

135-
return function (this: { _self: RedisClusterMultiCommand }, ...args: Array<unknown>): RedisClusterMultiCommand {
139+
return function (this: { _self: RedisClusterMultiCommand }, ...args: Array<unknown>) {
136140
const parser = new BasicCommandParser();
137141
parser.push(...prefix);
138142
fn.parseCommand(parser, ...args);
@@ -141,28 +145,33 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
141145
redisArgs.preserve = parser.preserve;
142146
const firstKey = parser.firstKey;
143147

144-
this._self.#setState(firstKey, fn.IS_READ_ONLY);
145-
this._self.#multi.addCommand(redisArgs, transformReply);
146-
147-
return this._self;
148+
return this._self.addCommand(
149+
firstKey,
150+
fn.IS_READ_ONLY,
151+
redisArgs,
152+
transformReply
153+
);
148154
};
149155
}
150156

151157
static #createScriptCommand(script: RedisScript, resp: RespVersions) {
152158
const transformReply = getTransformReply(script, resp);
153159

154-
return function (this: RedisClusterMultiCommand, ...args: Array<unknown>): RedisClusterMultiCommand {
160+
return function (this: RedisClusterMultiCommand, ...args: Array<unknown>) {
155161
const parser = new BasicCommandParser();
156162
script.parseCommand(parser, ...args);
157163

158164
const scriptArgs: CommandArguments = parser.redisArgs;
159165
scriptArgs.preserve = parser.preserve;
160166
const firstKey = parser.firstKey;
161167

162-
this.#setState(firstKey, script.IS_READ_ONLY);
163-
this.#multi.addScript(script, scriptArgs, transformReply);
164-
165-
return this;
168+
return this.#addScript(
169+
firstKey,
170+
script.IS_READ_ONLY,
171+
script,
172+
scriptArgs,
173+
transformReply
174+
);
166175
};
167176
}
168177

@@ -183,7 +192,7 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
183192
});
184193
}
185194

186-
readonly #multi: RedisMultiCommand;
195+
readonly #multi: RedisMultiCommand
187196

188197
readonly #executeMulti: ClusterMultiExecute;
189198
readonly #executePipeline: ClusterMultiExecute;
@@ -210,6 +219,30 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
210219
this.#isReadonly &&= isReadonly;
211220
}
212221

222+
addCommand(
223+
firstKey: RedisArgument | undefined,
224+
isReadonly: boolean | undefined,
225+
args: CommandArguments,
226+
transformReply?: TransformReply
227+
) {
228+
this.#setState(firstKey, isReadonly);
229+
this.#multi.addCommand(args, transformReply);
230+
return this;
231+
}
232+
233+
#addScript(
234+
firstKey: RedisArgument | undefined,
235+
isReadonly: boolean | undefined,
236+
script: RedisScript,
237+
args: CommandArguments,
238+
transformReply?: TransformReply
239+
) {
240+
this.#setState(firstKey, isReadonly);
241+
this.#multi.addScript(script, args, transformReply);
242+
243+
return this;
244+
}
245+
213246
async exec<T extends MultiReply = MULTI_REPLY['GENERIC']>(execAsPipeline = false) {
214247
if (execAsPipeline) return this.execAsPipeline<T>();
215248

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

+48-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import COMMANDS from '../commands';
22
import RedisMultiCommand, { MULTI_REPLY, MultiReply, MultiReplyType } from '../multi-command';
3-
import { ReplyWithTypeMapping, CommandReply, Command, CommandArguments, CommanderConfig, RedisFunctions, RedisModules, RedisScripts, RespVersions, RedisScript, RedisFunction, TypeMapping } from '../RESP/types';
3+
import { ReplyWithTypeMapping, CommandReply, Command, CommandArguments, CommanderConfig, RedisFunctions, RedisModules, RedisScripts, RespVersions, TransformReply, RedisScript, RedisFunction, TypeMapping } from '../RESP/types';
44
import { attachConfig, functionArgumentsPrefix, getTransformReply } from '../commander';
55
import { RedisSentinelType } from './types';
66
import { BasicCommandParser } from '../client/parser';
@@ -90,70 +90,75 @@ export default class RedisSentinelMultiCommand<REPLIES = []> {
9090
private static _createCommand(command: Command, resp: RespVersions) {
9191
const transformReply = getTransformReply(command, resp);
9292

93-
return function (this: RedisSentinelMultiCommand, ...args: Array<unknown>): RedisSentinelMultiCommand {
93+
return function (this: RedisSentinelMultiCommand, ...args: Array<unknown>) {
9494
const parser = new BasicCommandParser();
9595
command.parseCommand(parser, ...args);
9696

9797
const redisArgs: CommandArguments = parser.redisArgs;
9898
redisArgs.preserve = parser.preserve;
9999

100-
this.#setState(command.IS_READ_ONLY);
101-
this.#multi.addCommand(redisArgs, transformReply);
102-
103-
return this;
100+
return this.addCommand(
101+
command.IS_READ_ONLY,
102+
redisArgs,
103+
transformReply
104+
);
104105
};
105106
}
106107

107108
private static _createModuleCommand(command: Command, resp: RespVersions) {
108109
const transformReply = getTransformReply(command, resp);
109110

110-
return function (this: { _self: RedisSentinelMultiCommand }, ...args: Array<unknown>): RedisSentinelMultiCommand {
111+
return function (this: { _self: RedisSentinelMultiCommand }, ...args: Array<unknown>) {
111112
const parser = new BasicCommandParser();
112113
command.parseCommand(parser, ...args);
113114

114115
const redisArgs: CommandArguments = parser.redisArgs;
115116
redisArgs.preserve = parser.preserve;
116117

117-
this._self.#setState(command.IS_READ_ONLY);
118-
this._self.#multi.addCommand(redisArgs, transformReply);
119-
120-
return this._self;
118+
return this._self.addCommand(
119+
command.IS_READ_ONLY,
120+
redisArgs,
121+
transformReply
122+
);
121123
};
122124
}
123125

124126
private static _createFunctionCommand(name: string, fn: RedisFunction, resp: RespVersions) {
125127
const prefix = functionArgumentsPrefix(name, fn);
126128
const transformReply = getTransformReply(fn, resp);
127129

128-
return function (this: { _self: RedisSentinelMultiCommand }, ...args: Array<unknown>): RedisSentinelMultiCommand {
130+
return function (this: { _self: RedisSentinelMultiCommand }, ...args: Array<unknown>) {
129131
const parser = new BasicCommandParser();
130132
parser.push(...prefix);
131133
fn.parseCommand(parser, ...args);
132134

133135
const redisArgs: CommandArguments = parser.redisArgs;
134136
redisArgs.preserve = parser.preserve;
135137

136-
this._self.#setState(fn.IS_READ_ONLY);
137-
this._self.#multi.addCommand(redisArgs, transformReply);
138-
139-
return this._self;
138+
return this._self.addCommand(
139+
fn.IS_READ_ONLY,
140+
redisArgs,
141+
transformReply
142+
);
140143
};
141144
}
142145

143146
private static _createScriptCommand(script: RedisScript, resp: RespVersions) {
144147
const transformReply = getTransformReply(script, resp);
145148

146-
return function (this: RedisSentinelMultiCommand, ...args: Array<unknown>): RedisSentinelMultiCommand {
149+
return function (this: RedisSentinelMultiCommand, ...args: Array<unknown>) {
147150
const parser = new BasicCommandParser();
148151
script.parseCommand(parser, ...args);
149152

150153
const scriptArgs: CommandArguments = parser.redisArgs;
151154
scriptArgs.preserve = parser.preserve;
152155

153-
this.#setState(script.IS_READ_ONLY);
154-
this.#multi.addScript(script, scriptArgs, transformReply);
155-
156-
return this;
156+
return this.#addScript(
157+
script.IS_READ_ONLY,
158+
script,
159+
scriptArgs,
160+
transformReply
161+
);
157162
};
158163
}
159164

@@ -189,6 +194,28 @@ export default class RedisSentinelMultiCommand<REPLIES = []> {
189194
this.#isReadonly &&= isReadonly;
190195
}
191196

197+
addCommand(
198+
isReadonly: boolean | undefined,
199+
args: CommandArguments,
200+
transformReply?: TransformReply
201+
) {
202+
this.#setState(isReadonly);
203+
this.#multi.addCommand(args, transformReply);
204+
return this;
205+
}
206+
207+
#addScript(
208+
isReadonly: boolean | undefined,
209+
script: RedisScript,
210+
args: CommandArguments,
211+
transformReply?: TransformReply
212+
) {
213+
this.#setState(isReadonly);
214+
this.#multi.addScript(script, args, transformReply);
215+
216+
return this;
217+
}
218+
192219
async exec<T extends MultiReply = MULTI_REPLY['GENERIC']>(execAsPipeline = false) {
193220
if (execAsPipeline) return this.execAsPipeline<T>();
194221

0 commit comments

Comments
 (0)