This repository has been archived by the owner on May 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
61 lines (51 loc) · 1.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
var redis = require('redis'),
redisCmds = require('redis-commands').list;
function createCb(resolve, reject) {
return function (err, value) {
if (err !== null) {
reject(err);
} else {
resolve(value);
}
};
}
module.exports = function (promiseFactory) {
var mlproto = redis.Multi.prototype,
clproto = redis.RedisClient.prototype;
if (!promiseFactory) {
promiseFactory = function (resolver) {
return new Promise(resolver);
};
}
function promisify(f) {
return function () {
var args = Array.prototype.slice.call(arguments),
that = this;
if (typeof args[args.length - 1] === 'function') {
// Okay. Someone supplied a callback. Most likely some internal
// node-redis call (ready probe etc.). Oh, as a result of
// supporting internal callback-style calls, one can now use
// promise-redis as a dropin replacement for node-redis.
f.apply(this, args);
} else {
return promiseFactory(function (resolve, reject) {
args.push(createCb(resolve, reject));
f.apply(that, args);
});
}
};
}
redisCmds.forEach(function (fullCommand) {
var cmd = fullCommand.split(' ')[0];
if (cmd !== 'multi') {
clproto[cmd] = promisify(clproto[cmd]);
clproto[cmd.toUpperCase()] = clproto[cmd];
}
});
// For Multi only `exec` command returns promise.
mlproto.exec_transaction = promisify(mlproto.exec_transaction);
mlproto.exec = mlproto.exec_transaction;
mlproto.EXEC = mlproto.exec;
return redis;
};