@@ -74,51 +74,57 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
7474 }
7575
7676 private async initializePreviewForDevice ( data : IPreviewAppLiveSyncData , device : Device ) : Promise < FilesPayload > {
77- const filesToSyncMap : IDictionary < string [ ] > = { } ;
78- const hmrData : IDictionary < IPlatformHmrData > = { } ;
79- let promise = Promise . resolve < FilesPayload > ( null ) ;
80- const startSyncFilesTimeout = async ( platform : string ) => {
81- await promise
82- . then ( async ( ) => {
83- const currentHmrData = _ . cloneDeep ( hmrData ) ;
84- const platformHmrData = currentHmrData [ platform ] || < any > { } ;
85- const filesToSync = _ . cloneDeep ( filesToSyncMap [ platform ] ) ;
86- // We don't need to prepare when webpack emits changed files. We just need to send a message to pubnub.
87- promise = this . syncFilesForPlatformSafe ( data , platform , { filesToSync, skipPrepare : true , useHotModuleReload : data . appFilesUpdaterOptions . useHotModuleReload } ) ;
88- await promise ;
89-
90- if ( data . appFilesUpdaterOptions . useHotModuleReload && platformHmrData . hash ) {
91- const devices = _ . filter ( this . $previewSdkService . connectedDevices , { platform : platform . toLowerCase ( ) } ) ;
92-
93- await Promise . all ( _ . map ( devices , async ( previewDevice : Device ) => {
94- const status = await this . $hmrStatusService . getHmrStatus ( previewDevice . id , platformHmrData . hash ) ;
95- if ( status === HmrConstants . HMR_ERROR_STATUS ) {
96- await this . syncFilesForPlatformSafe ( data , platform , { filesToSync : platformHmrData . fallbackFiles , useHotModuleReload : false , deviceId : previewDevice . id } ) ;
97- }
98- } ) ) ;
99- }
100- } ) ;
101- filesToSyncMap [ platform ] = [ ] ;
102- } ;
103- await this . $hooksService . executeBeforeHooks ( "preview-sync" , {
104- hookArgs : {
105- projectData : this . $projectDataService . getProjectData ( data . projectDir ) ,
106- hmrData,
107- config : {
108- env : data . env ,
109- platform : device . platform ,
110- appFilesUpdaterOptions : data . appFilesUpdaterOptions ,
111- } ,
112- externals : this . $previewAppPluginsService . getExternalPlugins ( device ) ,
113- filesToSyncMap,
114- startSyncFilesTimeout : startSyncFilesTimeout . bind ( this )
115- }
116- } ) ;
77+ const hookArgs = this . getHookArgs ( data , device ) ;
78+ await this . $hooksService . executeBeforeHooks ( "preview-sync" , { hookArgs } ) ;
11779 await this . $previewAppPluginsService . comparePluginsOnDevice ( data , device ) ;
11880 const payloads = await this . syncFilesForPlatformSafe ( data , device . platform , { isInitialSync : true , useHotModuleReload : data . appFilesUpdaterOptions . useHotModuleReload } ) ;
11981 return payloads ;
12082 }
12183
84+ private getHookArgs ( data : IPreviewAppLiveSyncData , device : Device ) {
85+ const filesToSyncMap : IDictionary < string [ ] > = { } ;
86+ const hmrData : IDictionary < IPlatformHmrData > = { } ;
87+ const promise = Promise . resolve < FilesPayload > ( null ) ;
88+ const result = {
89+ projectData : this . $projectDataService . getProjectData ( data . projectDir ) ,
90+ hmrData,
91+ config : {
92+ env : data . env ,
93+ platform : device . platform ,
94+ appFilesUpdaterOptions : data . appFilesUpdaterOptions ,
95+ } ,
96+ externals : this . $previewAppPluginsService . getExternalPlugins ( device ) ,
97+ filesToSyncMap,
98+ startSyncFilesTimeout : async ( platform : string ) => await this . onWebpackCompilationComplete ( data , hmrData , filesToSyncMap , promise , platform )
99+ } ;
100+
101+ return result ;
102+ }
103+
104+ private async onWebpackCompilationComplete ( data : IPreviewAppLiveSyncData , hmrData : IDictionary < IPlatformHmrData > , filesToSyncMap : IDictionary < string [ ] > , promise : Promise < FilesPayload > , platform : string ) {
105+ await promise
106+ . then ( async ( ) => {
107+ const currentHmrData = _ . cloneDeep ( hmrData ) ;
108+ const platformHmrData = currentHmrData [ platform ] || < any > { } ;
109+ const filesToSync = _ . cloneDeep ( filesToSyncMap [ platform ] ) ;
110+ // We don't need to prepare when webpack emits changed files. We just need to send a message to pubnub.
111+ promise = this . syncFilesForPlatformSafe ( data , platform , { filesToSync, skipPrepare : true , useHotModuleReload : data . appFilesUpdaterOptions . useHotModuleReload } ) ;
112+ await promise ;
113+
114+ if ( data . appFilesUpdaterOptions . useHotModuleReload && platformHmrData . hash ) {
115+ const devices = _ . filter ( this . $previewSdkService . connectedDevices , { platform : platform . toLowerCase ( ) } ) ;
116+
117+ await Promise . all ( _ . map ( devices , async ( previewDevice : Device ) => {
118+ const status = await this . $hmrStatusService . getHmrStatus ( previewDevice . id , platformHmrData . hash ) ;
119+ if ( status === HmrConstants . HMR_ERROR_STATUS ) {
120+ await this . syncFilesForPlatformSafe ( data , platform , { filesToSync : platformHmrData . fallbackFiles , useHotModuleReload : false , deviceId : previewDevice . id } ) ;
121+ }
122+ } ) ) ;
123+ }
124+ } ) ;
125+ filesToSyncMap [ platform ] = [ ] ;
126+ }
127+
122128 private async syncFilesForPlatformSafe ( data : IPreviewAppLiveSyncData , platform : string , opts ?: ISyncFilesOptions ) : Promise < FilesPayload > {
123129 this . $logger . info ( `Start syncing changes for platform ${ platform } .` ) ;
124130
0 commit comments