@@ -658,7 +658,7 @@ RedisClient.prototype.return_reply = function (reply) {
658
658
} else if ( type === 'pmessage' ) {
659
659
this . emit ( 'pmessage' , reply [ 1 ] . toString ( ) , reply [ 2 ] , reply [ 3 ] ) ; // pattern, channel, message
660
660
} else if ( type === 'subscribe' || type === 'unsubscribe' || type === 'psubscribe' || type === 'punsubscribe' ) {
661
- if ( reply [ 2 ] === 0 ) {
661
+ if ( reply [ 2 ] . toString ( ) === '0' ) {
662
662
this . pub_sub_mode = false ;
663
663
debug ( 'All subscriptions removed, exiting pub/sub mode' ) ;
664
664
} else {
@@ -1127,6 +1127,7 @@ Multi.prototype.exec_transaction = function (callback) {
1127
1127
var self = this ;
1128
1128
var len = this . queue . length ;
1129
1129
var cb ;
1130
+ var args_len = 1 ;
1130
1131
this . errors = [ ] ;
1131
1132
this . callback = callback ;
1132
1133
this . _client . cork ( len + 2 ) ;
@@ -1136,9 +1137,11 @@ Multi.prototype.exec_transaction = function (callback) {
1136
1137
for ( var index = 0 ; index < len ; index ++ ) {
1137
1138
var args = this . queue . get ( index ) . slice ( 0 ) ;
1138
1139
var command = args . shift ( ) ;
1139
- if ( typeof args [ args . length - 1 ] === 'function' ) {
1140
+ args_len = args . length - 1 ;
1141
+ if ( typeof args [ args_len ] === 'function' || typeof args [ args_len ] === 'undefined' ) {
1140
1142
cb = args . pop ( ) ;
1141
1143
} else {
1144
+ // Explicitly set the callback to undefined. Otherwise we might have the callback from the command earlier
1142
1145
cb = undefined ;
1143
1146
}
1144
1147
// Keep track of who wants buffer responses:
@@ -1213,14 +1216,12 @@ Multi.prototype.callback = function (cb, i) {
1213
1216
return function ( err , res ) {
1214
1217
if ( err ) {
1215
1218
self . results [ i ] = err ;
1219
+ // Add the position to the error
1220
+ self . results [ i ] . position = i ;
1216
1221
} else {
1217
1222
self . results [ i ] = res ;
1218
1223
}
1219
- if ( cb ) {
1220
- cb ( err , res ) ;
1221
- }
1222
- // Do not emit an error here. Otherwise each error would result in one emit.
1223
- // The errors will be returned in the result anyway
1224
+ cb ( err , res ) ;
1224
1225
} ;
1225
1226
} ;
1226
1227
@@ -1229,6 +1230,25 @@ Multi.prototype.exec = Multi.prototype.EXEC = Multi.prototype.exec_batch = funct
1229
1230
var self = this ;
1230
1231
var index = 0 ;
1231
1232
var args ;
1233
+ var args_len = 1 ;
1234
+ var callbackWithoutCB = function ( err , res ) {
1235
+ if ( err ) {
1236
+ self . results . push ( err ) ;
1237
+ // Add the position to the error
1238
+ var i = self . results . length - 1 ;
1239
+ self . results [ i ] . position = i ;
1240
+ } else {
1241
+ self . results . push ( res ) ;
1242
+ }
1243
+ // Do not emit an error here. Otherwise each error would result in one emit.
1244
+ // The errors will be returned in the result anyway
1245
+ } ;
1246
+ var lastCallback = function ( cb ) {
1247
+ return function ( err , res ) {
1248
+ cb ( err , res ) ;
1249
+ callback ( null , self . results ) ;
1250
+ } ;
1251
+ } ;
1232
1252
if ( len === 0 ) {
1233
1253
if ( callback ) {
1234
1254
// The execution order won't be obtained in this case
@@ -1238,21 +1258,21 @@ Multi.prototype.exec = Multi.prototype.EXEC = Multi.prototype.exec_batch = funct
1238
1258
}
1239
1259
return true ;
1240
1260
}
1241
- this . results = new Array ( len ) ;
1261
+ this . results = [ ] ;
1242
1262
this . _client . cork ( len ) ;
1243
- var lastCallback = function ( cb ) {
1244
- return function ( err , res ) {
1245
- cb ( err , res ) ;
1246
- callback ( null , self . results ) ;
1247
- } ;
1248
- } ;
1249
1263
while ( args = this . queue . shift ( ) ) {
1250
1264
var command = args . shift ( ) ;
1251
1265
var cb ;
1252
- if ( typeof args [ args . length - 1 ] === 'function' ) {
1266
+ // Improve the callback function generation by using new Function
1267
+ // check https://github.com/petkaantonov/bluebird/blob/master/src/promisify.js
1268
+ args_len = args . length - 1 ;
1269
+ if ( typeof args [ args_len ] === 'function' ) {
1253
1270
cb = this . callback ( args . pop ( ) , index ) ;
1254
1271
} else {
1255
- cb = this . callback ( undefined , index ) ;
1272
+ cb = callbackWithoutCB ;
1273
+ if ( typeof args [ args_len ] === 'undefined' ) {
1274
+ args . pop ( ) ;
1275
+ }
1256
1276
}
1257
1277
if ( callback && index === len - 1 ) {
1258
1278
cb = lastCallback ( cb ) ;
0 commit comments