1
1
import COMMANDS from '../commands' ;
2
- import RedisMultiCommand , { RedisMultiQueuedCommand } from '../multi-command' ;
2
+ import RedisMultiCommand , { MULTI_REPLY , MultiReply , MultiReplyType } from '../multi-command' ;
3
3
import { ReplyWithFlags , CommandReply , Command , CommandArguments , CommanderConfig , RedisFunctions , RedisModules , RedisScripts , RespVersions , TransformReply , RedisScript , RedisFunction , Flags , ReplyUnion } from '../RESP/types' ;
4
4
import { attachConfig , functionArgumentsPrefix , getTransformReply } from '../commander' ;
5
5
import { RedisClientType } from '.' ;
@@ -84,64 +84,50 @@ export type RedisClientMultiCommandType<
84
84
WithScripts < REPLIES , M , F , S , RESP , FLAGS >
85
85
) ;
86
86
87
- type MULTI_REPLY = {
88
- GENERIC : 'generic' ;
89
- TYPED : 'typed' ;
90
- } ;
91
-
92
- type MultiReply = MULTI_REPLY [ keyof MULTI_REPLY ] ;
93
-
94
- type ReplyType < T extends MultiReply , REPLIES > = T extends MULTI_REPLY [ 'TYPED' ] ? REPLIES : Array < unknown > ;
95
-
96
- export type RedisClientMultiExecutor = (
97
- queue : Array < RedisMultiQueuedCommand > ,
98
- selectedDB ?: number ,
99
- chainId ?: symbol
100
- ) => Promise < Array < unknown > > ;
101
-
102
- export default class RedisClientMultiCommand < REPLIES = [ ] > extends RedisMultiCommand {
103
- static #createCommand( command : Command , resp : RespVersions ) {
87
+ export default class RedisClientMultiCommand < REPLIES = [ ] > {
88
+ private static _createCommand ( command : Command , resp : RespVersions ) {
104
89
const transformReply = getTransformReply ( command , resp ) ;
105
- return function ( this : RedisClientMultiCommand ) {
106
- return this . addCommand (
107
- command . transformArguments . apply ( undefined , arguments as any ) ,
90
+ return function ( this : RedisClientMultiCommand , ... args : Array < unknown > ) {
91
+ return this . _multi . addCommand (
92
+ command . transformArguments ( ... args ) ,
108
93
transformReply
109
94
) ;
110
95
} ;
111
96
}
112
97
113
- static #createModuleCommand ( command : Command , resp : RespVersions ) {
98
+ private static _createModuleCommand ( command : Command , resp : RespVersions ) {
114
99
const transformReply = getTransformReply ( command , resp ) ;
115
- return function ( this : { self : RedisClientMultiCommand } ) {
116
- return this . self . addCommand (
117
- command . transformArguments . apply ( undefined , arguments as any ) ,
100
+ return function ( this : { self : RedisClientMultiCommand } , ... args : Array < unknown > ) {
101
+ return this . self . _multi . addCommand (
102
+ command . transformArguments ( ... args ) ,
118
103
transformReply
119
104
) ;
120
105
} ;
121
106
}
122
107
123
- static #createFunctionCommand ( name : string , fn : RedisFunction , resp : RespVersions ) {
108
+ private static _createFunctionCommand ( name : string , fn : RedisFunction , resp : RespVersions ) {
124
109
const prefix = functionArgumentsPrefix ( name , fn ) ,
125
110
transformReply = getTransformReply ( fn , resp ) ;
126
- return function ( this : { self : RedisClientMultiCommand } ) {
127
- const fnArgs = fn . transformArguments . apply ( undefined , arguments as any ) ,
128
- args : CommandArguments = prefix . concat ( fnArgs ) ;
129
- args . preserve = fnArgs . preserve ;
130
- return this . self . addCommand (
131
- args ,
111
+ return function ( this : { self : RedisClientMultiCommand } , ... args : Array < unknown > ) {
112
+ const fnArgs = fn . transformArguments ( ... args ) ,
113
+ redisArgs : CommandArguments = prefix . concat ( fnArgs ) ;
114
+ redisArgs . preserve = fnArgs . preserve ;
115
+ return this . self . _multi . addCommand (
116
+ redisArgs ,
132
117
transformReply
133
118
) ;
134
119
} ;
135
120
}
136
121
137
- static #createScriptCommand ( script : RedisScript , resp : RespVersions ) {
122
+ private static _createScriptCommand ( script : RedisScript , resp : RespVersions ) {
138
123
const transformReply = getTransformReply ( script , resp ) ;
139
- return function ( this : RedisClientMultiCommand ) {
140
- return this . addScript (
124
+ return function ( this : RedisClientMultiCommand , ... args : Array < unknown > ) {
125
+ this . _multi . addScript (
141
126
script ,
142
- script . transformArguments . apply ( undefined , arguments as any ) ,
127
+ script . transformArguments ( ... args ) ,
143
128
transformReply
144
129
) ;
130
+ return this ;
145
131
} ;
146
132
}
147
133
@@ -154,35 +140,36 @@ export default class RedisClientMultiCommand<REPLIES = []> extends RedisMultiCom
154
140
return attachConfig ( {
155
141
BaseClass : RedisClientMultiCommand ,
156
142
commands : COMMANDS ,
157
- createCommand : RedisClientMultiCommand . #createCommand ,
158
- createModuleCommand : RedisClientMultiCommand . #createModuleCommand ,
159
- createFunctionCommand : RedisClientMultiCommand . #createFunctionCommand ,
160
- createScriptCommand : RedisClientMultiCommand . #createScriptCommand ,
143
+ createCommand : RedisClientMultiCommand . _createCommand ,
144
+ createModuleCommand : RedisClientMultiCommand . _createModuleCommand ,
145
+ createFunctionCommand : RedisClientMultiCommand . _createFunctionCommand ,
146
+ createScriptCommand : RedisClientMultiCommand . _createScriptCommand ,
161
147
config
162
148
} ) ;
163
149
}
164
150
165
- readonly #client: RedisClientType ;
166
- #selectedDB?: number ;
151
+ private readonly _multi = new RedisMultiCommand ( ) ;
152
+ private readonly _client : RedisClientType ;
153
+ private _selectedDB ?: number ;
167
154
168
155
constructor ( client : RedisClientType ) {
169
- super ( ) ;
170
- this . #client = client ;
156
+ this . _client = client ;
171
157
}
172
158
173
159
SELECT ( db : number , transformReply ?: TransformReply ) : this {
174
- this . #selectedDB = db ;
175
- return this . addCommand ( [ 'SELECT' , db . toString ( ) ] , transformReply ) ;
160
+ this . _selectedDB = db ;
161
+ this . _multi . addCommand ( [ 'SELECT' , db . toString ( ) ] , transformReply ) ;
162
+ return this ;
176
163
}
177
164
178
165
select = this . SELECT ;
179
166
180
- async exec < T extends MultiReply = MULTI_REPLY [ 'GENERIC' ] > ( execAsPipeline = false ) : Promise < ReplyType < T , REPLIES > > {
167
+ async exec < T extends MultiReply = MULTI_REPLY [ 'GENERIC' ] > ( execAsPipeline = false ) : Promise < MultiReplyType < T , REPLIES > > {
181
168
if ( execAsPipeline ) return this . execAsPipeline < T > ( ) ;
182
169
183
- return this . transformReplies (
184
- await this . #client . executeMulti ( this . queue , this . #selectedDB )
185
- ) as ReplyType < T , REPLIES > ;
170
+ return this . _multi . transformReplies (
171
+ await this . _client . executeMulti ( this . _multi . queue , this . _selectedDB )
172
+ ) as MultiReplyType < T , REPLIES > ;
186
173
}
187
174
188
175
EXEC = this . exec ;
@@ -191,12 +178,12 @@ export default class RedisClientMultiCommand<REPLIES = []> extends RedisMultiCom
191
178
return this . exec < MULTI_REPLY [ 'TYPED' ] > ( execAsPipeline ) ;
192
179
}
193
180
194
- async execAsPipeline < T extends MultiReply = MULTI_REPLY [ 'GENERIC' ] > ( ) : Promise < ReplyType < T , REPLIES > > {
195
- if ( this . queue . length === 0 ) return [ ] as ReplyType < T , REPLIES > ;
181
+ async execAsPipeline < T extends MultiReply = MULTI_REPLY [ 'GENERIC' ] > ( ) : Promise < MultiReplyType < T , REPLIES > > {
182
+ if ( this . _multi . queue . length === 0 ) return [ ] as MultiReplyType < T , REPLIES > ;
196
183
197
- return this . transformReplies (
198
- await this . #client . executePipeline ( this . queue )
199
- ) as ReplyType < T , REPLIES > ;
184
+ return this . _multi . transformReplies (
185
+ await this . _client . executePipeline ( this . _multi . queue )
186
+ ) as MultiReplyType < T , REPLIES > ;
200
187
}
201
188
202
189
execAsPipelineTyped ( ) {
0 commit comments