woonplan-packages-redishelper 1.0.4 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +27 -4
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
|
-
exports.
|
|
2
|
-
|
|
1
|
+
exports.hookCallbackToRedisStream = async ( redis, groupname, channelname, consumername, callback ) => {
|
|
2
|
+
// try to create channel
|
|
3
|
+
group = redis.xgroup('CREATE', channelname, groupname, '$', 'MKSTREAM' );
|
|
4
|
+
group.catch((error)=>{
|
|
5
|
+
console.log( 'GROUP EXISTS' );
|
|
6
|
+
}).then(()=>{
|
|
7
|
+
console.log( 'listening to ' + channelname )
|
|
8
|
+
listenForMessage( redis, groupname, channelname, consumername, callback )
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
const listenForMessage = ( redis, groupname, channelname, consumername, callback, lastId = "$" ) => {
|
|
3
16
|
redis.xreadgroup('GROUP', groupname, consumername, 'COUNT', 1, 'BLOCK', 0, 'STREAMS', channelname, '>').then( async (data) => {
|
|
4
17
|
const channelname = data[0][0];
|
|
5
18
|
const id = data[0][0];
|
|
@@ -11,9 +24,19 @@ exports.listenForMessage = async ( redis, groupname, channelname, consumername,
|
|
|
11
24
|
for( let n in messages ){
|
|
12
25
|
const message = messages[n];
|
|
13
26
|
const id = message[0];
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
const messageData = message[1].filter( (key,n) => n == 0 || n % 2 == 0 ).reduce(
|
|
31
|
+
(obj, key ) => Object.defineProperty(object1, key, {
|
|
32
|
+
value: message[1][message[1].indexOf( key )+1],
|
|
33
|
+
writable: false
|
|
34
|
+
}), {}
|
|
35
|
+
);
|
|
36
|
+
|
|
14
37
|
|
|
15
|
-
callback(
|
|
16
|
-
|
|
38
|
+
callback( messageData ).then( result => {
|
|
39
|
+
redis.xack( channelname, groupname, id ).then( success => {
|
|
17
40
|
console.log( id + ' acknowledged')
|
|
18
41
|
});
|
|
19
42
|
});
|