@@ -9,8 +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 prepareReadyEventHandler : any = null ;
12
13
private deviceInitializationPromise : IDictionary < boolean > = { } ;
13
- private platformPrepareHandlers : IDictionary < boolean > = { } ;
14
14
private promise = Promise . resolve ( ) ;
15
15
16
16
constructor (
@@ -41,6 +41,10 @@ export class PreviewAppController extends EventEmitter implements IPreviewAppCon
41
41
public async stopPreview ( ) : Promise < void > {
42
42
this . $previewSdkService . stop ( ) ;
43
43
this . $previewDevicesService . updateConnectedDevices ( [ ] ) ;
44
+ if ( this . prepareReadyEventHandler ) {
45
+ this . removeListener ( PREPARE_READY_EVENT_NAME , this . prepareReadyEventHandler ) ;
46
+ this . prepareReadyEventHandler = null ;
47
+ }
44
48
}
45
49
46
50
private async previewCore ( data : IPreviewAppLiveSyncData ) : Promise < void > {
@@ -76,17 +80,11 @@ export class PreviewAppController extends EventEmitter implements IPreviewAppCon
76
80
77
81
await this . $previewAppPluginsService . comparePluginsOnDevice ( data , device ) ;
78
82
79
- if ( ! this . platformPrepareHandlers [ device . platform ] ) {
80
- // TODO: Unset this property once the preview operation for this platform is stopped
81
- this . platformPrepareHandlers [ device . platform ] = true ;
82
-
83
- // TODO: Remove the handler once the preview operation for this platform is stopped
84
- this . $prepareController . on ( PREPARE_READY_EVENT_NAME , async ( currentPrepareData : IFilesChangeEventData ) => {
85
- if ( currentPrepareData . platform . toLowerCase ( ) === device . platform . toLowerCase ( ) ) {
86
- await this . handlePrepareReadyEvent ( data , currentPrepareData . hmrData , currentPrepareData . files , device . platform ) ;
87
- }
88
- } ) ;
89
-
83
+ if ( ! this . prepareReadyEventHandler ) {
84
+ this . prepareReadyEventHandler = async ( currentPrepareData : IFilesChangeEventData ) => {
85
+ await this . handlePrepareReadyEvent ( data , currentPrepareData ) ;
86
+ } ;
87
+ this . $prepareController . on ( PREPARE_READY_EVENT_NAME , this . prepareReadyEventHandler . bind ( this ) ) ;
90
88
}
91
89
92
90
data . env = data . env || { } ;
@@ -115,9 +113,10 @@ export class PreviewAppController extends EventEmitter implements IPreviewAppCon
115
113
}
116
114
117
115
@performanceLog ( )
118
- private async handlePrepareReadyEvent ( data : IPreviewAppLiveSyncData , hmrData : IPlatformHmrData , files : string [ ] , platform : string ) {
116
+ private async handlePrepareReadyEvent ( data : IPreviewAppLiveSyncData , currentPrepareData : IFilesChangeEventData ) {
119
117
await this . promise
120
118
. then ( async ( ) => {
119
+ const { hmrData, files, platform } = currentPrepareData ;
121
120
const platformHmrData = _ . cloneDeep ( hmrData ) ;
122
121
123
122
this . promise = this . syncFilesForPlatformSafe ( data , { filesToSync : files } , platform ) ;
0 commit comments