@@ -3,7 +3,7 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
33 if ( k2 === undefined ) k2 = k ;
44 var desc = Object . getOwnPropertyDescriptor ( m , k ) ;
55 if ( ! desc || ( "get" in desc ? ! m . __esModule : desc . writable || desc . configurable ) ) {
6- desc = { enumerable : true , get : function ( ) { return m [ k ] ; } } ;
6+ desc = { enumerable : true , get : function ( ) { return m [ k ] ; } } ;
77 }
88 Object . defineProperty ( o , k2 , desc ) ;
99} ) : ( function ( o , m , k , k2 ) {
@@ -82,8 +82,6 @@ function launchEmulator(systemImageApiLevel, target, arch, profile, cores, ramSi
8282 } ,
8383 } ,
8484 } ) ;
85- // Wait a few seconds for emulator process to initialize before polling
86- yield delay ( 5000 ) ;
8785 // wait for emulator to complete booting
8886 yield waitForDevice ( port , emulatorBootTimeout ) ;
8987 yield adb ( port , `shell input keyevent 82` ) ;
@@ -134,11 +132,46 @@ function adb(port, command) {
134132 */
135133function waitForDevice ( port , emulatorBootTimeout ) {
136134 return __awaiter ( this , void 0 , void 0 , function * ( ) {
137- let booted = false ;
138- let attempts = 0 ;
135+ const startTime = Date . now ( ) ;
139136 const retryInterval = 2 ; // retry every 2 seconds
140- const maxAttempts = emulatorBootTimeout / 2 ;
137+ // Step 1: Poll until device appears in ADB
138+ console . log ( 'Waiting for emulator device to connect...' ) ;
139+ let deviceConnected = false ;
140+ while ( ! deviceConnected ) {
141+ const elapsedSeconds = ( Date . now ( ) - startTime ) / 1000 ;
142+ if ( elapsedSeconds > emulatorBootTimeout ) {
143+ throw new Error ( 'Timeout waiting for emulator device to connect.' ) ;
144+ }
145+ try {
146+ // Try to get device state - will fail if device doesn't exist
147+ let state = '' ;
148+ yield exec . exec ( `adb -s emulator-${ port } get-state` , [ ] , {
149+ listeners : {
150+ stdout : ( data ) => {
151+ state += data . toString ( ) ;
152+ } ,
153+ } ,
154+ } ) ;
155+ if ( state . trim ( ) === 'device' ) {
156+ deviceConnected = true ;
157+ }
158+ }
159+ catch ( error ) {
160+ // Device not found yet, keep waiting
161+ console . warn ( error instanceof Error ? error . message : error ) ;
162+ }
163+ if ( ! deviceConnected ) {
164+ yield delay ( retryInterval * 1000 ) ;
165+ }
166+ }
167+ console . log ( 'Device connected. Waiting for boot to complete...' ) ;
168+ // Step 2: Poll for boot completion
169+ let booted = false ;
141170 while ( ! booted ) {
171+ const elapsedSeconds = ( Date . now ( ) - startTime ) / 1000 ;
172+ if ( elapsedSeconds > emulatorBootTimeout ) {
173+ throw new Error ( 'Timeout waiting for emulator to boot.' ) ;
174+ }
142175 try {
143176 let result = '' ;
144177 yield exec . exec ( `adb -s emulator-${ port } shell getprop sys.boot_completed` , [ ] , {
@@ -151,19 +184,14 @@ function waitForDevice(port, emulatorBootTimeout) {
151184 if ( result . trim ( ) === '1' ) {
152185 console . log ( 'Emulator booted.' ) ;
153186 booted = true ;
154- break ;
155187 }
156188 }
157189 catch ( error ) {
158190 console . warn ( error instanceof Error ? error . message : error ) ;
159191 }
160- if ( attempts < maxAttempts ) {
192+ if ( ! booted ) {
161193 yield delay ( retryInterval * 1000 ) ;
162194 }
163- else {
164- throw new Error ( `Timeout waiting for emulator to boot.` ) ;
165- }
166- attempts ++ ;
167195 }
168196 } ) ;
169197}
0 commit comments