@@ -9,7 +9,8 @@ import { PrepareDataService } from "../services/prepare-data-service";
99import { PreviewAppLiveSyncEvents } from "../services/livesync/playground/preview-app-constants" ;
1010
1111export class PreviewAppController extends EventEmitter implements IPreviewAppController {
12- private deviceInitializationPromise : IDictionary < Promise < FilesPayload > > = { } ;
12+ private deviceInitializationPromise : IDictionary < boolean > = { } ;
13+ private platformPrepareHandlers : IDictionary < boolean > = { } ;
1314 private promise = Promise . resolve ( ) ;
1415
1516 constructor (
@@ -49,9 +50,14 @@ export class PreviewAppController extends EventEmitter implements IPreviewAppCon
4950 }
5051
5152 if ( this . deviceInitializationPromise [ device . id ] ) {
52- return this . deviceInitializationPromise [ device . id ] ;
53+ // In some cases devices are reported several times during initialization.
54+ // In case we are already preparing the sending of initial files, disregard consecutive requests for initial files
55+ // until we send the files we are currently preparing.
56+ return null ;
5357 }
5458
59+ this . deviceInitializationPromise [ device . id ] = true ;
60+
5561 if ( device . uniqueId ) {
5662 await this . $analyticsService . trackEventActionInGoogleAnalytics ( {
5763 action : TrackActionNames . PreviewAppData ,
@@ -68,20 +74,25 @@ export class PreviewAppController extends EventEmitter implements IPreviewAppCon
6874
6975 await this . $previewAppPluginsService . comparePluginsOnDevice ( data , device ) ;
7076
71- this . $prepareController . on ( PREPARE_READY_EVENT_NAME , async currentPrepareData => {
72- await this . handlePrepareReadyEvent ( data , currentPrepareData . hmrData , currentPrepareData . files , device . platform ) ;
73- } ) ;
77+ if ( ! this . platformPrepareHandlers [ device . platform ] ) {
78+ // TODO: Unset this property once the preview operation for this platform is stopped
79+ this . platformPrepareHandlers [ device . platform ] = true ;
80+
81+ // TODO: Remove the handler once the preview operation for this platform is stopped
82+ this . $prepareController . on ( PREPARE_READY_EVENT_NAME , async currentPrepareData => {
83+ await this . handlePrepareReadyEvent ( data , currentPrepareData . hmrData , currentPrepareData . files , device . platform ) ;
84+ } ) ;
7485
75- if ( ! data . env ) { data . env = { } ; }
86+ }
87+
88+ data . env = data . env || { } ;
7689 data . env . externals = this . $previewAppPluginsService . getExternalPlugins ( device ) ;
7790
78- const prepareData = this . $prepareDataService . getPrepareData ( data . projectDir , device . platform . toLowerCase ( ) , { ...data , nativePrepare : { skipNativePrepare : true } , watch : true } ) ;
91+ const prepareData = this . $prepareDataService . getPrepareData ( data . projectDir , device . platform . toLowerCase ( ) , { ...data , nativePrepare : { skipNativePrepare : true } , watch : true } ) ;
7992 await this . $prepareController . prepare ( prepareData ) ;
8093
81- this . deviceInitializationPromise [ device . id ] = this . getInitialFilesForPlatformSafe ( data , device . platform ) ;
82-
8394 try {
84- const payloads = await this . deviceInitializationPromise [ device . id ] ;
95+ const payloads = await this . getInitialFilesForPlatformSafe ( data , device . platform ) ;
8596 return payloads ;
8697 } finally {
8798 this . deviceInitializationPromise [ device . id ] = null ;
@@ -116,7 +127,7 @@ export class PreviewAppController extends EventEmitter implements IPreviewAppCon
116127 if ( status === HmrConstants . HMR_ERROR_STATUS ) {
117128 const originalUseHotModuleReload = data . useHotModuleReload ;
118129 data . useHotModuleReload = false ;
119- await this . syncFilesForPlatformSafe ( data , { filesToSync : platformHmrData . fallbackFiles } , platform , previewDevice . id ) ;
130+ await this . syncFilesForPlatformSafe ( data , { filesToSync : platformHmrData . fallbackFiles } , platform , previewDevice . id ) ;
120131 data . useHotModuleReload = originalUseHotModuleReload ;
121132 }
122133 } ) ) ;
0 commit comments