Fixed some issues with too many devices showing up in the sidebar

Summary:
Device management was inconsistent so far, this diff addresses the following issues

* pending a subtle timing issue, a physical android device might also show up as emulator, so effectively the device would be shown twice, but with the same content
* Metro devices now behave more like the android devices: offline devices are replaced if it comes online again
* Generalized this logic; the reducer now forces serials to be unique
* Fixed issue where a Metro device that disconnected due to a connection failure would be archived twice
* Use the metro connection url as serial, to have a slightly more future proof serial

Reviewed By: jknoxville

Differential Revision: D19996385

fbshipit-source-id: 0f6e3ddc6444542553d25cc3b592591652d688f2
This commit is contained in:
Michel Weststrate
2020-02-20 12:54:31 -08:00
committed by Facebook Github Bot
parent c262ab4e14
commit 64b06ba6f6
4 changed files with 78 additions and 12 deletions

View File

@@ -20,7 +20,6 @@ const METRO_URL = `http://${METRO_HOST}:${METRO_PORT}`;
const METRO_LOGS_ENDPOINT = `ws://${METRO_HOST}:${METRO_PORT}/events`;
const METRO_MESSAGE = ['React Native packager is running', 'Metro is running'];
const QUERY_INTERVAL = 5000;
const METRO_DEVICE_ID = 'metro'; // there is always only one activve
async function isMetroRunning(): Promise<boolean> {
return new Promise(resolve => {
@@ -50,7 +49,7 @@ async function registerDevice(
store: Store,
logger: Logger,
) {
const metroDevice = new MetroDevice(METRO_DEVICE_ID, ws);
const metroDevice = new MetroDevice(METRO_URL, ws);
logger.track('usage', 'register-device', {
os: 'Metro',
name: metroDevice.title,
@@ -60,7 +59,7 @@ async function registerDevice(
store.dispatch({
type: 'REGISTER_DEVICE',
payload: metroDevice,
serial: METRO_DEVICE_ID,
serial: METRO_URL,
});
registerDeviceCallbackOnPlugins(
@@ -74,20 +73,20 @@ async function registerDevice(
async function unregisterDevices(store: Store, logger: Logger) {
logger.track('usage', 'unregister-device', {
os: 'Metro',
serial: METRO_DEVICE_ID,
serial: METRO_URL,
});
let archivedDevice: ArchivedDevice | undefined = undefined;
const device = store
.getState()
.connections.devices.find(device => device.serial === METRO_DEVICE_ID);
.connections.devices.find(device => device.serial === METRO_URL);
if (device && !device.isArchived) {
archivedDevice = device.archive();
}
store.dispatch({
type: 'UNREGISTER_DEVICES',
payload: new Set([METRO_DEVICE_ID]),
payload: new Set([METRO_URL]),
});
if (archivedDevice) {
@@ -102,6 +101,7 @@ async function unregisterDevices(store: Store, logger: Logger) {
export default (store: Store, logger: Logger) => {
let timeoutHandle: NodeJS.Timeout;
let ws: WebSocket | undefined;
let unregistered = false;
async function tryConnectToMetro() {
if (ws) {
@@ -118,10 +118,13 @@ export default (store: Store, logger: Logger) => {
};
_ws.onclose = _ws.onerror = () => {
clearTimeout(guard);
ws = undefined;
unregisterDevices(store, logger);
scheduleNext();
if (!unregistered) {
unregistered = true;
clearTimeout(guard);
ws = undefined;
unregisterDevices(store, logger);
scheduleNext();
}
};
const guard = setTimeout(() => {