1
- import { EventEmitter } from "events" ;
2
- import { LiveSyncServiceResolver } from "../resolvers/livesync-service-resolver" ;
3
1
import { HmrConstants , DeviceDiscoveryEventNames } from "../common/constants" ;
4
2
import { PREPARE_READY_EVENT_NAME , TrackActionNames } from "../constants" ;
5
- import { cache } from "../common/decorators" ;
3
+ import { cache , performanceLog } from "../common/decorators" ;
6
4
7
- export class RunController extends EventEmitter {
5
+ export class RunController implements IRunController {
8
6
private processesInfo : IDictionary < IRunOnDeviceProcessInfo > = { } ;
9
7
10
8
constructor (
11
9
private $analyticsService : IAnalyticsService ,
12
10
private $buildDataService : IBuildDataService ,
13
11
private $buildController : IBuildController ,
14
- private $deviceDebugAppService : IDeviceDebugAppService ,
15
12
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 ,
19
15
private $hmrStatusService : IHmrStatusService ,
20
16
public $hooksService : IHooksService ,
21
- private $liveSyncServiceResolver : LiveSyncServiceResolver ,
22
- private $logger : ILogger ,
17
+ private $liveSyncServiceResolver : ILiveSyncServiceResolver ,
18
+ protected $logger : ILogger ,
23
19
private $platformsDataService : IPlatformsDataService ,
24
20
private $pluginsService : IPluginsService ,
25
21
private $prepareController : IPrepareController ,
26
22
private $prepareDataService : IPrepareDataService ,
27
23
private $prepareNativePlatformService : IPrepareNativePlatformService ,
28
- private $projectDataService : IProjectDataService ,
29
- private $runEmitter : IRunEmitter
30
- ) { super ( ) ; }
24
+ protected $projectDataService : IProjectDataService ,
25
+ protected $runEmitter : IRunEmitter
26
+ ) { }
31
27
32
28
public async run ( runData : IRunData ) : Promise < void > {
33
29
const { projectDir, liveSyncInfo, deviceDescriptors } = runData ;
@@ -115,6 +111,40 @@ export class RunController extends EventEmitter {
115
111
return currentDescriptors || [ ] ;
116
112
}
117
113
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
+
118
148
private getDeviceDescriptorsForInitialSync ( projectDir : string , deviceDescriptors : ILiveSyncDeviceInfo [ ] ) {
119
149
const currentRunData = this . processesInfo [ projectDir ] ;
120
150
const isAlreadyLiveSyncing = currentRunData && ! currentRunData . isStopped ;
@@ -178,17 +208,13 @@ export class RunController extends EventEmitter {
178
208
const { force, useHotModuleReload, skipWatcher } = liveSyncInfo ;
179
209
const liveSyncResultInfo = await platformLiveSyncService . fullSync ( { force, useHotModuleReload, projectData, device, watch : ! skipWatcher , liveSyncDeviceInfo : deviceDescriptor } ) ;
180
210
181
- const refreshInfo = await this . $deviceRefreshAppService . refreshApplication ( projectData , liveSyncResultInfo , deviceDescriptor ) ;
211
+ await this . refreshApplication ( projectData , liveSyncResultInfo , deviceDescriptor ) ;
182
212
183
213
this . $runEmitter . emitRunExecutedEvent ( projectData , device , {
184
214
syncedFiles : liveSyncResultInfo . modifiedFilesData . map ( m => m . getLocalPath ( ) ) ,
185
215
isFullSync : liveSyncResultInfo . isFullSync
186
216
} ) ;
187
217
188
- if ( liveSyncResultInfo && deviceDescriptor . debuggingEnabled ) {
189
- await this . $deviceDebugAppService . enableDebugging ( projectData , deviceDescriptor , refreshInfo ) ;
190
- }
191
-
192
218
this . $logger . info ( `Successfully synced application ${ liveSyncResultInfo . deviceAppData . appIdentifier } on device ${ liveSyncResultInfo . deviceAppData . device . deviceInfo . identifier } .` ) ;
193
219
194
220
this . $runEmitter . emitRunStartedEvent ( projectData , device ) ;
@@ -236,6 +262,11 @@ export class RunController extends EventEmitter {
236
262
237
263
await this . refreshApplication ( projectData , liveSyncResultInfo , deviceDescriptor ) ;
238
264
265
+ this . $runEmitter . emitRunExecutedEvent ( projectData , liveSyncResultInfo . deviceAppData . device , {
266
+ syncedFiles : liveSyncResultInfo . modifiedFilesData . map ( m => m . getLocalPath ( ) ) ,
267
+ isFullSync : liveSyncResultInfo . isFullSync
268
+ } ) ;
269
+
239
270
if ( ! liveSyncResultInfo . didRecover && isInHMRMode ) {
240
271
const status = await this . $hmrStatusService . getHmrStatus ( device . deviceInfo . identifier , data . hmrData . hash ) ;
241
272
if ( status === HmrConstants . HMR_ERROR_STATUS ) {
@@ -244,6 +275,11 @@ export class RunController extends EventEmitter {
244
275
// We want to force a restart of the application.
245
276
liveSyncResultInfo . isFullSync = true ;
246
277
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
+ } ) ;
247
283
}
248
284
}
249
285
@@ -266,19 +302,6 @@ export class RunController extends EventEmitter {
266
302
} ) ) ;
267
303
}
268
304
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
-
282
305
private async addActionToChain < T > ( projectDir : string , action : ( ) => Promise < T > ) : Promise < T > {
283
306
const liveSyncInfo = this . processesInfo [ projectDir ] ;
284
307
if ( liveSyncInfo ) {
0 commit comments