From 6bdbb4f76391520d0c07915f497c8eed7a926578 Mon Sep 17 00:00:00 2001 From: John Knox Date: Tue, 19 Feb 2019 03:43:07 -0800 Subject: [PATCH] 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 --- src/dispatcher/server.js | 7 +++++++ src/reducers/connections.js | 34 ++++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/dispatcher/server.js b/src/dispatcher/server.js index 882c76ffb..1266b6521 100644 --- a/src/dispatcher/server.js +++ b/src/dispatcher/server.js @@ -21,6 +21,13 @@ export default (store: Store, logger: Logger) => { 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) => { diff --git a/src/reducers/connections.js b/src/reducers/connections.js index 66dfc64bf..d9611a22f 100644 --- a/src/reducers/connections.js +++ b/src/reducers/connections.js @@ -70,6 +70,10 @@ export type Action = type: 'NEW_CLIENT', payload: Client, } + | { + type: 'NEW_CLIENT_SANITY_CHECK', + payload: Client, + } | { type: 'CLIENT_REMOVED', payload: string, @@ -237,16 +241,6 @@ export default function reducer( selectedPlugin = userPreferredPlugin; } - const matchingDeviceForClient = state.devices.filter( - device => payload.query.device_id === device.serial, - ); - if (matchingDeviceForClient.length === 0) { - console.error( - new RecurringError(`Client initialised for non-displayed device`), - payload.id, - ); - } - return { ...state, clients: state.clients.concat(payload), @@ -260,6 +254,26 @@ export default function reducer( selectedPlugin, }; } + case 'NEW_CLIENT_SANITY_CHECK': { + const {payload} = action; + // Check for clients initialised when there is no matching device + const clientIsStillConnected = state.clients.filter( + client => client.id == payload.query.device_id, + ); + if (clientIsStillConnected) { + const matchingDeviceForClient = state.devices.filter( + device => payload.query.device_id === device.serial, + ); + if (matchingDeviceForClient.length === 0) { + console.error( + new RecurringError(`Client initialised for non-displayed device`), + payload.id, + ); + } + } + + return state; + } case 'CLIENT_REMOVED': { const {payload} = action;