woonplan-packages-redishelper 1.0.16 → 1.0.20

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/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import { Redis } from "ioredis";
2
- declare const listenToStream: (client: Redis, groupname: string, channelname: string, consumername: string, callback: Function) => void;
3
- export { listenToStream };
2
+ import { RedisMessage } from "./types";
3
+ declare const listenToStream: (client: Redis, groupname: string, channelname: string, consumername: string, callback: Function) => Promise<boolean>;
4
+ export { listenToStream, RedisMessage };
package/dist/index.js CHANGED
@@ -1,19 +1,52 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.listenToStream = void 0;
4
- var listenToStream = function (client, groupname, channelname, consumername, callback) {
5
- client.xgroup('CREATE', channelname, groupname, '$', 'MKSTREAM').catch(function (error) {
6
- console.log('halloo');
7
- console.log(typeof error);
4
+ var listenToStream = function (client, groupname, channelname, consumername, callback) { return new Promise(function (resolve, reject) {
5
+ createGroup(client, groupname, channelname).then(function () {
6
+ listenForMessage(client, groupname, channelname, consumername, callback);
7
+ resolve(true);
8
+ }).catch(function (error) {
8
9
  console.log(error);
10
+ reject(error);
9
11
  });
10
- };
12
+ }); };
11
13
  exports.listenToStream = listenToStream;
12
- var listenForMessage = function (client, groupname, channelname, consumername, lastId) {
14
+ var createGroup = function (client, groupname, channelname) { return new Promise(function (resolve, reject) {
15
+ client.xgroup('CREATE', channelname, groupname, '$', 'MKSTREAM').then(function () { return resolve(true); }).catch(function (error) {
16
+ if (error.message.includes('BUSYGROUP'))
17
+ return resolve(true);
18
+ reject(error);
19
+ });
20
+ }); };
21
+ var listenForMessage = function (client, groupname, channelname, consumername, callback, lastId) {
13
22
  if (lastId === void 0) { lastId = "$"; }
23
+ console.log("listening to ".concat(channelname, " as ").concat(consumername));
14
24
  client.xreadgroup('GROUP', groupname, consumername, 'COUNT', 1, 'BLOCK', 0, 'STREAMS', channelname, '>')
15
- .then(function () {
16
- console.log(1);
25
+ .then(function (results) {
26
+ results.forEach(function (result) {
27
+ var group = result[0];
28
+ var messages = result[1];
29
+ messages.forEach(function (message) {
30
+ var id = message[0];
31
+ var keysandvalues = message[1];
32
+ var messagedata = {};
33
+ keysandvalues.forEach(function (key, n) {
34
+ if (!(n == 0 || n % 2 == 0))
35
+ return;
36
+ messagedata[key] = keysandvalues[n + 1];
37
+ });
38
+ callback(messagedata).then(function () {
39
+ return client.xack(channelname, groupname, id).then(function () {
40
+ console.log(id + ' acknowledged');
41
+ });
42
+ }).catch(function (error) {
43
+ console.log(error);
44
+ }).finally(function () {
45
+ // relisten
46
+ listenForMessage(client, groupname, channelname, consumername, callback);
47
+ });
48
+ });
49
+ });
17
50
  })
18
51
  .catch(function (error) {
19
52
  console.log(error);
package/index.ts CHANGED
@@ -1,24 +1,79 @@
1
1
  import { Redis } from "ioredis"
2
+ import { RedisMessage } from "./types"
2
3
 
3
- const listenToStream = ( client:Redis, groupname:string, channelname:string, consumername:string, callback:Function ) => {
4
- client.xgroup('CREATE', channelname, groupname, '$', 'MKSTREAM' ).catch(
4
+ const listenToStream = ( client:Redis, groupname:string, channelname:string, consumername:string, callback:Function ):Promise<boolean> => new Promise((resolve,reject) => {
5
+ createGroup( client, groupname, channelname ).then(
6
+ ( ) => {
7
+ listenForMessage( client, groupname, channelname, consumername, callback );
8
+ resolve( true )
9
+ }
10
+ ).catch(
5
11
  ( error ) => {
6
- console.log( 'halloo');
7
- console.log( typeof error );
8
12
  console.log( error );
13
+ reject( error )
9
14
  }
10
15
  )
11
- }
16
+ })
17
+
18
+ const createGroup = ( client:Redis, groupname:string, channelname:string ):Promise<boolean> => new Promise((resolve,reject) => {
19
+ client.xgroup('CREATE', channelname, groupname, '$', 'MKSTREAM' ).then(
20
+ () => resolve( true )
21
+ ).catch(
22
+ ( error:Error ) => {
23
+ if( error.message.includes('BUSYGROUP'))
24
+ return resolve( true );
25
+
26
+ reject( error );
27
+ }
28
+ )
29
+ })
30
+
12
31
 
13
- const listenForMessage = ( client:Redis, groupname:string, channelname:string, consumername:string, lastId = "$" ) => {
32
+
33
+ const listenForMessage = ( client:Redis, groupname:string, channelname:string, consumername:string, callback:Function, lastId = "$" ) => {
34
+ console.log( `listening to ${channelname} as ${consumername}`)
14
35
  client.xreadgroup('GROUP', groupname, consumername, 'COUNT', 1, 'BLOCK', 0, 'STREAMS', channelname, '>')
15
- .then( () => {
16
- console.log( 1 );
17
- } )
18
- .catch( ( error ) => {
19
-
20
- console.log( error );
21
- })
36
+ .then( ( results ) => {
37
+ results.forEach(
38
+ (result) => {
39
+ const group = result[0];
40
+ const messages = result[1];
41
+
42
+ messages.forEach(
43
+ (message) => {
44
+ const id = message[0];
45
+ const keysandvalues = message[1];
46
+
47
+
48
+ let messagedata:RedisMessage = {};
49
+
50
+ keysandvalues.forEach( (key, n ) => {
51
+ if( !( n == 0 || n % 2 == 0 ) ) return;
52
+ messagedata[key] = keysandvalues[n+1];
53
+ })
54
+
55
+ callback( messagedata ).then(() =>
56
+ client.xack( channelname, groupname, id ).then( () => {
57
+ console.log( id + ' acknowledged')
58
+ })
59
+ ).catch(
60
+ ( error:Error ) => {
61
+ console.log( error );
62
+ }
63
+ ).finally(()=>{
64
+ // relisten
65
+ listenForMessage( client, groupname, channelname, consumername, callback );
66
+ })
67
+ }
68
+ )
69
+
70
+ }
71
+ )
72
+ })
73
+ .catch( ( error ) => {
74
+
75
+ console.log( error );
76
+ })
22
77
  }
23
78
 
24
- export { listenToStream }
79
+ export { listenToStream, RedisMessage }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "woonplan-packages-redishelper",
3
- "version": "1.0.16",
3
+ "version": "1.0.20",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/types.d.ts",
package/types.d.ts CHANGED
@@ -1,3 +1,5 @@
1
- interface test{
2
- test : boolean
3
- }
1
+ interface RedisMessage{
2
+ [index: string]: any;
3
+ }
4
+
5
+ export { RedisMessage}