@nebulae/event-store-tpi-rx6 1.1.4 → 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.
@@ -28,11 +28,22 @@ class PubSubBroker {
28
28
  * Returns an Obserable that resolves to each connection result
29
29
  */
30
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);
35
- observer.next(`Event Store PubSub Broker listening: Topic=${this.eventsTopic}, subscriptionName=${this.eventsTopicSubscription}`);
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}`);
36
47
  observer.complete();
37
48
  });
38
49
  }
@@ -42,10 +53,10 @@ class PubSubBroker {
42
53
  */
43
54
  stop$() {
44
55
  return Rx.Observable.create(observer => {
45
- this.getSubscription$().subscribe(
56
+ Rx.defer(() => this.getSubscription$()).subscribe(
46
57
  (subscription) => {
47
58
  subscription.removeListener(`message`, this.onMessage);
48
- 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}`);
49
60
  },
50
61
  (error) => observer.error(error),
51
62
  () => {
@@ -92,17 +103,26 @@ class PubSubBroker {
92
103
  /**
93
104
  * Returns an Observable that resolves to the subscription
94
105
  */
95
- getSubscription$(topic) {
96
- return Rx.defer(() => (topic || this.topic).createSubscription(this.eventsTopicSubscription)).pipe(
97
- map(([subscription]) => subscription),
98
- );
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
+ }
99
119
  }
100
120
 
101
121
  /**
102
122
  * Starts to listen messages
103
123
  */
104
124
  startMessageListener(topic) {
105
- this.messageListenerSubscription = this.getSubscription$(topic)
125
+ this.messageListenerSubscription = Rx.defer(() => this.getSubscription$(topic))
106
126
  .subscribe(
107
127
  (pubSubSubscription) => {
108
128
  this.onMessage = message => {
package/package.json CHANGED
@@ -64,5 +64,5 @@
64
64
  "scripts": {
65
65
  "test": "mocha --recursive --reporter spec"
66
66
  },
67
- "version": "1.1.4"
67
+ "version": "1.1.5"
68
68
  }