-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
85 lines (67 loc) · 1.49 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict'
const kPipeline = Symbol('pipeline')
const kExec = Symbol('exec')
const notAllowedCommands = ['subscribe', 'psubscribe', 'pipeline', 'multi']
function auto (client) {
let pipeline
let running = false
const obj = {}
for (const cmd of client.getBuiltinCommands()) {
if (!notAllowedCommands.includes(cmd)) {
obj[cmd] = buildWrap(cmd)
}
}
Object.defineProperty(obj, 'queued', {
get () {
if (pipeline === undefined) {
return 0
}
return pipeline.queued
}
})
Object.defineProperty(obj, kPipeline, {
get () {
if (pipeline === undefined) {
pipeline = client.pipeline()
pipeline[kExec] = false
pipeline.queued = 0
}
return pipeline
}
})
return obj
function exec () {
if (running) {
return
}
running = true
pipeline.exec(function () {
running = false
if (pipeline) {
exec()
}
})
pipeline = undefined
}
function buildWrap (key) {
return function (...args) {
const pipeline = this[kPipeline]
if (!pipeline[kExec]) {
pipeline[kExec] = true
process.nextTick(exec)
}
pipeline.queued++
return new Promise(function (resolve, reject) {
pipeline[key](...args, function (err, value) {
if (err) {
reject(err)
return
}
resolve(value)
})
})
}
}
}
module.exports = auto
module.exports.kPipeline = kPipeline