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

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,30 +19,31 @@ 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();
46
- observer.next(`Event Store PubSub Broker listening: Topic=${this.eventsTopic}, subscriptionName=${this.eventsTopicSubscription}`);
30
+ start$() {
31
+ return new Rx.Observable(async (observer) => {
32
+ try {
33
+ const [topic] = await this.pubsubClient.createTopic(this.eventsTopic);
34
+ console.log(`@nebulae/event-store-tpi-rx6: Event Store PubSub Broker created topic: Topic=${this.eventsTopic}`);
35
+ this.topic = topic;
36
+ } catch (error) {
37
+ if(error.code === 6) {
38
+ this.topic = this.pubsubClient.topic(this.eventsTopic);
39
+ console.log(`@nebulae/event-store-tpi-rx6: Event Store PubSub Broker topic already exists, getting existing topic: Topic=${this.eventsTopic}`);
40
+ } else {
41
+ observer.error(`@nebulae/event-store-tpi-rx6: Event Store PubSub Broker failed to create or get topic: Topic=${this.eventsTopic}, Error=${error.message}`);
42
+ return;
43
+ }
44
+ }
45
+ this.startMessageListener(this.topic);
46
+ observer.next(`@nebulae/event-store-tpi-rx6: Event Store PubSub Broker listening: Topic=${this.eventsTopic}, subscriptionName=${this.eventsTopicSubscription}`);
47
47
  observer.complete();
48
48
  });
49
49
  }
@@ -53,10 +53,10 @@ class PubSubBroker {
53
53
  */
54
54
  stop$() {
55
55
  return Rx.Observable.create(observer => {
56
- this.getSubscription$().subscribe(
56
+ Rx.defer(() => this.getSubscription$()).subscribe(
57
57
  (subscription) => {
58
58
  subscription.removeListener(`message`, this.onMessage);
59
- observer.next(`Event Store PubSub Broker removed listener: Topic=${this.eventsTopic}, subscriptionName=${subscription}`);
59
+ observer.next(`@nebulae/event-store-tpi-rx6: Event Store PubSub Broker removed listener: Topic=${this.eventsTopic}, subscriptionName=${subscription}`);
60
60
  },
61
61
  (error) => observer.error(error),
62
62
  () => {
@@ -78,11 +78,12 @@ class PubSubBroker {
78
78
  publish$(data) {
79
79
  const dataBuffer = Buffer.from(JSON.stringify(data));
80
80
  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
- ;
81
+ this.topic.publishMessage(
82
+ {
83
+ data: dataBuffer,
84
+ attributes: { senderId: this.senderId }
85
+ }
86
+ ));
86
87
  }
87
88
 
88
89
  /**
@@ -102,33 +103,41 @@ class PubSubBroker {
102
103
  /**
103
104
  * Returns an Observable that resolves to the subscription
104
105
  */
105
- getSubscription$() {
106
- return Rx.defer(() =>
107
- this.topic.subscription(this.eventsTopicSubscription)
108
- .get({ autoCreate: true })).pipe(
109
- map(results => results[0])
110
- );
106
+ async getSubscription$(topic) {
107
+ try {
108
+ const [subscription] = await (topic || this.topic).createSubscription(this.eventsTopicSubscription);
109
+ console.log(`@nebulae/event-store-tpi-rx6: Event Store PubSub Broker created subscription: Topic=${this.eventsTopic}, subscriptionName=${this.eventsTopicSubscription}`);
110
+ return subscription;
111
+ } catch (error) {
112
+ if(error.code === 6) {
113
+ console.log(`@nebulae/event-store-tpi-rx6: Event Store PubSub Broker subscription already exists, getting existing subscription: Topic=${this.eventsTopic}, subscriptionName=${this.eventsTopicSubscription}`);
114
+ return (topic || this.topic).subscription(this.eventsTopicSubscription);
115
+ } else {
116
+ throw new Error(`@nebulae/event-store-tpi-rx6: Event Store PubSub Broker failed to create or get subscription: Topic=${this.eventsTopic}, subscriptionName=${this.eventsTopicSubscription}, Error=${error.message}`);
117
+ }
118
+ }
111
119
  }
112
120
 
113
121
  /**
114
122
  * Starts to listen messages
115
123
  */
116
- startMessageListener() {
117
- this.messageListenerSubscription = this.getSubscription$()
124
+ startMessageListener(topic) {
125
+ this.messageListenerSubscription = Rx.defer(() => this.getSubscription$(topic))
118
126
  .subscribe(
119
127
  (pubSubSubscription) => {
120
128
  this.onMessage = message => {
121
129
  message.ack();
122
- //console.log(`Received message ${message.id}:`);
123
130
  this.incomingEvents$.next({
124
131
  data: JSON.parse(message.data),
125
132
  id: message.id,
126
133
  attributes: message.attributes,
127
134
  correlationId: message.attributes.correlationId
128
135
  });
129
- //console.log(`Execute ack message ${message.id}:`);
130
136
  };
131
137
  pubSubSubscription.on(`message`, this.onMessage);
138
+ pubSubSubscription.on('error', error => {
139
+ console.error('@nebulae/event-store-tpi-rx6.PubSubBroker: Received error:', error);
140
+ });
132
141
  },
133
142
  (err) => {
134
143
  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.5"
68
68
  }