@@ -82,6 +82,10 @@ export class Android {
8282 }
8383 return [ ...this . _devices . values ( ) ] ;
8484 }
85+
86+ _deviceClosed ( device : AndroidDevice ) {
87+ this . _devices . delete ( device . serial ) ;
88+ }
8589}
8690
8791export class AndroidDevice extends EventEmitter {
@@ -98,12 +102,16 @@ export class AndroidDevice extends EventEmitter {
98102 static Events = {
99103 WebViewAdded : 'webViewAdded' ,
100104 WebViewRemoved : 'webViewRemoved' ,
105+ Closed : 'closed'
101106 } ;
102107
103108 private _browserConnections = new Set < AndroidBrowser > ( ) ;
109+ private _android : Android ;
110+ private _isClosed = false ;
104111
105112 constructor ( android : Android , backend : DeviceBackend , model : string ) {
106113 super ( ) ;
114+ this . _android = android ;
107115 this . _backend = backend ;
108116 this . model = model ;
109117 this . serial = backend . serial ;
@@ -202,6 +210,7 @@ export class AndroidDevice extends EventEmitter {
202210 }
203211
204212 async close ( ) {
213+ this . _isClosed = true ;
205214 if ( this . _pollingWebViews )
206215 clearTimeout ( this . _pollingWebViews ) ;
207216 for ( const connection of this . _browserConnections )
@@ -211,6 +220,8 @@ export class AndroidDevice extends EventEmitter {
211220 driver . close ( ) ;
212221 }
213222 await this . _backend . close ( ) ;
223+ this . _android . _deviceClosed ( this ) ;
224+ this . emit ( AndroidDevice . Events . Closed ) ;
214225 }
215226
216227 async launchBrowser ( pkg : string = 'com.android.chrome' , options : types . BrowserContextOptions = { } ) : Promise < BrowserContext > {
@@ -234,11 +245,11 @@ export class AndroidDevice extends EventEmitter {
234245 return await this . _connectToBrowser ( socketName , options ) ;
235246 }
236247
237- connectToWebView ( pid : number ) : Promise < BrowserContext > {
248+ async connectToWebView ( pid : number ) : Promise < BrowserContext > {
238249 const webView = this . _webViews . get ( pid ) ;
239250 if ( ! webView )
240251 throw new Error ( 'WebView has been closed' ) ;
241- return this . _connectToBrowser ( `webview_devtools_remote_${ pid } ` ) ;
252+ return await this . _connectToBrowser ( `webview_devtools_remote_${ pid } ` ) ;
242253 }
243254
244255 private async _connectToBrowser ( socketName : string , options : types . BrowserContextOptions = { } ) : Promise < BrowserContext > {
@@ -272,6 +283,8 @@ export class AndroidDevice extends EventEmitter {
272283
273284 private async _refreshWebViews ( ) {
274285 const sockets = ( await this . _backend . runCommand ( `shell:cat /proc/net/unix | grep webview_devtools_remote` ) ) . split ( '\n' ) ;
286+ if ( this . _isClosed )
287+ return ;
275288
276289 const newPids = new Set < number > ( ) ;
277290 for ( const line of sockets ) {
@@ -286,6 +299,8 @@ export class AndroidDevice extends EventEmitter {
286299 continue ;
287300
288301 const procs = ( await this . _backend . runCommand ( `shell:ps -A | grep ${ pid } ` ) ) . split ( '\n' ) ;
302+ if ( this . _isClosed )
303+ return ;
289304 let pkg = '' ;
290305 for ( const proc of procs ) {
291306 const match = proc . match ( / [ ^ \s ] + \s + ( \d + ) .* $ / ) ;
0 commit comments