Fix adb client use of promise.resolve

Summary: This was misusing promise.resolve by calling it before the result is produced, meaning there was a race condition. It would also resolve as success even if listDevices failed.

Reviewed By: passy

Differential Revision: D13121173

fbshipit-source-id: f120a1b199d1e3168059c537461f65a8dc125a94
This commit is contained in:
John Knox
2018-11-19 09:44:17 -08:00
committed by Facebook Github Bot
parent a6765deec6
commit 018718244a

View File

@@ -55,14 +55,12 @@ function getRunningEmulatorName(id: string): Promise<?string> {
export default (store: Store, logger: Logger) => {
// Using this client before adb server is started up will cause failures.
// safeClient gets around this by waiting for listDevices first, which ensures
// the server is up and running.
const unsafeClient = adb.createClient();
const safeClient = () =>
new Promise((resolve, reject) => {
resolve(unsafeClient.listDevices().then(() => unsafeClient));
});
// this gets around this by waiting for listDevices first, which ensures
// the server is up and running before allowing any other operations.
const clientPromise = (() => {
const unsafeClient = adb.createClient();
return unsafeClient.listDevices().then(() => unsafeClient);
})();
const watchAndroidDevices = () => {
// get emulators
@@ -81,7 +79,7 @@ export default (store: Store, logger: Logger) => {
},
);
safeClient().then(client => {
clientPromise.then(client => {
client
.trackDevices()
.then(tracker => {