Files
flipper/src/dispatcher/server.js
John Knox 6bdbb4f763 Wait 2s before checking for matching devices for connected clients
Summary:
Currently when a client connects, if there's no matching device we know of, it emits an error.

The problem is that there's a race between clients connecting and devices being detected, if the client connects first, then we'll emit this error, even though the device is displayed shortly afterwards.

Fixing this by waiting 2 seconds after a client connects, and then if it's still connected, checking for a matching device. This should be enough time to make this error more reliable.

Reviewed By: passy

Differential Revision: D14126315

fbshipit-source-id: c81b2c6d9a6e0639a656d1a4d7a8f999f715bfbf
2019-02-19 03:46:55 -08:00

94 lines
2.3 KiB
JavaScript

/**
* Copyright 2018-present Facebook.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @format
*/
import Server from '../server.js';
import type {Store} from '../reducers/index.js';
import type {Logger} from '../fb-interfaces/Logger.js';
import type Client from '../Client.js';
import type {UninitializedClient} from '../UninitializedClient';
export default (store: Store, logger: Logger) => {
const server = new Server(logger, store);
server.init();
server.addListener('new-client', (client: Client) => {
store.dispatch({
type: 'NEW_CLIENT',
payload: client,
});
// Wait 2 seconds, and then trigger another event so we can check it's displayed
setTimeout(() => {
store.dispatch({
type: 'NEW_CLIENT_SANITY_CHECK',
payload: client,
});
}, 2000);
});
server.addListener('removed-client', (id: string) => {
store.dispatch({
type: 'CLIENT_REMOVED',
payload: id,
});
store.dispatch({
type: 'CLEAR_PLUGIN_STATE',
payload: {
id,
devicePlugins: new Set([
...store.getState().plugins.devicePlugins.keys(),
]),
},
});
});
server.addListener('error', err => {
const payload: string =
err.code === 'EADDRINUSE'
? "Couldn't start websocket server. Looks like you have multiple copies of Flipper running."
: err.message || 'Unknown error';
store.dispatch({
type: 'SERVER_ERROR',
payload,
});
});
server.addListener('start-client-setup', (client: UninitializedClient) => {
store.dispatch({
type: 'START_CLIENT_SETUP',
payload: client,
});
});
server.addListener(
'finish-client-setup',
(payload: {client: UninitializedClient, deviceId: string}) => {
store.dispatch({
type: 'FINISH_CLIENT_SETUP',
payload: payload,
});
},
);
server.addListener(
'client-setup-error',
(payload: {client: UninitializedClient, error: Error}) => {
store.dispatch({
type: 'CLIENT_SETUP_ERROR',
payload: payload,
});
},
);
if (typeof window !== 'undefined') {
window.addEventListener('beforeunload', () => {
server.close();
});
}
};