@@ -7,6 +7,7 @@ const isTextOrBinary = require('istextorbinary');
77export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
88 private excludedFileExtensions = [ ".ts" , ".sass" , ".scss" , ".less" ] ;
99 private excludedFiles = [ ".DS_Store" ] ;
10+ private deviceInitializationPromise : IDictionary < Promise < FilesPayload > > = { } ;
1011
1112 constructor ( private $fs : IFileSystem ,
1213 private $errors : IErrors ,
@@ -26,37 +27,51 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
2627 this . $errors . failWithoutHelp ( "Sending initial preview files without a specified device is not supported." ) ;
2728 }
2829
29- const filesToSyncMap : IDictionary < string [ ] > = { } ;
30- let promise = Promise . resolve < FilesPayload > ( null ) ;
31- const startSyncFilesTimeout = async ( platform : string ) => {
32- await promise
33- . then ( async ( ) => {
34- const projectData = this . $projectDataService . getProjectData ( data . projectDir ) ;
35- promise = this . applyChanges ( this . $platformsData . getPlatformData ( platform , projectData ) , projectData , filesToSyncMap [ platform ] ) ;
36- await promise ;
37- } ) ;
38- filesToSyncMap [ platform ] = [ ] ;
39- } ;
40- await this . $hooksService . executeBeforeHooks ( "preview-sync" , {
41- hookArgs : {
42- projectData : this . $projectDataService . getProjectData ( data . projectDir ) ,
43- config : {
44- env : data . env ,
45- platform : device . platform ,
46- appFilesUpdaterOptions : data . appFilesUpdaterOptions ,
47- } ,
48- externals : this . $previewAppPluginsService . getExternalPlugins ( device ) ,
49- filesToSyncMap,
50- startSyncFilesTimeout : startSyncFilesTimeout . bind ( this )
51- }
52- } ) ;
53- await this . $previewAppPluginsService . comparePluginsOnDevice ( data , device ) ;
54- const payloads = await this . syncFilesForPlatformSafe ( data , device . platform ) ;
30+ if ( this . deviceInitializationPromise [ device . id ] ) {
31+ return this . deviceInitializationPromise [ device . id ] ;
32+ }
5533
56- return payloads ;
34+ this . deviceInitializationPromise [ device . id ] = this . initializePreviewForDevice ( data , device ) ;
35+ try {
36+ const payloads = await this . deviceInitializationPromise [ device . id ] ;
37+ return payloads ;
38+ } finally {
39+ this . deviceInitializationPromise [ device . id ] = null ;
40+ }
5741 } ) ;
5842 }
5943
44+ private async initializePreviewForDevice ( data : IPreviewAppLiveSyncData , device : Device ) : Promise < FilesPayload > {
45+ const filesToSyncMap : IDictionary < string [ ] > = { } ;
46+ let promise = Promise . resolve < FilesPayload > ( null ) ;
47+ const startSyncFilesTimeout = async ( platform : string ) => {
48+ await promise
49+ . then ( async ( ) => {
50+ const projectData = this . $projectDataService . getProjectData ( data . projectDir ) ;
51+ promise = this . applyChanges ( this . $platformsData . getPlatformData ( platform , projectData ) , projectData , filesToSyncMap [ platform ] ) ;
52+ await promise ;
53+ } ) ;
54+ filesToSyncMap [ platform ] = [ ] ;
55+ } ;
56+ await this . $hooksService . executeBeforeHooks ( "preview-sync" , {
57+ hookArgs : {
58+ projectData : this . $projectDataService . getProjectData ( data . projectDir ) ,
59+ config : {
60+ env : data . env ,
61+ platform : device . platform ,
62+ appFilesUpdaterOptions : data . appFilesUpdaterOptions ,
63+ } ,
64+ externals : this . $previewAppPluginsService . getExternalPlugins ( device ) ,
65+ filesToSyncMap,
66+ startSyncFilesTimeout : startSyncFilesTimeout . bind ( this )
67+ }
68+ } ) ;
69+ await this . $previewAppPluginsService . comparePluginsOnDevice ( data , device ) ;
70+ const payloads = await this . syncFilesForPlatformSafe ( data , device . platform ) ;
71+ payloads . deviceId = device . id ;
72+ return payloads ;
73+ }
74+
6075 public async syncFiles ( data : IPreviewAppLiveSyncData , files ?: string [ ] ) : Promise < void > {
6176 this . showWarningsForNativeFiles ( files ) ;
6277
0 commit comments