From 8e24380d533485298523b2a03c07ab8f13f897cb Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Wed, 13 Apr 2016 04:00:23 +0200 Subject: [PATCH] Add optional callback option to duplicate function --- README.md | 3 ++- lib/extendedApi.js | 19 ++++++++++++++++++- test/node_redis.spec.js | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e9c92bd..5aa7804 100644 --- a/README.md +++ b/README.md @@ -597,9 +597,10 @@ the second word as first parameter: client.multi().script('load', 'return 1').exec(...); client.multi([['script', 'load', 'return 1']]).exec(...); -## client.duplicate([options]) +## client.duplicate([options][, callback]) Duplicate all current options and return a new redisClient instance. All options passed to the duplicate function are going to replace the original option. +If you pass a callback, duplicate is going to wait until the client is ready and returns it in the callback. If an error occurs in the meanwhile, that is going to return an error instead in the callback. ## client.send_command(command_name[, [args][, callback]]) diff --git a/lib/extendedApi.js b/lib/extendedApi.js index 0d70bf1..2101314 100644 --- a/lib/extendedApi.js +++ b/lib/extendedApi.js @@ -79,7 +79,11 @@ RedisClient.prototype.unref = function () { } }; -RedisClient.prototype.duplicate = function (options) { +RedisClient.prototype.duplicate = function (options, callback) { + if (typeof options === 'function') { + callback = options; + options = null; + } var existing_options = utils.clone(this.options); options = utils.clone(options); for (var elem in options) { // jshint ignore: line @@ -87,5 +91,18 @@ RedisClient.prototype.duplicate = function (options) { } var client = new RedisClient(existing_options); client.selected_db = this.selected_db; + if (typeof callback === 'function') { + var ready_listener = function () { + callback(null, client); + client.removeAllListeners(error_listener); + }; + var error_listener = function (err) { + callback(err); + client.end(true); + }; + client.once('ready', ready_listener); + client.once('error', error_listener); + return; + } return client; }; diff --git a/test/node_redis.spec.js b/test/node_redis.spec.js index fd9fc2c..e556dc7 100644 --- a/test/node_redis.spec.js +++ b/test/node_redis.spec.js @@ -66,6 +66,38 @@ describe('The node_redis client', function () { done(); }); }); + + it('works with a callback', function (done) { + client.duplicate(function (err, client) { + assert(!err); + assert.strictEqual(client.ready, true); + client.quit(done); + }); + }); + + it('works with a callback and errors out', function (done) { + client.duplicate({ + port: '9999' + }, function (err, client) { + assert.strictEqual(err.code, 'ECONNREFUSED'); + done(client); + }); + }); + + it('works with a promises', function () { + return client.duplicateAsync().then(function (client) { + assert.strictEqual(client.ready, true); + return client.quitAsync(); + }); + }); + + it('works with a promises and errors', function () { + return client.duplicateAsync({ + port: 9999 + }).catch(function (err) { + assert.strictEqual(err.code, 'ECONNREFUSED'); + }); + }); }); describe('big data', function () {