Skip to content

Commit 7c07d2e

Browse files
fix: handle previewAppLiveSyncError event in CLI
Currently the `previewAppLiveSyncError` event is handled only when CLI is used as a library. Add handler when CLI is used from command line and use the same workflow in both cases. Attach to the event every time when a new operation is started. Currently, once LiveSync is stopped, we've removed all handlers and we were never attaching them again. Now we rely on the isInitialized property, so we'll attach the event handler whenever it is needed.
1 parent f7a4437 commit 7c07d2e

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

lib/commands/preview.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,11 @@ export class PreviewCommand implements ICommand {
1919
this.$logger.info(message);
2020
});
2121

22-
await this.$liveSyncService.liveSync([], {
23-
syncToPreviewApp: true,
24-
projectDir: this.$projectData.projectDir,
25-
skipWatcher: !this.$options.watch,
26-
watchAllFiles: this.$options.syncAllFiles,
27-
clean: this.$options.clean,
22+
await this.$liveSyncService.liveSyncToPreviewApp({
2823
bundle: !!this.$options.bundle,
29-
release: this.$options.release,
30-
env: this.$options.env,
31-
timeout: this.$options.timeout,
32-
useHotModuleReload: this.$options.hmr
24+
useHotModuleReload: this.$options.hmr,
25+
projectDir: this.$projectData.projectDir,
26+
env: this.$options.env
3327
});
3428

3529
await this.$previewQrCodeService.printLiveSyncQrCode({ useHotModuleReload: this.$options.hmr, link: this.$options.link });

lib/definitions/livesync.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,13 @@ interface ILiveSyncService {
242242
*/
243243
liveSync(deviceDescriptors: ILiveSyncDeviceInfo[], liveSyncData: ILiveSyncInfo): Promise<void>;
244244

245+
/**
246+
* Starts LiveSync operation to Preview app.
247+
* @param {IPreviewAppLiveSyncData} data Describes information about the current operation.
248+
* @returns {Promise<IQrCodeImageData>} Data of the QR code that should be used to start the LiveSync operation.
249+
*/
250+
liveSyncToPreviewApp(data: IPreviewAppLiveSyncData): Promise<IQrCodeImageData>;
251+
245252
/**
246253
* Stops LiveSync operation for specified directory.
247254
* @param {string} projectDir The directory for which to stop the operation.

lib/services/livesync/livesync-service.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,13 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
137137
return currentDescriptors || [];
138138
}
139139

140-
@cache()
141140
private attachToPreviewAppLiveSyncError(): void {
142-
this.$previewAppLiveSyncService.on(LiveSyncEvents.previewAppLiveSyncError, liveSyncData => {
143-
this.$logger.error(liveSyncData.error);
144-
this.emit(LiveSyncEvents.previewAppLiveSyncError, liveSyncData);
145-
});
141+
if (!this.$usbLiveSyncService.isInitialized) {
142+
this.$previewAppLiveSyncService.on(LiveSyncEvents.previewAppLiveSyncError, liveSyncData => {
143+
this.$logger.error(liveSyncData.error);
144+
this.emit(LiveSyncEvents.previewAppLiveSyncError, liveSyncData);
145+
});
146+
}
146147
}
147148

148149
private handleWarnings(liveSyncData: ILiveSyncInfo, projectData: IProjectData) {

test/stubs.ts

+4
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,10 @@ export class DebugServiceStub extends EventEmitter implements IDeviceDebugServic
661661
}
662662

663663
export class LiveSyncServiceStub implements ILiveSyncService {
664+
public async liveSyncToPreviewApp(data: IPreviewAppLiveSyncData): Promise<IQrCodeImageData> {
665+
return;
666+
}
667+
664668
public async liveSync(deviceDescriptors: ILiveSyncDeviceInfo[], liveSyncData: ILiveSyncInfo): Promise<void> {
665669
return;
666670
}

0 commit comments

Comments
 (0)