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",
"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",

View File

@ -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<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 configJsonBackend: ConfigJsonConfigBackend;
@ -173,7 +185,7 @@ export class Config extends EventEmitter {
.return();
}
}).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 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<key> }) => {
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

View File

@ -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<key> }) => {
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;