From a59af6abe894000cd9cdc43d6963bdfc572e2f2b Mon Sep 17 00:00:00 2001 From: Pagan Gazzard Date: Wed, 8 Apr 2020 17:03:07 +0100 Subject: [PATCH] Workaround a circular dependency Change-type: patch --- src/api-binder.ts | 13 +++++-------- src/supervisor.ts | 3 ++- test/11-api-binder.spec.ts | 4 +++- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/api-binder.ts b/src/api-binder.ts index b877a7f0..43d7c8bd 100644 --- a/src/api-binder.ts +++ b/src/api-binder.ts @@ -44,7 +44,6 @@ export interface APIBinderConstructOpts { config: Config; // FIXME: Remove this db: Database; - deviceState: DeviceState; eventTracker: EventTracker; logger: Logger; } @@ -94,20 +93,18 @@ export class APIBinder { private targetStateFetchErrors = 0; private readyForUpdates = false; - public constructor({ - config, - deviceState, - eventTracker, - logger, - }: APIBinderConstructOpts) { + public constructor({ config, eventTracker, logger }: APIBinderConstructOpts) { this.config = config; - this.deviceState = deviceState; this.eventTracker = eventTracker; this.logger = logger; this.router = this.createAPIBinderRouter(this); } + public setDeviceState(deviceState: DeviceState) { + this.deviceState = deviceState; + } + public async healthcheck() { const { appUpdatePollInterval, diff --git a/src/supervisor.ts b/src/supervisor.ts index b2f150cb..b84ec8de 100644 --- a/src/supervisor.ts +++ b/src/supervisor.ts @@ -43,7 +43,6 @@ export class Supervisor { this.apiBinder = new APIBinder({ config: this.config, db: this.db, - deviceState: this.deviceState, eventTracker: this.eventTracker, logger: this.logger, }); @@ -54,6 +53,8 @@ export class Supervisor { logger: this.logger, apiBinder: this.apiBinder, }); + // workaround the circular dependency + this.apiBinder.setDeviceState(this.deviceState); // FIXME: rearchitect proxyvisor to avoid this circular dependency // by storing current state and having the APIBinder query and report it / provision devices diff --git a/test/11-api-binder.spec.ts b/test/11-api-binder.spec.ts index 1c2b6afd..83269c58 100644 --- a/test/11-api-binder.spec.ts +++ b/test/11-api-binder.spec.ts @@ -34,7 +34,6 @@ const initModels = async (obj: Dictionary, filename: string) => { config: obj.config, logger: obj.logger, eventTracker: obj.eventTracker, - deviceState: obj.deviceState, }); obj.deviceState = new DeviceState({ @@ -44,6 +43,9 @@ const initModels = async (obj: Dictionary, filename: string) => { logger: obj.logger, apiBinder: obj.apiBinder, }); + + obj.apiBinder.setDeviceState(obj.deviceState); + await obj.db.init(); await obj.config.init(); await obj.apiBinder.initClient(); // Initializes the clients but doesn't trigger provisioning