diff --git a/package.json b/package.json index 125c8b8c..cd8541f5 100644 --- a/package.json +++ b/package.json @@ -91,6 +91,7 @@ "rimraf": "^2.6.2", "rwlock": "^5.0.0", "shell-quote": "^1.6.1", + "strict-event-emitter-types": "^2.0.0", "ts-loader": "^5.3.0", "typed-error": "^2.0.0", "typescript": "^3.2.2", diff --git a/src/config/index.ts b/src/config/index.ts index e1a21bdf..28be040c 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -3,6 +3,7 @@ import { EventEmitter } from 'events'; import { Transaction } from 'knex'; import * as _ from 'lodash'; import { generateUniqueKey } from 'resin-register-device'; +import StrictEventEmitter from 'strict-event-emitter-types'; import { Either } from 'fp-ts/lib/Either'; import * as t from 'io-ts'; @@ -25,8 +26,19 @@ interface ConfigOpts { } type ConfigMap = { [key in T]: SchemaReturn }; +type ConfigChangeMap = { + [key in T]: SchemaReturn | undefined +}; -export class Config extends EventEmitter { +interface ConfigEvents { + change: ConfigChangeMap; +} + +type ConfigEventEmitter = StrictEventEmitter; + +export class Config extends (EventEmitter as { + new (): ConfigEventEmitter; +}) { private db: DB; private configJsonBackend: ConfigJsonConfigBackend; @@ -173,7 +185,7 @@ export class Config extends EventEmitter { .return(); } }).then(() => { - this.emit('change', keyValues); + this.emit('change', keyValues as ConfigMap); }); } diff --git a/src/local-mode.ts b/src/local-mode.ts index b67ae975..870d4b6f 100644 --- a/src/local-mode.ts +++ b/src/local-mode.ts @@ -3,7 +3,6 @@ import * as Docker from 'dockerode'; import * as _ from 'lodash'; import Config from './config'; -import { SchemaReturn, SchemaTypeKey } from './config/schema-type'; import Database from './db'; import { Logger } from './logger'; @@ -25,23 +24,20 @@ export class LocalModeManager { public async init() { // Setup a listener to catch state changes relating to local mode - this.config.on( - 'change', - (changed: { [key in SchemaTypeKey]: SchemaReturn }) => { - if (changed.localMode != null) { - const localMode = changed.localMode || false; + this.config.on('change', changed => { + if (changed.localMode != null) { + const localMode = changed.localMode || false; - // First switch the logger to it's correct state - this.logger.switchBackend(localMode); + // First switch the logger to it's correct state + this.logger.switchBackend(localMode); - // If we're leaving local mode, make sure to remove all of the - // leftover artifacts - if (!localMode) { - this.removeLocalModeArtifacts(); - } + // If we're leaving local mode, make sure to remove all of the + // leftover artifacts + if (!localMode) { + this.removeLocalModeArtifacts(); } - }, - ); + } + }); // On startup, check if we're in unmanaged mode, // as local mode needs to be set diff --git a/src/supervisor-api.ts b/src/supervisor-api.ts index 2d370c8c..5f2f1cf5 100644 --- a/src/supervisor-api.ts +++ b/src/supervisor-api.ts @@ -4,7 +4,6 @@ import * as _ from 'lodash'; import * as morgan from 'morgan'; import Config from './config'; -import { SchemaReturn, SchemaTypeKey } from './config/schema-type'; import { EventTracker } from './event-tracker'; import blink = require('./lib/blink'); import * as iptables from './lib/iptables'; @@ -147,18 +146,15 @@ export class SupervisorAPI { // Monitor the switching of local mode, and change which interfaces will // be listened to based on that - this.config.on( - 'change', - (changedConfig: { [key in SchemaTypeKey]: SchemaReturn }) => { - if (changedConfig.localMode != null) { - this.applyListeningRules( - changedConfig.localMode || false, - port, - allowedInterfaces, - ); - } - }, - ); + this.config.on('change', changedConfig => { + if (changedConfig.localMode != null) { + this.applyListeningRules( + changedConfig.localMode || false, + port, + allowedInterfaces, + ); + } + }); this.server = this.api.listen(port); this.server.timeout = apiTimeout;