@@ -2,7 +2,7 @@ import * as stream from 'stream';
22import * as path from 'path' ;
33import * as adb from '@devicefarmer/adbkit' ;
44import { reportError } from '../../error-tracking' ;
5- import { delay } from '../../util' ;
5+ import { delay , waitUntil } from '../../util' ;
66import { getCertificateFingerprint , parseCert } from '../../certificates' ;
77
88export const ANDROID_TEMP = '/data/local/tmp' ;
@@ -75,18 +75,7 @@ export const getConnectedDevices = batchCalls(async (adbClient: adb.AdbClient) =
7575 throw e ;
7676 }
7777 }
78- } ) ;
79-
80- async function waitUntilAvailable ( adbClient : adb . AdbClient , deviceId : string , tries : number ) {
81- delay ( 200 ) ;
82-
83- while ( tries > 0 && ! ( await getConnectedDevices ( adbClient ) ) . includes ( deviceId ) ) {
84- tries = tries - 1 ;
85- await delay ( 500 ) ;
86- }
87-
88- if ( tries <= 0 ) throw new Error ( `Device ${ deviceId } not available via ADB` ) ;
89- }
78+ } )
9079
9180export function stringAsStream ( input : string ) {
9281 const contentStream = new stream . Readable ( ) ;
@@ -148,9 +137,11 @@ export async function getRootCommand(adbClient: adb.AdbClient, deviceId: string)
148137
149138 // Sometimes switching to root can disconnect ADB devices, so double-check
150139 // they're still here, and wait a few seconds for them to come back if not.
151- await waitUntilAvailable ( adbClient , deviceId , 10 ) ;
152140
153- const whoami = await run ( adbClient , deviceId , [ 'whoami' ] ) . catch ( console . log ) ;
141+ await delay ( 500 ) ; // Wait, since they may not disconnect immediately
142+ const whoami = await waitUntil ( 250 , 10 , ( ) : Promise < string | false > => {
143+ return run ( adbClient , deviceId , [ 'whoami' ] ) . catch ( ( ) => false )
144+ } ) . catch ( console . log ) ;
154145
155146 return ( whoami || '' ) . trim ( ) === 'root'
156147 ? [ ] // all commands now run as root, so no prefix required.
0 commit comments