@@ -2,7 +2,7 @@ import * as stream from 'stream';
2
2
import * as path from 'path' ;
3
3
import * as adb from '@devicefarmer/adbkit' ;
4
4
import { reportError } from '../../error-tracking' ;
5
- import { delay } from '../../util' ;
5
+ import { delay , waitUntil } from '../../util' ;
6
6
import { getCertificateFingerprint , parseCert } from '../../certificates' ;
7
7
8
8
export const ANDROID_TEMP = '/data/local/tmp' ;
@@ -75,18 +75,7 @@ export const getConnectedDevices = batchCalls(async (adbClient: adb.AdbClient) =
75
75
throw e ;
76
76
}
77
77
}
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
+ } )
90
79
91
80
export function stringAsStream ( input : string ) {
92
81
const contentStream = new stream . Readable ( ) ;
@@ -148,9 +137,11 @@ export async function getRootCommand(adbClient: adb.AdbClient, deviceId: string)
148
137
149
138
// Sometimes switching to root can disconnect ADB devices, so double-check
150
139
// they're still here, and wait a few seconds for them to come back if not.
151
- await waitUntilAvailable ( adbClient , deviceId , 10 ) ;
152
140
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 ) ;
154
145
155
146
return ( whoami || '' ) . trim ( ) === 'root'
156
147
? [ ] // all commands now run as root, so no prefix required.
0 commit comments