@@ -9,7 +9,8 @@ import { PrepareDataService } from "../services/prepare-data-service";
9
9
import { PreviewAppLiveSyncEvents } from "../services/livesync/playground/preview-app-constants" ;
10
10
11
11
export class PreviewAppController extends EventEmitter implements IPreviewAppController {
12
- private deviceInitializationPromise : IDictionary < Promise < FilesPayload > > = { } ;
12
+ private deviceInitializationPromise : IDictionary < boolean > = { } ;
13
+ private platformPrepareHandlers : IDictionary < boolean > = { } ;
13
14
private promise = Promise . resolve ( ) ;
14
15
15
16
constructor (
@@ -49,9 +50,14 @@ export class PreviewAppController extends EventEmitter implements IPreviewAppCon
49
50
}
50
51
51
52
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 ;
53
57
}
54
58
59
+ this . deviceInitializationPromise [ device . id ] = true ;
60
+
55
61
if ( device . uniqueId ) {
56
62
await this . $analyticsService . trackEventActionInGoogleAnalytics ( {
57
63
action : TrackActionNames . PreviewAppData ,
@@ -68,20 +74,25 @@ export class PreviewAppController extends EventEmitter implements IPreviewAppCon
68
74
69
75
await this . $previewAppPluginsService . comparePluginsOnDevice ( data , device ) ;
70
76
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
+ } ) ;
74
85
75
- if ( ! data . env ) { data . env = { } ; }
86
+ }
87
+
88
+ data . env = data . env || { } ;
76
89
data . env . externals = this . $previewAppPluginsService . getExternalPlugins ( device ) ;
77
90
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 } ) ;
79
92
await this . $prepareController . prepare ( prepareData ) ;
80
93
81
- this . deviceInitializationPromise [ device . id ] = this . getInitialFilesForPlatformSafe ( data , device . platform ) ;
82
-
83
94
try {
84
- const payloads = await this . deviceInitializationPromise [ device . id ] ;
95
+ const payloads = await this . getInitialFilesForPlatformSafe ( data , device . platform ) ;
85
96
return payloads ;
86
97
} finally {
87
98
this . deviceInitializationPromise [ device . id ] = null ;
@@ -116,7 +127,7 @@ export class PreviewAppController extends EventEmitter implements IPreviewAppCon
116
127
if ( status === HmrConstants . HMR_ERROR_STATUS ) {
117
128
const originalUseHotModuleReload = data . useHotModuleReload ;
118
129
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 ) ;
120
131
data . useHotModuleReload = originalUseHotModuleReload ;
121
132
}
122
133
} ) ) ;
0 commit comments