@@ -2,6 +2,7 @@ import * as path from "path";
22import { FilePayload , Device , FilesPayload } from "nativescript-preview-sdk" ;
33import { PreviewSdkEventNames } from "./preview-app-constants" ;
44import { APP_FOLDER_NAME , APP_RESOURCES_FOLDER_NAME , TNS_MODULES_FOLDER_NAME } from "../../../constants" ;
5+ import { HmrConstants } from "../../../common/constants" ;
56const isTextOrBinary = require ( 'istextorbinary' ) ;
67
78export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
@@ -19,6 +20,7 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
1920 private $previewSdkService : IPreviewSdkService ,
2021 private $previewAppPluginsService : IPreviewAppPluginsService ,
2122 private $projectFilesManager : IProjectFilesManager ,
23+ private $hmrStatusService : IHmrStatusService ,
2224 private $projectFilesProvider : IProjectFilesProvider ) { }
2325
2426 public async initialize ( data : IPreviewAppLiveSyncData ) : Promise < void > {
@@ -43,19 +45,38 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
4345
4446 private async initializePreviewForDevice ( data : IPreviewAppLiveSyncData , device : Device ) : Promise < FilesPayload > {
4547 const filesToSyncMap : IDictionary < string [ ] > = { } ;
48+ const hmrData : { hash : string ; fallbackFiles : IDictionary < string [ ] > } = {
49+ hash : "" ,
50+ fallbackFiles : { }
51+ } ;
4652 let promise = Promise . resolve < FilesPayload > ( null ) ;
4753 const startSyncFilesTimeout = async ( platform : string ) => {
4854 await promise
4955 . then ( async ( ) => {
5056 const projectData = this . $projectDataService . getProjectData ( data . projectDir ) ;
51- promise = this . applyChanges ( this . $platformsData . getPlatformData ( platform , projectData ) , projectData , filesToSyncMap [ platform ] ) ;
57+ const platformData = this . $platformsData . getPlatformData ( platform , projectData ) ;
58+ const currentHmrData = _ . cloneDeep ( hmrData ) ;
59+ const filesToSync = _ . cloneDeep ( filesToSyncMap [ platform ] ) ;
60+ promise = this . applyChanges ( platformData , projectData , filesToSync , { useHotModuleReload : data . appFilesUpdaterOptions . useHotModuleReload } ) ;
5261 await promise ;
62+
63+ if ( data . appFilesUpdaterOptions . useHotModuleReload && currentHmrData . hash ) {
64+ const devices = _ . filter ( this . $previewSdkService . connectedDevices , { platform : platform . toLowerCase ( ) } ) ;
65+
66+ await Promise . all ( _ . map ( devices , async ( previewDevice : Device ) => {
67+ const status = await this . $hmrStatusService . getHmrStatus ( previewDevice . id , currentHmrData . hash ) ;
68+ if ( status === HmrConstants . HMR_ERROR_STATUS ) {
69+ await this . applyChanges ( platformData , projectData , currentHmrData . fallbackFiles [ platform ] , { useHotModuleReload : false } , previewDevice . id ) ;
70+ }
71+ } ) ) ;
72+ }
5373 } ) ;
5474 filesToSyncMap [ platform ] = [ ] ;
5575 } ;
5676 await this . $hooksService . executeBeforeHooks ( "preview-sync" , {
5777 hookArgs : {
5878 projectData : this . $projectDataService . getProjectData ( data . projectDir ) ,
79+ hmrData,
5980 config : {
6081 env : data . env ,
6182 platform : device . platform ,
@@ -104,10 +125,11 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
104125
105126 let result : FilesPayload = null ;
106127 if ( files && files . length ) {
107- result = await this . applyChanges ( platformData , projectData , files ) ;
128+ result = await this . applyChanges ( platformData , projectData , files , { useHotModuleReload : data . appFilesUpdaterOptions . useHotModuleReload } ) ;
108129 this . $logger . info ( `Successfully synced ${ result . files . map ( filePayload => filePayload . file . yellow ) } for platform ${ platform } .` ) ;
109130 } else {
110- result = await this . getFilesPayload ( platformData , projectData ) ;
131+ const hmrMode = data . appFilesUpdaterOptions . useHotModuleReload ? 1 : 0 ;
132+ result = await this . getFilesPayload ( platformData , projectData , hmrMode ) ;
111133 this . $logger . info ( `Successfully synced changes for platform ${ platform } .` ) ;
112134 }
113135
@@ -117,14 +139,15 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
117139 }
118140 }
119141
120- private async applyChanges ( platformData : IPlatformData , projectData : IProjectData , files : string [ ] ) : Promise < FilesPayload > {
121- const payloads = this . getFilesPayload ( platformData , projectData , _ ( files ) . uniq ( ) . value ( ) ) ;
142+ private async applyChanges ( platformData : IPlatformData , projectData : IProjectData , files : string [ ] , { useHotModuleReload } : { useHotModuleReload : Boolean } , deviceId ?: string ) : Promise < FilesPayload > {
143+ const hmrMode = useHotModuleReload ? 1 : 0 ;
144+ const payloads = this . getFilesPayload ( platformData , projectData , hmrMode , _ ( files ) . uniq ( ) . value ( ) , deviceId ) ;
122145 await this . $previewSdkService . applyChanges ( payloads ) ;
123146
124147 return payloads ;
125148 }
126149
127- private getFilesPayload ( platformData : IPlatformData , projectData : IProjectData , files ?: string [ ] ) : FilesPayload {
150+ private getFilesPayload ( platformData : IPlatformData , projectData : IProjectData , hmrMode : number , files ?: string [ ] , deviceId ?: string ) : FilesPayload {
128151 const platformsAppFolderPath = path . join ( platformData . appDestinationDirectoryPath , APP_FOLDER_NAME ) ;
129152
130153 if ( files && files . length ) {
@@ -163,7 +186,7 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
163186 return filePayload ;
164187 } ) ;
165188
166- return { files : payloads , platform : platformData . normalizedPlatformName . toLowerCase ( ) } ;
189+ return { files : payloads , platform : platformData . normalizedPlatformName . toLowerCase ( ) , hmrMode , deviceId } ;
167190 }
168191
169192 private async preparePlatform ( platform : string , appFilesUpdaterOptions : IAppFilesUpdaterOptions , env : Object , projectData : IProjectData ) : Promise < void > {
0 commit comments