-
Notifications
You must be signed in to change notification settings - Fork 17
/
counter.js
45 lines (38 loc) · 950 Bytes
/
counter.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
module.exports = (opts) => {
let redisClient = opts.redisClient;
let counterRedisKey = opts.counterRedisKey;
let counterStream;
const add = (call, callback) => {
redisClient.incrby(counterRedisKey, call.request.count, (err, counter) => {
counterStream && counterStream.write(parseInt(counter));
callback(err, {
count: parseInt(counter),
});
});
};
const reset = (call, callback) => {
redisClient.set(counterRedisKey, 0, (err, counter) => {
counterStream && counterStream.write(0);
callback(err, {
count: 0
});
});
};
const get = (call, callback) => {
redisClient.get(counterRedisKey, (err, counter) => {
callback(err, {
count: parseInt(counter),
});
});
};
const watch = (stream) => {
counterStream = stream;
};
const methods = {
add: add,
reset: reset,
get: get,
watch: watch,
};
return methods;
}