@@ -65,9 +65,9 @@ export class MultiClient
6565
6666 public async multi ( ) : Promise < void > {
6767
68- if ( ! this . _multi && this . _queue . length ) {
68+ if ( this . _multi ) {
6969
70- throw new E . E_PIPELINING ( ) ;
70+ return ;
7171 }
7272
7373 await this . _command ( 'multi' , [ ] ) ;
@@ -77,7 +77,10 @@ export class MultiClient
7777
7878 public async discard ( ) : Promise < void > {
7979
80- await this . _command ( 'discard' , [ ] ) ;
80+ if ( this . _multi ) {
81+
82+ await this . _command ( 'discard' , [ ] ) ;
83+ }
8184
8285 this . _queue = [ ] ;
8386
@@ -86,8 +89,14 @@ export class MultiClient
8689
8790 public async exec ( ) : Promise < any > {
8891
92+ if ( ! this . _multi ) {
93+
94+ throw new E . E_NOT_MULTI_MODE ( ) ;
95+ }
96+
8997 if ( ! this . _queue . length ) {
9098
99+ await this . discard ( ) ;
91100 return [ ] ;
92101 }
93102
@@ -97,50 +106,25 @@ export class MultiClient
97106
98107 const ret : any [ ] = new Array ( queue . length ) ;
99108
100- if ( this . _multi ) {
101-
102- const data = await this . command ( 'EXEC' , [ ] ) ;
103-
104- this . _multi = false ;
105-
106- for ( let i = 0 ; i < queue . length ; i ++ ) {
109+ const data = await this . command ( 'EXEC' , [ ] ) ;
107110
108- const qi = queue [ i ] ;
111+ this . _multi = false ;
109112
110- if ( qi . process === undefined ) {
113+ for ( let i = 0 ; i < queue . length ; i ++ ) {
111114
112- ret [ i ] = data [ i ] [ 1 ] ;
113- }
114- else if ( qi . process === null ) {
115+ const qi = queue [ i ] ;
115116
116- ret [ i ] = null ;
117- }
118- else {
117+ if ( qi . process === undefined ) {
119118
120- ret [ i ] = qi . process ( data [ i ] [ 1 ] , qi . args ) ;
121- }
119+ ret [ i ] = data [ i ] [ 1 ] ;
122120 }
123- }
124- else {
125-
126- const data : any [ ] = await this . _bulkCommands ( queue ) ;
127-
128- for ( let i = 0 ; i < queue . length ; i ++ ) {
129-
130- const qi = queue [ i ] ;
131-
132- if ( qi . process === undefined ) {
133-
134- ret [ i ] = data [ i ] ;
135- }
136- else if ( qi . process === null ) {
121+ else if ( qi . process === null ) {
137122
138- ret [ i ] = null ;
139- }
140- else {
123+ ret [ i ] = null ;
124+ }
125+ else {
141126
142- ret [ i ] = qi . process ( data [ i ] , qi . args ) ;
143- }
127+ ret [ i ] = qi . process ( data [ i ] [ 1 ] , qi . args ) ;
144128 }
145129 }
146130
@@ -183,13 +167,13 @@ export class MultiClient
183167 'E' ,
184168 `return function(...args) {
185169
186- const req = command(...args);
187-
188170 if (!this._multi) {
189171
190172 throw new E.E_NOT_MULTI_MODE();
191173 }
192174
175+ const req = command(...args);
176+
193177 const ret = this._command(req.cmd, req.args);
194178
195179 this._queue.push({ args: req.args, process: process, cmd: req.cmd });
0 commit comments