@@ -594,7 +594,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
594594 let filesToRemove : string [ ] = [ ] ;
595595 let timeoutTimer : NodeJS . Timer ;
596596
597- const startSyncFilesTimeout = ( platform ?: string ) => {
597+ const startSyncFilesTimeout = ( platform ?: string , opts ?: { calledFromHook : boolean } ) => {
598598 timeoutTimer = setTimeout ( async ( ) => {
599599 if ( platform && liveSyncData . bundle ) {
600600 filesToSync = filesToSyncMap [ platform ] ;
@@ -636,6 +636,52 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
636636 const liveSyncProcessInfo = this . liveSyncProcessesInfo [ projectData . projectDir ] ;
637637 const deviceBuildInfoDescriptor = _ . find ( liveSyncProcessInfo . deviceDescriptors , dd => dd . identifier === device . deviceInfo . identifier ) ;
638638
639+ const settings : ILiveSyncWatchInfo = {
640+ liveSyncDeviceInfo : deviceBuildInfoDescriptor ,
641+ projectData,
642+ filesToRemove : currentFilesToRemove ,
643+ filesToSync : currentFilesToSync ,
644+ isReinstalled : false ,
645+ syncAllFiles : liveSyncData . watchAllFiles ,
646+ hmrData : currentHmrData ,
647+ useHotModuleReload : liveSyncData . useHotModuleReload ,
648+ force : liveSyncData . force ,
649+ connectTimeout : 1000
650+ } ;
651+
652+ const service = this . getLiveSyncService ( device . deviceInfo . platform ) ;
653+
654+ const watchAction = async ( watchInfo : ILiveSyncWatchInfo ) : Promise < void > => {
655+ let liveSyncResultInfo = await service . liveSyncWatchAction ( device , watchInfo ) ;
656+
657+ await this . refreshApplication ( projectData , liveSyncResultInfo , deviceBuildInfoDescriptor . debugOptions , deviceBuildInfoDescriptor . outputPath ) ;
658+
659+ // If didRecover is true, this means we were in ErrorActivity and fallback files were already transfered and app will be restarted.
660+ if ( ! liveSyncResultInfo . didRecover && liveSyncData . useHotModuleReload && currentHmrData . hash ) {
661+ const status = await this . $hmrStatusService . getHmrStatus ( device . deviceInfo . identifier , currentHmrData . hash ) ;
662+ if ( status === HmrConstants . HMR_ERROR_STATUS ) {
663+ watchInfo . filesToSync = currentHmrData . fallbackFiles [ device . deviceInfo . platform ] ;
664+ liveSyncResultInfo = await service . liveSyncWatchAction ( device , watchInfo ) ;
665+ // We want to force a restart of the application.
666+ liveSyncResultInfo . isFullSync = true ;
667+ await this . refreshApplication ( projectData , liveSyncResultInfo , deviceBuildInfoDescriptor . debugOptions , deviceBuildInfoDescriptor . outputPath ) ;
668+ }
669+ }
670+
671+ this . $logger . info ( `Successfully synced application ${ liveSyncResultInfo . deviceAppData . appIdentifier } on device ${ liveSyncResultInfo . deviceAppData . device . deviceInfo . identifier } .` ) ;
672+ } ;
673+
674+ if ( liveSyncData . useHotModuleReload && opts && opts . calledFromHook ) {
675+ try {
676+ this . $logger . trace ( "Try executing watch action without any preparation of files." ) ;
677+ await watchAction ( settings ) ;
678+ this . $logger . trace ( "Successfully executed watch action without any preparation of files." ) ;
679+ return ;
680+ } catch ( err ) {
681+ this . $logger . trace ( `Error while trying to execute fast sync. Now we'll check the state of the app and we'll try to resurrect from the error. The error is: ${ err } ` ) ;
682+ }
683+ }
684+
639685 const appInstalledOnDeviceResult = await this . ensureLatestAppPackageIsInstalledOnDevice ( {
640686 device,
641687 preparedPlatforms,
@@ -653,41 +699,15 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
653699 skipModulesNativeCheck : ! liveSyncData . watchAllFiles
654700 } , { skipNativePrepare : deviceBuildInfoDescriptor . skipNativePrepare } ) ;
655701
702+ settings . isReinstalled = appInstalledOnDeviceResult . appInstalled ;
703+ settings . connectTimeout = null ;
704+
656705 if ( liveSyncData . useHotModuleReload && appInstalledOnDeviceResult . appInstalled ) {
657706 const additionalFilesToSync = currentHmrData && currentHmrData . fallbackFiles && currentHmrData . fallbackFiles [ device . deviceInfo . platform ] ;
658707 _ . each ( additionalFilesToSync , fileToSync => currentFilesToSync . push ( fileToSync ) ) ;
659708 }
660709
661- const service = this . getLiveSyncService ( device . deviceInfo . platform ) ;
662- const settings : ILiveSyncWatchInfo = {
663- liveSyncDeviceInfo : deviceBuildInfoDescriptor ,
664- projectData,
665- filesToRemove : currentFilesToRemove ,
666- filesToSync : currentFilesToSync ,
667- isReinstalled : appInstalledOnDeviceResult . appInstalled ,
668- syncAllFiles : liveSyncData . watchAllFiles ,
669- hmrData : currentHmrData ,
670- useHotModuleReload : liveSyncData . useHotModuleReload ,
671- force : liveSyncData . force
672- } ;
673-
674- let liveSyncResultInfo = await service . liveSyncWatchAction ( device , settings ) ;
675-
676- await this . refreshApplication ( projectData , liveSyncResultInfo , deviceBuildInfoDescriptor . debugOptions , deviceBuildInfoDescriptor . outputPath ) ;
677-
678- //If didRecover is true, this means we were in ErrorActivity and fallback files were already transfered and app will be restarted.
679- if ( ! liveSyncResultInfo . didRecover && liveSyncData . useHotModuleReload && currentHmrData . hash ) {
680- const status = await this . $hmrStatusService . getHmrStatus ( device . deviceInfo . identifier , currentHmrData . hash ) ;
681- if ( status === HmrConstants . HMR_ERROR_STATUS ) {
682- settings . filesToSync = currentHmrData . fallbackFiles [ device . deviceInfo . platform ] ;
683- liveSyncResultInfo = await service . liveSyncWatchAction ( device , settings ) ;
684- //We want to force a restart of the application.
685- liveSyncResultInfo . isFullSync = true ;
686- await this . refreshApplication ( projectData , liveSyncResultInfo , deviceBuildInfoDescriptor . debugOptions , deviceBuildInfoDescriptor . outputPath ) ;
687- }
688- }
689-
690- this . $logger . info ( `Successfully synced application ${ liveSyncResultInfo . deviceAppData . appIdentifier } on device ${ liveSyncResultInfo . deviceAppData . device . deviceInfo . identifier } .` ) ;
710+ await watchAction ( settings ) ;
691711 } ,
692712 ( device : Mobile . IDevice ) => {
693713 const liveSyncProcessInfo = this . liveSyncProcessesInfo [ projectData . projectDir ] ;
@@ -715,7 +735,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
715735 } ) ;
716736 }
717737 }
718- } , 250 ) ;
738+ } , liveSyncData . useHotModuleReload ? 0 : 250 ) ;
719739
720740 this . liveSyncProcessesInfo [ liveSyncData . projectDir ] . timer = timeoutTimer ;
721741 } ;
@@ -738,11 +758,12 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
738758 hmrData,
739759 filesToRemove,
740760 startSyncFilesTimeout : async ( platform : string ) => {
761+ const opts = { calledFromHook : true } ;
741762 if ( platform ) {
742- await startSyncFilesTimeout ( platform ) ;
763+ await startSyncFilesTimeout ( platform , opts ) ;
743764 } else {
744765 // This code is added for backwards compatibility with old versions of nativescript-dev-webpack plugin.
745- await startSyncFilesTimeout ( ) ;
766+ await startSyncFilesTimeout ( null , opts ) ;
746767 }
747768 }
748769 }
@@ -823,7 +844,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
823844 }
824845 }
825846
826- public emitLivesyncEvent ( event : string , livesyncData : ILiveSyncEventData ) : boolean {
847+ public emitLivesyncEvent ( event : string , livesyncData : ILiveSyncEventData ) : boolean {
827848 this . $logger . trace ( `Will emit event ${ event } with data` , livesyncData ) ;
828849 return this . emit ( event , livesyncData ) ;
829850 }
0 commit comments