Skip to content

Commit 8e24380

Browse files
author
Ruben Bridgewater
committed
Add optional callback option to duplicate function
1 parent d2b8f2f commit 8e24380

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,10 @@ the second word as first parameter:
597597
client.multi().script('load', 'return 1').exec(...);
598598
client.multi([['script', 'load', 'return 1']]).exec(...);
599599

600-
## client.duplicate([options])
600+
## client.duplicate([options][, callback])
601601

602602
Duplicate all current options and return a new redisClient instance. All options passed to the duplicate function are going to replace the original option.
603+
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.
603604

604605
## client.send_command(command_name[, [args][, callback]])
605606

lib/extendedApi.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,30 @@ RedisClient.prototype.unref = function () {
7979
}
8080
};
8181

82-
RedisClient.prototype.duplicate = function (options) {
82+
RedisClient.prototype.duplicate = function (options, callback) {
83+
if (typeof options === 'function') {
84+
callback = options;
85+
options = null;
86+
}
8387
var existing_options = utils.clone(this.options);
8488
options = utils.clone(options);
8589
for (var elem in options) { // jshint ignore: line
8690
existing_options[elem] = options[elem];
8791
}
8892
var client = new RedisClient(existing_options);
8993
client.selected_db = this.selected_db;
94+
if (typeof callback === 'function') {
95+
var ready_listener = function () {
96+
callback(null, client);
97+
client.removeAllListeners(error_listener);
98+
};
99+
var error_listener = function (err) {
100+
callback(err);
101+
client.end(true);
102+
};
103+
client.once('ready', ready_listener);
104+
client.once('error', error_listener);
105+
return;
106+
}
90107
return client;
91108
};

test/node_redis.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,38 @@ describe('The node_redis client', function () {
6666
done();
6767
});
6868
});
69+
70+
it('works with a callback', function (done) {
71+
client.duplicate(function (err, client) {
72+
assert(!err);
73+
assert.strictEqual(client.ready, true);
74+
client.quit(done);
75+
});
76+
});
77+
78+
it('works with a callback and errors out', function (done) {
79+
client.duplicate({
80+
port: '9999'
81+
}, function (err, client) {
82+
assert.strictEqual(err.code, 'ECONNREFUSED');
83+
done(client);
84+
});
85+
});
86+
87+
it('works with a promises', function () {
88+
return client.duplicateAsync().then(function (client) {
89+
assert.strictEqual(client.ready, true);
90+
return client.quitAsync();
91+
});
92+
});
93+
94+
it('works with a promises and errors', function () {
95+
return client.duplicateAsync({
96+
port: 9999
97+
}).catch(function (err) {
98+
assert.strictEqual(err.code, 'ECONNREFUSED');
99+
});
100+
});
69101
});
70102

71103
describe('big data', function () {

0 commit comments

Comments
 (0)