@nebulae/event-store-tpi-rx6 1.1.3 → 1.1.4

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.
@@ -1,10 +1,10 @@
1
1
  'use strict'
2
2
 
3
3
  const Rx = require('rxjs');
4
- const { filter, map} = require('rxjs/operators');
4
+ const { filter, map } = require('rxjs/operators');
5
5
  // Imports the Google Cloud client library
6
6
  const uuidv4 = require('uuid/v4');
7
- const PubSub = require('@google-cloud/pubsub');
7
+ const { PubSub } = require('@google-cloud/pubsub');
8
8
 
9
9
  class PubSubBroker {
10
10
 
@@ -12,7 +12,6 @@ class PubSubBroker {
12
12
  //this.projectId = projectId;
13
13
  this.eventsTopic = eventsTopic;
14
14
  this.eventsTopicSubscription = eventsTopicSubscription;
15
-
16
15
  /**
17
16
  * Rx Subject for every incoming event
18
17
  */
@@ -20,29 +19,19 @@ class PubSubBroker {
20
19
  this.orderedIncomingEvents$ = this.incomingEvents$.pipe(
21
20
  filter(msg => msg)
22
21
  )
23
- // .groupBy(msg => msg.data.at)
24
- // .mergeMap(groupStream =>
25
- // groupStream.bufferWhen(() => groupStream.debounceTime(250))
26
- // .filter(bufferedArray => bufferedArray && bufferedArray.length > 0)
27
- // .map(bufferedArray => bufferedArray.sort((o1, o2) => { return o1.data.av - o2.data.av }))
28
- // .mergeMap(bufferedArray => Rx.Observable.from(bufferedArray))
29
- // );
30
22
  this.senderId = uuidv4();
31
-
32
- this.pubsubClient = new PubSub({
33
- //projectId: projectId,
34
- });
35
-
36
- this.topic = this.pubsubClient.topic(eventsTopic);
23
+ this.pubsubClient = new PubSub({});
37
24
  }
38
25
 
39
26
  /**
40
27
  * Starts Broker connections
41
28
  * Returns an Obserable that resolves to each connection result
42
29
  */
43
- start$() {
44
- return Rx.Observable.create(observer => {
45
- this.startMessageListener();
30
+ start$() {
31
+ return new Rx.Observable(async (observer) => {
32
+ const [topic] = await this.pubsubClient.createTopic(this.eventsTopic);
33
+ this.topic = topic;
34
+ this.startMessageListener(topic);
46
35
  observer.next(`Event Store PubSub Broker listening: Topic=${this.eventsTopic}, subscriptionName=${this.eventsTopicSubscription}`);
47
36
  observer.complete();
48
37
  });
@@ -78,11 +67,12 @@ class PubSubBroker {
78
67
  publish$(data) {
79
68
  const dataBuffer = Buffer.from(JSON.stringify(data));
80
69
  return Rx.defer(() =>
81
- this.topic.publisher().publish(
82
- dataBuffer,
83
- { senderId: this.senderId }))
84
- //.do(messageId => console.log(`PubSub Message published through ${this.topic.name}, Message=${JSON.stringify(data)}`))
85
- ;
70
+ this.topic.publishMessage(
71
+ {
72
+ data: dataBuffer,
73
+ attributes: { senderId: this.senderId }
74
+ }
75
+ ));
86
76
  }
87
77
 
88
78
  /**
@@ -102,33 +92,32 @@ class PubSubBroker {
102
92
  /**
103
93
  * Returns an Observable that resolves to the subscription
104
94
  */
105
- getSubscription$() {
106
- return Rx.defer(() =>
107
- this.topic.subscription(this.eventsTopicSubscription)
108
- .get({ autoCreate: true })).pipe(
109
- map(results => results[0])
110
- );
95
+ getSubscription$(topic) {
96
+ return Rx.defer(() => (topic || this.topic).createSubscription(this.eventsTopicSubscription)).pipe(
97
+ map(([subscription]) => subscription),
98
+ );
111
99
  }
112
100
 
113
101
  /**
114
102
  * Starts to listen messages
115
103
  */
116
- startMessageListener() {
117
- this.messageListenerSubscription = this.getSubscription$()
104
+ startMessageListener(topic) {
105
+ this.messageListenerSubscription = this.getSubscription$(topic)
118
106
  .subscribe(
119
107
  (pubSubSubscription) => {
120
108
  this.onMessage = message => {
121
109
  message.ack();
122
- //console.log(`Received message ${message.id}:`);
123
110
  this.incomingEvents$.next({
124
111
  data: JSON.parse(message.data),
125
112
  id: message.id,
126
113
  attributes: message.attributes,
127
114
  correlationId: message.attributes.correlationId
128
115
  });
129
- //console.log(`Execute ack message ${message.id}:`);
130
116
  };
131
117
  pubSubSubscription.on(`message`, this.onMessage);
118
+ pubSubSubscription.on('error', error => {
119
+ console.error('@nebulae/event-store-tpi-rx6.PubSubBroker: Received error:', error);
120
+ });
132
121
  },
133
122
  (err) => {
134
123
  console.error('Failed to obtain EventStore subscription', err);
package/package.json CHANGED
@@ -35,7 +35,7 @@
35
35
  "url": "https://github.com/NebulaEngineering/event-store-tpi-rx6/issues"
36
36
  },
37
37
  "dependencies": {
38
- "@google-cloud/pubsub": "^0.18.0",
38
+ "@google-cloud/pubsub": "^2.19.4",
39
39
  "async-mqtt": "^1.0.1",
40
40
  "dotenv": "^5.0.1",
41
41
  "mongodb": "^3.0.5",
@@ -64,5 +64,5 @@
64
64
  "scripts": {
65
65
  "test": "mocha --recursive --reporter spec"
66
66
  },
67
- "version": "1.1.3"
67
+ "version": "1.1.4"
68
68
  }