config: Properly type the change events from config module

Change-type: patch
Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
Cameron Diver 2019-01-21 11:11:50 +00:00
parent acf2a6d6b5
commit 0505c0f976
No known key found for this signature in database
GPG Key ID: 49690ED87032539F
4 changed files with 35 additions and 30 deletions

View File

@ -91,6 +91,7 @@
"rimraf": "^2.6.2", "rimraf": "^2.6.2",
"rwlock": "^5.0.0", "rwlock": "^5.0.0",
"shell-quote": "^1.6.1", "shell-quote": "^1.6.1",
"strict-event-emitter-types": "^2.0.0",
"ts-loader": "^5.3.0", "ts-loader": "^5.3.0",
"typed-error": "^2.0.0", "typed-error": "^2.0.0",
"typescript": "^3.2.2", "typescript": "^3.2.2",

View File

@ -3,6 +3,7 @@ import { EventEmitter } from 'events';
import { Transaction } from 'knex'; import { Transaction } from 'knex';
import * as _ from 'lodash'; import * as _ from 'lodash';
import { generateUniqueKey } from 'resin-register-device'; import { generateUniqueKey } from 'resin-register-device';
import StrictEventEmitter from 'strict-event-emitter-types';
import { Either } from 'fp-ts/lib/Either'; import { Either } from 'fp-ts/lib/Either';
import * as t from 'io-ts'; import * as t from 'io-ts';
@ -25,8 +26,19 @@ interface ConfigOpts {
} }
type ConfigMap<T extends SchemaTypeKey> = { [key in T]: SchemaReturn<key> }; type ConfigMap<T extends SchemaTypeKey> = { [key in T]: SchemaReturn<key> };
type ConfigChangeMap<T extends SchemaTypeKey> = {
[key in T]: SchemaReturn<key> | undefined
};
export class Config extends EventEmitter { interface ConfigEvents {
change: ConfigChangeMap<SchemaTypeKey>;
}
type ConfigEventEmitter = StrictEventEmitter<EventEmitter, ConfigEvents>;
export class Config extends (EventEmitter as {
new (): ConfigEventEmitter;
}) {
private db: DB; private db: DB;
private configJsonBackend: ConfigJsonConfigBackend; private configJsonBackend: ConfigJsonConfigBackend;
@ -173,7 +185,7 @@ export class Config extends EventEmitter {
.return(); .return();
} }
}).then(() => { }).then(() => {
this.emit('change', keyValues); this.emit('change', keyValues as ConfigMap<SchemaTypeKey>);
}); });
} }

View File

@ -3,7 +3,6 @@ import * as Docker from 'dockerode';
import * as _ from 'lodash'; import * as _ from 'lodash';
import Config from './config'; import Config from './config';
import { SchemaReturn, SchemaTypeKey } from './config/schema-type';
import Database from './db'; import Database from './db';
import { Logger } from './logger'; import { Logger } from './logger';
@ -25,23 +24,20 @@ export class LocalModeManager {
public async init() { public async init() {
// Setup a listener to catch state changes relating to local mode // Setup a listener to catch state changes relating to local mode
this.config.on( this.config.on('change', changed => {
'change', if (changed.localMode != null) {
(changed: { [key in SchemaTypeKey]: SchemaReturn<key> }) => { const localMode = changed.localMode || false;
if (changed.localMode != null) {
const localMode = changed.localMode || false;
// First switch the logger to it's correct state // First switch the logger to it's correct state
this.logger.switchBackend(localMode); this.logger.switchBackend(localMode);
// If we're leaving local mode, make sure to remove all of the // If we're leaving local mode, make sure to remove all of the
// leftover artifacts // leftover artifacts
if (!localMode) { if (!localMode) {
this.removeLocalModeArtifacts(); this.removeLocalModeArtifacts();
}
} }
}, }
); });
// On startup, check if we're in unmanaged mode, // On startup, check if we're in unmanaged mode,
// as local mode needs to be set // as local mode needs to be set

View File

@ -4,7 +4,6 @@ import * as _ from 'lodash';
import * as morgan from 'morgan'; import * as morgan from 'morgan';
import Config from './config'; import Config from './config';
import { SchemaReturn, SchemaTypeKey } from './config/schema-type';
import { EventTracker } from './event-tracker'; import { EventTracker } from './event-tracker';
import blink = require('./lib/blink'); import blink = require('./lib/blink');
import * as iptables from './lib/iptables'; import * as iptables from './lib/iptables';
@ -147,18 +146,15 @@ export class SupervisorAPI {
// Monitor the switching of local mode, and change which interfaces will // Monitor the switching of local mode, and change which interfaces will
// be listened to based on that // be listened to based on that
this.config.on( this.config.on('change', changedConfig => {
'change', if (changedConfig.localMode != null) {
(changedConfig: { [key in SchemaTypeKey]: SchemaReturn<key> }) => { this.applyListeningRules(
if (changedConfig.localMode != null) { changedConfig.localMode || false,
this.applyListeningRules( port,
changedConfig.localMode || false, allowedInterfaces,
port, );
allowedInterfaces, }
); });
}
},
);
this.server = this.api.listen(port); this.server = this.api.listen(port);
this.server.timeout = apiTimeout; this.server.timeout = apiTimeout;