@@ -10,6 +10,7 @@ import { CONNECTED_STATUS } from "../../constants";
10
10
import { isInteractive } from "../../helpers" ;
11
11
import { DebugCommandErrors } from "../../../constants" ;
12
12
import { performanceLog } from "../../decorators" ;
13
+ import { Timers } from "../../timers" ;
13
14
14
15
export class DevicesService extends EventEmitter implements Mobile . IDevicesService {
15
16
private static DEVICE_LOOKING_INTERVAL = 200 ;
@@ -22,7 +23,8 @@ export class DevicesService extends EventEmitter implements Mobile.IDevicesServi
22
23
private _data : Mobile . IDevicesServicesInitializationOptions ;
23
24
private _otherDeviceDiscoveries : Mobile . IDeviceDiscovery [ ] = [ ] ;
24
25
private _allDeviceDiscoveries : Mobile . IDeviceDiscovery [ ] = [ ] ;
25
- private deviceDetectionIntervals : NodeJS . Timer [ ] = [ ] ;
26
+ private deviceDetectionInterval : NodeJS . Timer ;
27
+ private emulatorDetectionInterval : NodeJS . Timer ;
26
28
27
29
constructor ( private $logger : ILogger ,
28
30
private $errors : IErrors ,
@@ -37,12 +39,12 @@ export class DevicesService extends EventEmitter implements Mobile.IDevicesServi
37
39
private $injector : IInjector ,
38
40
private $options : IOptions ,
39
41
private $androidProcessService : Mobile . IAndroidProcessService ,
40
- private $processService : IProcessService ,
41
42
private $iOSEmulatorServices : Mobile . IiOSSimulatorService ,
42
43
private $androidEmulatorServices : Mobile . IEmulatorPlatformService ,
43
44
private $androidEmulatorDiscovery : Mobile . IDeviceDiscovery ,
44
45
private $emulatorHelper : Mobile . IEmulatorHelper ,
45
- private $prompter : IPrompter ) {
46
+ private $prompter : IPrompter ,
47
+ private $timers : Timers ) {
46
48
super ( ) ;
47
49
this . attachToKnownDeviceDiscoveryEvents ( ) ;
48
50
this . attachToKnownEmulatorDiscoveryEvents ( ) ;
@@ -261,7 +263,7 @@ export class DevicesService extends EventEmitter implements Mobile.IDevicesServi
261
263
/**
262
264
* Starts looking for devices. Any found devices are pushed to "_devices" variable.
263
265
*/
264
- protected async detectCurrentlyAttachedDevices ( deviceInitOpts ?: Mobile . IDevicesServicesInitializationOptions ) : Promise < void > {
266
+ protected async detectCurrentlyAttachedDevices ( deviceInitOpts ?: Mobile . IDeviceLookingOptions ) : Promise < void > {
265
267
const options = this . getDeviceLookingOptions ( deviceInitOpts ) ;
266
268
267
269
for ( const deviceDiscovery of this . _allDeviceDiscoveries ) {
@@ -277,7 +279,7 @@ export class DevicesService extends EventEmitter implements Mobile.IDevicesServi
277
279
try {
278
280
await this . $androidEmulatorDiscovery . startLookingForDevices ( ) ;
279
281
} catch ( err ) {
280
- this . $logger . trace ( `Error while checking for android emulators. ${ err } ` ) ;
282
+ this . $logger . trace ( `Error while checking for Android emulators. ${ err } ` ) ;
281
283
}
282
284
283
285
try {
@@ -287,51 +289,63 @@ export class DevicesService extends EventEmitter implements Mobile.IDevicesServi
287
289
}
288
290
}
289
291
290
- protected async startDeviceDetectionIntervals ( deviceInitOpts : Mobile . IDevicesServicesInitializationOptions = { } ) : Promise < void > {
291
- this . $processService . attachToProcessExitSignals ( this , this . clearDeviceDetectionInterval ) ;
292
-
293
- if ( this . deviceDetectionIntervals . length ) {
294
- this . $logger . trace ( "Device detection intervals are already started. New intervals will not be started." ) ;
295
- return ;
296
- }
292
+ @exported ( "devicesService" )
293
+ public startDeviceDetectionInterval ( deviceInitOpts : Mobile . IDeviceLookingOptions = < any > { } ) : void {
294
+ if ( ! this . deviceDetectionInterval ) {
295
+ let isDeviceDetectionIntervalInProgress = false ;
297
296
298
- let isDeviceDetectionIntervalInProgress = false ;
299
- const deviceDetectionInterval = setInterval ( async ( ) => {
300
- if ( isDeviceDetectionIntervalInProgress ) {
301
- return ;
302
- }
297
+ this . deviceDetectionInterval = this . $timers . setInterval ( async ( ) => {
298
+ if ( isDeviceDetectionIntervalInProgress ) {
299
+ return ;
300
+ }
303
301
304
- isDeviceDetectionIntervalInProgress = true ;
302
+ isDeviceDetectionIntervalInProgress = true ;
305
303
306
- await this . detectCurrentlyAttachedDevices ( deviceInitOpts ) ;
304
+ await this . detectCurrentlyAttachedDevices ( deviceInitOpts ) ;
307
305
308
- try {
309
- const trustedDevices = _ . filter ( this . _devices , device => device . deviceInfo . status === constants . CONNECTED_STATUS ) ;
310
- await settlePromises ( _ . map ( trustedDevices , device => device . applicationManager . checkForApplicationUpdates ( ) ) ) ;
311
- } catch ( err ) {
312
- this . $logger . trace ( "Error checking for application updates on devices." , err ) ;
313
- }
306
+ try {
307
+ const trustedDevices = _ . filter ( this . _devices , device => device . deviceInfo . status === constants . CONNECTED_STATUS ) ;
308
+ await settlePromises ( _ . map ( trustedDevices , device => device . applicationManager . checkForApplicationUpdates ( ) ) ) ;
309
+ } catch ( err ) {
310
+ this . $logger . trace ( "Error checking for application updates on devices." , err ) ;
311
+ }
314
312
315
- isDeviceDetectionIntervalInProgress = false ;
313
+ isDeviceDetectionIntervalInProgress = false ;
316
314
317
- } , DevicesService . DEVICE_LOOKING_INTERVAL ) ;
315
+ } , deviceInitOpts . detectionInterval || DevicesService . DEVICE_LOOKING_INTERVAL ) ;
318
316
319
- deviceDetectionInterval . unref ( ) ;
320
- this . deviceDetectionIntervals . push ( deviceDetectionInterval ) ;
317
+ this . deviceDetectionInterval . unref ( ) ;
318
+ }
319
+ }
321
320
321
+ @exported ( "devicesService" )
322
+ public startEmulatorDetectionInterval ( opts : Mobile . IHasDetectionInterval = { } ) : void {
322
323
let isEmulatorDetectionIntervalRunning = false ;
323
- const emulatorDetectionInterval = setInterval ( async ( ) => {
324
+ this . emulatorDetectionInterval = this . $timers . setInterval ( async ( ) => {
324
325
if ( isEmulatorDetectionIntervalRunning ) {
325
326
return ;
326
327
}
327
328
328
329
isEmulatorDetectionIntervalRunning = true ;
329
330
await this . detectCurrentlyAvailableEmulators ( ) ;
330
331
isEmulatorDetectionIntervalRunning = false ;
331
- } , DevicesService . EMULATOR_IMAGES_DETECTION_INTERVAL ) ;
332
+ } , opts . detectionInterval || DevicesService . EMULATOR_IMAGES_DETECTION_INTERVAL ) ;
332
333
333
- emulatorDetectionInterval . unref ( ) ;
334
- this . deviceDetectionIntervals . push ( emulatorDetectionInterval ) ;
334
+ this . emulatorDetectionInterval . unref ( ) ;
335
+ }
336
+
337
+ @exported ( "devicesService" )
338
+ public stopDeviceDetectionInterval ( ) : void {
339
+ if ( this . deviceDetectionInterval ) {
340
+ clearInterval ( this . deviceDetectionInterval ) ;
341
+ }
342
+ }
343
+
344
+ @exported ( "devicesService" )
345
+ public stopEmulatorDetectionInterval ( ) : void {
346
+ if ( this . emulatorDetectionInterval ) {
347
+ clearInterval ( this . emulatorDetectionInterval ) ;
348
+ }
335
349
}
336
350
337
351
/**
@@ -356,7 +370,7 @@ export class DevicesService extends EventEmitter implements Mobile.IDevicesServi
356
370
/**
357
371
* Starts looking for running devices. All found devices are pushed to _devices variable.
358
372
*/
359
- private async startLookingForDevices ( deviceInitOpts ?: Mobile . IDevicesServicesInitializationOptions ) : Promise < void > {
373
+ private async startLookingForDevices ( deviceInitOpts ?: Mobile . IDeviceLookingOptions ) : Promise < void > {
360
374
this . $logger . trace ( "startLookingForDevices; platform is %s" , this . _platform ) ;
361
375
362
376
if ( this . _platform ) {
@@ -366,7 +380,7 @@ export class DevicesService extends EventEmitter implements Mobile.IDevicesServi
366
380
await this . detectCurrentlyAttachedDevices ( deviceInitOpts ) ;
367
381
await this . detectCurrentlyAvailableEmulators ( ) ;
368
382
369
- await this . startDeviceDetectionIntervals ( deviceInitOpts ) ;
383
+ await this . startDeviceDetectionInterval ( deviceInitOpts ) ;
370
384
}
371
385
372
386
/**
@@ -517,7 +531,7 @@ export class DevicesService extends EventEmitter implements Mobile.IDevicesServi
517
531
// are there any running devices
518
532
this . _platform = deviceInitOpts . platform ;
519
533
try {
520
- await this . startLookingForDevices ( deviceInitOpts ) ;
534
+ await this . startLookingForDevices ( < Mobile . IDeviceLookingOptions > deviceInitOpts ) ;
521
535
} catch ( err ) {
522
536
this . $logger . trace ( "Error while checking for devices." , err ) ;
523
537
}
@@ -606,33 +620,39 @@ export class DevicesService extends EventEmitter implements Mobile.IDevicesServi
606
620
607
621
const platform = deviceInitOpts . platform ;
608
622
const deviceOption = deviceInitOpts . deviceId ;
623
+ const deviceLookingOptions : Mobile . IDeviceLookingOptions = {
624
+ emulator : deviceInitOpts . emulator ,
625
+ platform : deviceInitOpts . platform ,
626
+ shouldReturnImmediateResult : deviceInitOpts . shouldReturnImmediateResult ,
627
+ detectionInterval : deviceInitOpts . detectionInterval
628
+ } ;
609
629
610
630
if ( platform && deviceOption ) {
611
631
this . _platform = this . $mobileHelper . validatePlatformName ( deviceInitOpts . platform ) ;
612
- await this . startLookingForDevices ( deviceInitOpts ) ;
632
+ await this . startLookingForDevices ( deviceLookingOptions ) ;
613
633
this . _device = await this . getDevice ( deviceOption ) ;
614
634
if ( this . _device . deviceInfo . platform !== this . _platform ) {
615
635
this . $errors . fail ( constants . ERROR_CANNOT_RESOLVE_DEVICE ) ;
616
636
}
617
637
this . $logger . warn ( "Your application will be deployed only on the device specified by the provided index or identifier." ) ;
618
638
} else if ( ! platform && deviceOption ) {
619
- await this . startLookingForDevices ( deviceInitOpts ) ;
639
+ await this . startLookingForDevices ( deviceLookingOptions ) ;
620
640
this . _device = await this . getDevice ( deviceOption ) ;
621
641
this . _platform = this . _device . deviceInfo . platform ;
622
642
} else if ( platform && ! deviceOption ) {
623
643
this . _platform = this . $mobileHelper . validatePlatformName ( platform ) ;
624
- await this . startLookingForDevices ( deviceInitOpts ) ;
644
+ await this . startLookingForDevices ( deviceLookingOptions ) ;
625
645
} else {
626
646
// platform and deviceId are not specified
627
647
if ( deviceInitOpts . skipInferPlatform ) {
628
648
if ( deviceInitOpts . skipDeviceDetectionInterval ) {
629
- await this . detectCurrentlyAttachedDevices ( deviceInitOpts ) ;
649
+ await this . detectCurrentlyAttachedDevices ( deviceLookingOptions ) ;
630
650
} else {
631
651
deviceInitOpts . shouldReturnImmediateResult = true ;
632
- await this . startLookingForDevices ( deviceInitOpts ) ;
652
+ await this . startLookingForDevices ( deviceLookingOptions ) ;
633
653
}
634
654
} else {
635
- await this . startLookingForDevices ( deviceInitOpts ) ;
655
+ await this . startLookingForDevices ( deviceLookingOptions ) ;
636
656
637
657
const devices = this . getDeviceInstances ( ) ;
638
658
const platforms = _ ( devices )
@@ -700,18 +720,6 @@ export class DevicesService extends EventEmitter implements Mobile.IDevicesServi
700
720
return debuggableViewsPerApp && debuggableViewsPerApp [ appIdentifier ] ;
701
721
}
702
722
703
- private clearDeviceDetectionInterval ( ) : void {
704
- if ( this . deviceDetectionIntervals . length ) {
705
- for ( const interval of this . deviceDetectionIntervals ) {
706
- clearInterval ( interval ) ;
707
- }
708
-
709
- this . deviceDetectionIntervals . splice ( 0 , this . deviceDetectionIntervals . length ) ;
710
- } else {
711
- this . $logger . trace ( "Device detection intervals are not started, so it cannot be stopped." ) ;
712
- }
713
- }
714
-
715
723
private getDebuggableAppsCore ( deviceIdentifier : string ) : Promise < Mobile . IDeviceApplicationInformation [ ] > {
716
724
const device = this . getDeviceByIdentifier ( deviceIdentifier ) ;
717
725
return device . applicationManager . getDebuggableApps ( ) ;
0 commit comments