diff --git a/src/Repeater/DefaultRepeaterServer.ts b/src/Repeater/DefaultRepeaterServer.ts index 31ea877d..df48809c 100644 --- a/src/Repeater/DefaultRepeaterServer.ts +++ b/src/Repeater/DefaultRepeaterServer.ts @@ -95,6 +95,12 @@ export class DefaultRepeaterServer implements RepeaterServer { logger.debug('Event bus connected to %s', this.options.uri); } + public connected(handler: () => void | Promise): void { + this.socket.on('connect', () => + this.processEventHandler('connect', undefined, handler) + ); + } + public requestReceived( handler: ( event: RepeaterServerRequestEvent diff --git a/src/Repeater/RepeaterServer.ts b/src/Repeater/RepeaterServer.ts index a7df006a..adc75022 100644 --- a/src/Repeater/RepeaterServer.ts +++ b/src/Repeater/RepeaterServer.ts @@ -121,6 +121,8 @@ export interface RepeaterServer { reconnectionSucceeded(handler: () => void | Promise): void; + connected(handler: () => void | Promise): void; + errorOccurred( handler: (event: RepeaterServerErrorEvent) => void | Promise ): void; diff --git a/src/Repeater/ServerRepeaterLauncher.ts b/src/Repeater/ServerRepeaterLauncher.ts index ed3f8d13..3cf3ba06 100644 --- a/src/Repeater/ServerRepeaterLauncher.ts +++ b/src/Repeater/ServerRepeaterLauncher.ts @@ -19,6 +19,7 @@ import { delay, inject, injectable } from 'tsyringe'; export class ServerRepeaterLauncher implements RepeaterLauncher { private static SERVICE_NAME = 'bright-repeater'; private repeaterStarted: boolean = false; + private repeaterId: string | undefined; constructor( @inject(RuntimeDetector) private readonly runtimeDetector: RuntimeDetector, @@ -91,17 +92,9 @@ export class ServerRepeaterLauncher implements RepeaterLauncher { logger.log('Starting the Repeater (%s)...', this.info.version); + this.repeaterId = repeaterId; this.repeaterServer.connect(repeaterId); - this.subscribeToEvents(); - - await this.repeaterServer.deploy( - { - repeaterId - }, - this.getRuntime() - ); - this.repeaterStarted = true; logger.log(`The Repeater (%s) started`, this.info.version); @@ -121,6 +114,14 @@ export class ServerRepeaterLauncher implements RepeaterLauncher { } private subscribeToEvents() { + this.repeaterServer.connected(async () => { + await this.repeaterServer.deploy( + { + repeaterId: this.repeaterId + }, + this.getRuntime() + ); + }); this.repeaterServer.errorOccurred(({ message }) => { logger.error(message); });