From b5a750935deb1d6d1e5aa13bffe13792455c68e3 Mon Sep 17 00:00:00 2001 From: Andrey Goncharov Date: Thu, 13 Oct 2022 03:20:51 -0700 Subject: [PATCH] Populate the initial list of connected devices on startup Summary: When we start using Flipper from the CLI for automation, we want to have the list of currently connected devices as soon as flipper server is alive. Otherwise, we would have to poll flipper server waiting for some magical moment in the future when devices connect. This way, we can be certain that if flipper server started, but we do not see a device, it is not connected at all. Reviewed By: mweststrate Differential Revision: D40184106 fbshipit-source-id: ce4c2b897a2df0081e3a0b6a8c26640599e0f9e8 --- .../src/FlipperServerImpl.tsx | 6 +++- .../devices/android/androidDeviceManager.tsx | 36 ++++++++++++++++--- .../src/devices/ios/iOSDeviceManager.tsx | 2 +- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/desktop/flipper-server-core/src/FlipperServerImpl.tsx b/desktop/flipper-server-core/src/FlipperServerImpl.tsx index 0068a70b1..9ecb113e5 100644 --- a/desktop/flipper-server-core/src/FlipperServerImpl.tsx +++ b/desktop/flipper-server-core/src/FlipperServerImpl.tsx @@ -198,7 +198,7 @@ export class FlipperServerImpl implements FlipperServer { return; } this.android = new AndroidDeviceManager(this, adbClient); - return this.android.watchAndroidDevices(); + return this.android.watchAndroidDevices(true); }) .catch((e) => { console.error( @@ -588,6 +588,10 @@ export class FlipperServerImpl implements FlipperServer { return device; } + hasDevice(serial: string): boolean { + return !!this.devices.get(serial); + } + getDeviceSerials(): string[] { return Array.from(this.devices.keys()); } diff --git a/desktop/flipper-server-core/src/devices/android/androidDeviceManager.tsx b/desktop/flipper-server-core/src/devices/android/androidDeviceManager.tsx index b42d71c75..14789f4d9 100644 --- a/desktop/flipper-server-core/src/devices/android/androidDeviceManager.tsx +++ b/desktop/flipper-server-core/src/devices/android/androidDeviceManager.tsx @@ -191,7 +191,24 @@ export class AndroidDeviceManager { }); } - async watchAndroidDevices() { + async watchAndroidDevices(initialRun = false) { + if (initialRun) { + try { + const devices = await this.adbClient.listDevices(); + for (const device of devices) { + if (device.type !== 'offline') { + this.registerDevice(this.adbClient, device); + } else { + this.handleOfflineDevice(device); + } + } + } catch (e) { + console.warn( + `Failed to populate the initial list of android devices: ${e.message}`, + ); + } + } + try { this.adbClient .trackDevices() @@ -214,12 +231,17 @@ export class AndroidDeviceManager { }); tracker.on('add', async (device) => { + // Check if we have already registered this device during the `initialRun` + if (this.flipperServer.hasDevice(device.id)) { + console.debug( + `[conn] Trying to add an existing Android device ${device.id}. Skipping.`, + ); + return; + } if (device.type !== 'offline') { this.registerDevice(this.adbClient, device); } else { - console.warn( - `[conn] Found device ${device.id}, but it has status offline. If this concerns an emulator and the problem persists, try these potential solutions: https://stackoverflow.com/a/21330228/1983583, https://stackoverflow.com/a/56053223/1983583`, - ); + this.handleOfflineDevice(device); } }); @@ -247,6 +269,12 @@ export class AndroidDeviceManager { } } + private handleOfflineDevice(device: Device): void { + console.warn( + `[conn] Found device ${device.id}, but it has status offline. If this concerns an emulator and the problem persists, try these potential solutions: https://stackoverflow.com/a/21330228/1983583, https://stackoverflow.com/a/56053223/1983583`, + ); + } + private async registerDevice(adbClient: ADBClient, deviceData: Device) { const androidDevice = await this.createDevice(adbClient, deviceData); if (!androidDevice) { diff --git a/desktop/flipper-server-core/src/devices/ios/iOSDeviceManager.tsx b/desktop/flipper-server-core/src/devices/ios/iOSDeviceManager.tsx index 3dae9652c..8fa5e7627 100644 --- a/desktop/flipper-server-core/src/devices/ios/iOSDeviceManager.tsx +++ b/desktop/flipper-server-core/src/devices/ios/iOSDeviceManager.tsx @@ -149,7 +149,7 @@ export class IOSDeviceManager { isDetected, this.idbConfig.enablePhysicalIOS, ); - this.queryDevicesForever(bridge); + await this.queryDevicesForever(bridge); } catch (err) { // This case is expected if both Xcode and idb are missing. if (err.message === ERR_NO_IDB_OR_XCODE_AVAILABLE) {