Move async work out of the Server constructor
Summary: I feel like doing async stuff here isn't a good idea in general. But more pressingly, it means you can't immediately call server.close() after new Server(), because the things to close haven't been created yet. Reviewed By: danielbuechele Differential Revision: D10488301 fbshipit-source-id: 76ebe91e0c09f353e0bdb9f2e4116757e757abb2
This commit is contained in:
committed by
Facebook Github Bot
parent
086ab0188b
commit
764cdfe127
@@ -24,6 +24,7 @@ beforeAll(() => {
|
||||
}
|
||||
|
||||
server = new Server(new LogManager(), mockStore);
|
||||
return server.init();
|
||||
});
|
||||
|
||||
test('servers starting at ports', done => {
|
||||
|
||||
@@ -13,6 +13,7 @@ import type Client from '../Client.js';
|
||||
|
||||
export default (store: Store, logger: Logger) => {
|
||||
const server = new Server(logger, store);
|
||||
server.init();
|
||||
|
||||
server.addListener('new-client', (client: Client) => {
|
||||
store.dispatch({
|
||||
|
||||
@@ -44,6 +44,7 @@ export default class Server extends EventEmitter {
|
||||
connectionTracker: ConnectionTracker;
|
||||
logger: Logger;
|
||||
store: Store;
|
||||
initialisePromise: Promise<void>;
|
||||
|
||||
constructor(logger: Logger, store: Store) {
|
||||
super();
|
||||
@@ -52,7 +53,6 @@ export default class Server extends EventEmitter {
|
||||
this.certificateProvider = new CertificateProvider(this, logger);
|
||||
this.connectionTracker = new ConnectionTracker(logger);
|
||||
this.store = store;
|
||||
this.init();
|
||||
}
|
||||
|
||||
on: ((event: 'new-client', callback: (client: Client) => void) => void) &
|
||||
@@ -60,12 +60,16 @@ export default class Server extends EventEmitter {
|
||||
((event: 'clients-change', callback: () => void) => void);
|
||||
|
||||
init() {
|
||||
this.certificateProvider
|
||||
this.initialisePromise = this.certificateProvider
|
||||
.loadSecureServerConfig()
|
||||
.then(
|
||||
options => (this.secureServer = this.startServer(SECURE_PORT, options)),
|
||||
);
|
||||
this.insecureServer = this.startServer(INSECURE_PORT);
|
||||
)
|
||||
.then(() => {
|
||||
this.insecureServer = this.startServer(INSECURE_PORT);
|
||||
return;
|
||||
});
|
||||
return this.initialisePromise;
|
||||
}
|
||||
|
||||
startServer(port: number, sslConfig?: SecureServerConfig) {
|
||||
@@ -232,9 +236,14 @@ export default class Server extends EventEmitter {
|
||||
};
|
||||
};
|
||||
|
||||
close() {
|
||||
this.secureServer.stop();
|
||||
this.insecureServer.stop();
|
||||
close(): Promise<void> {
|
||||
if (this.initialisePromise) {
|
||||
return this.initialisePromise.then(_ => {
|
||||
this.secureServer.stop();
|
||||
this.insecureServer.stop();
|
||||
});
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
|
||||
Reference in New Issue
Block a user