1- import { EventEmitter } from "events" ;
2- import { LiveSyncServiceResolver } from "../resolvers/livesync-service-resolver" ;
31import { HmrConstants , DeviceDiscoveryEventNames } from "../common/constants" ;
42import { PREPARE_READY_EVENT_NAME , TrackActionNames } from "../constants" ;
5- import { cache } from "../common/decorators" ;
3+ import { cache , performanceLog } from "../common/decorators" ;
64
7- export class RunController extends EventEmitter {
5+ export class RunController implements IRunController {
86 private processesInfo : IDictionary < IRunOnDeviceProcessInfo > = { } ;
97
108 constructor (
119 private $analyticsService : IAnalyticsService ,
1210 private $buildDataService : IBuildDataService ,
1311 private $buildController : IBuildController ,
14- private $deviceDebugAppService : IDeviceDebugAppService ,
1512 private $deviceInstallAppService : IDeviceInstallAppService ,
16- private $deviceRefreshAppService : IDeviceRefreshAppService ,
17- private $devicesService : Mobile . IDevicesService ,
18- private $errors : IErrors ,
13+ protected $devicesService : Mobile . IDevicesService ,
14+ protected $errors : IErrors ,
1915 private $hmrStatusService : IHmrStatusService ,
2016 public $hooksService : IHooksService ,
21- private $liveSyncServiceResolver : LiveSyncServiceResolver ,
22- private $logger : ILogger ,
17+ private $liveSyncServiceResolver : ILiveSyncServiceResolver ,
18+ protected $logger : ILogger ,
2319 private $platformsDataService : IPlatformsDataService ,
2420 private $pluginsService : IPluginsService ,
2521 private $prepareController : IPrepareController ,
2622 private $prepareDataService : IPrepareDataService ,
2723 private $prepareNativePlatformService : IPrepareNativePlatformService ,
28- private $projectDataService : IProjectDataService ,
29- private $runEmitter : IRunEmitter
30- ) { super ( ) ; }
24+ protected $projectDataService : IProjectDataService ,
25+ protected $runEmitter : IRunEmitter
26+ ) { }
3127
3228 public async run ( runData : IRunData ) : Promise < void > {
3329 const { projectDir, liveSyncInfo, deviceDescriptors } = runData ;
@@ -115,6 +111,40 @@ export class RunController extends EventEmitter {
115111 return currentDescriptors || [ ] ;
116112 }
117113
114+ @performanceLog ( )
115+ protected async refreshApplication ( projectData : IProjectData , liveSyncResultInfo : ILiveSyncResultInfo , deviceDescriptor : ILiveSyncDeviceInfo , settings ?: IRefreshApplicationSettings ) : Promise < IRestartApplicationInfo > {
116+ const result = { didRestart : false } ;
117+ const platform = liveSyncResultInfo . deviceAppData . platform ;
118+ const applicationIdentifier = projectData . projectIdentifiers [ platform . toLowerCase ( ) ] ;
119+ const platformLiveSyncService = this . $liveSyncServiceResolver . resolveLiveSyncService ( platform ) ;
120+
121+ try {
122+ let shouldRestart = await platformLiveSyncService . shouldRestart ( projectData , liveSyncResultInfo ) ;
123+ if ( ! shouldRestart ) {
124+ shouldRestart = ! await platformLiveSyncService . tryRefreshApplication ( projectData , liveSyncResultInfo ) ;
125+ }
126+
127+ if ( shouldRestart ) {
128+ this . $runEmitter . emitDebuggerDetachedEvent ( liveSyncResultInfo . deviceAppData . device ) ;
129+ await platformLiveSyncService . restartApplication ( projectData , liveSyncResultInfo ) ;
130+ result . didRestart = true ;
131+ }
132+ } catch ( err ) {
133+ this . $logger . info ( `Error while trying to start application ${ applicationIdentifier } on device ${ liveSyncResultInfo . deviceAppData . device . deviceInfo . identifier } . Error is: ${ err . message || err } ` ) ;
134+ const msg = `Unable to start application ${ applicationIdentifier } on device ${ liveSyncResultInfo . deviceAppData . device . deviceInfo . identifier } . Try starting it manually.` ;
135+ this . $logger . warn ( msg ) ;
136+ if ( ! settings || ! settings . shouldSkipEmitLiveSyncNotification ) {
137+ this . $runEmitter . emitRunNotificationEvent ( projectData , liveSyncResultInfo . deviceAppData . device , msg ) ;
138+ }
139+
140+ if ( settings && settings . shouldCheckDeveloperDiscImage && ( err . message || err ) === "Could not find developer disk image" ) {
141+ this . $runEmitter . emitUserInteractionNeededEvent ( projectData , liveSyncResultInfo . deviceAppData . device , deviceDescriptor ) ;
142+ }
143+ }
144+
145+ return result ;
146+ }
147+
118148 private getDeviceDescriptorsForInitialSync ( projectDir : string , deviceDescriptors : ILiveSyncDeviceInfo [ ] ) {
119149 const currentRunData = this . processesInfo [ projectDir ] ;
120150 const isAlreadyLiveSyncing = currentRunData && ! currentRunData . isStopped ;
@@ -178,17 +208,13 @@ export class RunController extends EventEmitter {
178208 const { force, useHotModuleReload, skipWatcher } = liveSyncInfo ;
179209 const liveSyncResultInfo = await platformLiveSyncService . fullSync ( { force, useHotModuleReload, projectData, device, watch : ! skipWatcher , liveSyncDeviceInfo : deviceDescriptor } ) ;
180210
181- const refreshInfo = await this . $deviceRefreshAppService . refreshApplication ( projectData , liveSyncResultInfo , deviceDescriptor ) ;
211+ await this . refreshApplication ( projectData , liveSyncResultInfo , deviceDescriptor ) ;
182212
183213 this . $runEmitter . emitRunExecutedEvent ( projectData , device , {
184214 syncedFiles : liveSyncResultInfo . modifiedFilesData . map ( m => m . getLocalPath ( ) ) ,
185215 isFullSync : liveSyncResultInfo . isFullSync
186216 } ) ;
187217
188- if ( liveSyncResultInfo && deviceDescriptor . debuggingEnabled ) {
189- await this . $deviceDebugAppService . enableDebugging ( projectData , deviceDescriptor , refreshInfo ) ;
190- }
191-
192218 this . $logger . info ( `Successfully synced application ${ liveSyncResultInfo . deviceAppData . appIdentifier } on device ${ liveSyncResultInfo . deviceAppData . device . deviceInfo . identifier } .` ) ;
193219
194220 this . $runEmitter . emitRunStartedEvent ( projectData , device ) ;
@@ -236,6 +262,11 @@ export class RunController extends EventEmitter {
236262
237263 await this . refreshApplication ( projectData , liveSyncResultInfo , deviceDescriptor ) ;
238264
265+ this . $runEmitter . emitRunExecutedEvent ( projectData , liveSyncResultInfo . deviceAppData . device , {
266+ syncedFiles : liveSyncResultInfo . modifiedFilesData . map ( m => m . getLocalPath ( ) ) ,
267+ isFullSync : liveSyncResultInfo . isFullSync
268+ } ) ;
269+
239270 if ( ! liveSyncResultInfo . didRecover && isInHMRMode ) {
240271 const status = await this . $hmrStatusService . getHmrStatus ( device . deviceInfo . identifier , data . hmrData . hash ) ;
241272 if ( status === HmrConstants . HMR_ERROR_STATUS ) {
@@ -244,6 +275,11 @@ export class RunController extends EventEmitter {
244275 // We want to force a restart of the application.
245276 liveSyncResultInfo . isFullSync = true ;
246277 await this . refreshApplication ( projectData , liveSyncResultInfo , deviceDescriptor ) ;
278+
279+ this . $runEmitter . emitRunExecutedEvent ( projectData , liveSyncResultInfo . deviceAppData . device , {
280+ syncedFiles : liveSyncResultInfo . modifiedFilesData . map ( m => m . getLocalPath ( ) ) ,
281+ isFullSync : liveSyncResultInfo . isFullSync
282+ } ) ;
247283 }
248284 }
249285
@@ -266,19 +302,6 @@ export class RunController extends EventEmitter {
266302 } ) ) ;
267303 }
268304
269- private async refreshApplication ( projectData : IProjectData , liveSyncResultInfo : ILiveSyncResultInfo , deviceDescriptor : ILiveSyncDeviceInfo ) {
270- const refreshInfo = await this . $deviceRefreshAppService . refreshApplication ( projectData , liveSyncResultInfo , deviceDescriptor ) ;
271-
272- this . $runEmitter . emitRunExecutedEvent ( projectData , liveSyncResultInfo . deviceAppData . device , {
273- syncedFiles : liveSyncResultInfo . modifiedFilesData . map ( m => m . getLocalPath ( ) ) ,
274- isFullSync : liveSyncResultInfo . isFullSync
275- } ) ;
276-
277- if ( liveSyncResultInfo && deviceDescriptor . debuggingEnabled ) {
278- await this . $deviceDebugAppService . enableDebugging ( projectData , deviceDescriptor , refreshInfo ) ;
279- }
280- }
281-
282305 private async addActionToChain < T > ( projectDir : string , action : ( ) => Promise < T > ) : Promise < T > {
283306 const liveSyncInfo = this . processesInfo [ projectDir ] ;
284307 if ( liveSyncInfo ) {
0 commit comments