diff --git a/package.json b/package.json index e40ac7a..4dcfb9a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@extollo/lib", - "version": "0.9.7", + "version": "0.9.8", "description": "The framework library that lifts up your code.", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/support/bus/Bus.ts b/src/support/bus/Bus.ts index 8bac60b..ec6b3b3 100644 --- a/src/support/bus/Bus.ts +++ b/src/support/bus/Bus.ts @@ -147,7 +147,7 @@ export class Bus extends Unit implements EventBus< /** Connect an external event bus to this bus. */ async connect(bus: EventBus): Promise { - if ( this.isUp ) { + if ( this.isUp && !bus.isConnected() ) { await bus.up() } @@ -167,6 +167,10 @@ export class Bus extends Unit implements EventBus< }) } + isConnected(): boolean { + return this.isUp + } + async disconnect(bus: EventBus): Promise { await this.subscriptions .where('busUuid', '=', bus.uuid) @@ -202,7 +206,9 @@ export class Bus extends Unit implements EventBus< } } - await this.connectors.awaitMapCall('up') + await this.connectors + .filter(bus => !bus.isConnected()) + .awaitMapCall('up') this.isUp = true } diff --git a/src/support/bus/LocalBus.ts b/src/support/bus/LocalBus.ts index ebc3c90..6d598af 100644 --- a/src/support/bus/LocalBus.ts +++ b/src/support/bus/LocalBus.ts @@ -118,6 +118,10 @@ export class LocalBus extends CanonicalItemClass i this.isUp = true } + isConnected(): boolean { + return this.isUp + } + /** Clean up this event bus. */ async down(): Promise { if ( !this.isUp ) { diff --git a/src/support/bus/RedisBus.ts b/src/support/bus/RedisBus.ts index 6a667cd..d5653a1 100644 --- a/src/support/bus/RedisBus.ts +++ b/src/support/bus/RedisBus.ts @@ -104,6 +104,10 @@ export class RedisBus implements EventBus { .awaitAll() } + isConnected(): boolean { + return Boolean(this.subscriberConnection && this.publisherConnection) + } + async up(): Promise { this.subscriberConnection = await this.redis.getNewConnection() this.publisherConnection = await this.redis.getNewConnection() diff --git a/src/support/bus/types.ts b/src/support/bus/types.ts index 6350fa8..46f1439 100644 --- a/src/support/bus/types.ts +++ b/src/support/bus/types.ts @@ -75,6 +75,8 @@ export interface EventBus { up(): Awaitable down(): Awaitable + + isConnected(): boolean } /** Internal storage format for local event bus subscribers. */