@@ -2,6 +2,7 @@ import * as path from "path";
2
2
import { FilePayload , Device , FilesPayload } from "nativescript-preview-sdk" ;
3
3
import { PreviewSdkEventNames } from "./preview-app-constants" ;
4
4
import { APP_FOLDER_NAME , APP_RESOURCES_FOLDER_NAME , TNS_MODULES_FOLDER_NAME } from "../../../constants" ;
5
+ import { HmrConstants } from "../../../common/constants" ;
5
6
const isTextOrBinary = require ( 'istextorbinary' ) ;
6
7
7
8
export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
@@ -19,6 +20,7 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
19
20
private $previewSdkService : IPreviewSdkService ,
20
21
private $previewAppPluginsService : IPreviewAppPluginsService ,
21
22
private $projectFilesManager : IProjectFilesManager ,
23
+ private $hmrStatusService : IHmrStatusService ,
22
24
private $projectFilesProvider : IProjectFilesProvider ) { }
23
25
24
26
public async initialize ( data : IPreviewAppLiveSyncData ) : Promise < void > {
@@ -43,19 +45,38 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
43
45
44
46
private async initializePreviewForDevice ( data : IPreviewAppLiveSyncData , device : Device ) : Promise < FilesPayload > {
45
47
const filesToSyncMap : IDictionary < string [ ] > = { } ;
48
+ const hmrData : { hash : string ; fallbackFiles : IDictionary < string [ ] > } = {
49
+ hash : "" ,
50
+ fallbackFiles : { }
51
+ } ;
46
52
let promise = Promise . resolve < FilesPayload > ( null ) ;
47
53
const startSyncFilesTimeout = async ( platform : string ) => {
48
54
await promise
49
55
. then ( async ( ) => {
50
56
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 } ) ;
52
61
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
+ }
53
73
} ) ;
54
74
filesToSyncMap [ platform ] = [ ] ;
55
75
} ;
56
76
await this . $hooksService . executeBeforeHooks ( "preview-sync" , {
57
77
hookArgs : {
58
78
projectData : this . $projectDataService . getProjectData ( data . projectDir ) ,
79
+ hmrData,
59
80
config : {
60
81
env : data . env ,
61
82
platform : device . platform ,
@@ -104,10 +125,11 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
104
125
105
126
let result : FilesPayload = null ;
106
127
if ( files && files . length ) {
107
- result = await this . applyChanges ( platformData , projectData , files ) ;
128
+ result = await this . applyChanges ( platformData , projectData , files , { useHotModuleReload : data . appFilesUpdaterOptions . useHotModuleReload } ) ;
108
129
this . $logger . info ( `Successfully synced ${ result . files . map ( filePayload => filePayload . file . yellow ) } for platform ${ platform } .` ) ;
109
130
} else {
110
- result = await this . getFilesPayload ( platformData , projectData ) ;
131
+ const hmrMode = data . appFilesUpdaterOptions . useHotModuleReload ? 1 : 0 ;
132
+ result = await this . getFilesPayload ( platformData , projectData , hmrMode ) ;
111
133
this . $logger . info ( `Successfully synced changes for platform ${ platform } .` ) ;
112
134
}
113
135
@@ -117,14 +139,15 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
117
139
}
118
140
}
119
141
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 ) ;
122
145
await this . $previewSdkService . applyChanges ( payloads ) ;
123
146
124
147
return payloads ;
125
148
}
126
149
127
- private getFilesPayload ( platformData : IPlatformData , projectData : IProjectData , files ?: string [ ] ) : FilesPayload {
150
+ private getFilesPayload ( platformData : IPlatformData , projectData : IProjectData , hmrMode : number , files ?: string [ ] , deviceId ?: string ) : FilesPayload {
128
151
const platformsAppFolderPath = path . join ( platformData . appDestinationDirectoryPath , APP_FOLDER_NAME ) ;
129
152
130
153
if ( files && files . length ) {
@@ -163,7 +186,7 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
163
186
return filePayload ;
164
187
} ) ;
165
188
166
- return { files : payloads , platform : platformData . normalizedPlatformName . toLowerCase ( ) } ;
189
+ return { files : payloads , platform : platformData . normalizedPlatformName . toLowerCase ( ) , hmrMode , deviceId } ;
167
190
}
168
191
169
192
private async preparePlatform ( platform : string , appFilesUpdaterOptions : IAppFilesUpdaterOptions , env : Object , projectData : IProjectData ) : Promise < void > {
0 commit comments