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,9 +24,7 @@ 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> }) => {
this.config.on('change', changed => {
if (changed.localMode != null) {
const localMode = changed.localMode || false;
@ -40,8 +37,7 @@ export class LocalModeManager {
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,9 +146,7 @@ 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> }) => {
this.config.on('change', changedConfig => {
if (changedConfig.localMode != null) {
this.applyListeningRules(
changedConfig.localMode || false,
@ -157,8 +154,7 @@ export class SupervisorAPI {
allowedInterfaces,
);
}
},
);
});
this.server = this.api.listen(port);
this.server.timeout = apiTimeout;