From f7a4437604ed695138dd2575dea1e1a747700165 Mon Sep 17 00:00:00 2001 From: rosen-vladimirov Date: Mon, 7 Jan 2019 18:19:08 +0200 Subject: [PATCH 1/2] fix: do not show error from preview initialization when stop is called When preview is called with webpack from Public API, if you stop the livesync (via stopLiveSync method) while webpack is still working, in the output you'll see an error as webpack's child process exits with `null` exit code. However, this error does not have any meaning for the user, so we shouldn't raise it. Move the logging of the error to LiveSyncService's handler of `previewAppLiveSyncError` event - in the described case it will not be raised as LiveSyncService had already removed all listeners. --- lib/services/livesync/livesync-service.ts | 1 + .../livesync/playground/preview-app-livesync-service.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/services/livesync/livesync-service.ts b/lib/services/livesync/livesync-service.ts index fd8dc63993..54df97043e 100644 --- a/lib/services/livesync/livesync-service.ts +++ b/lib/services/livesync/livesync-service.ts @@ -140,6 +140,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi @cache() private attachToPreviewAppLiveSyncError(): void { this.$previewAppLiveSyncService.on(LiveSyncEvents.previewAppLiveSyncError, liveSyncData => { + this.$logger.error(liveSyncData.error); this.emit(LiveSyncEvents.previewAppLiveSyncError, liveSyncData); }); } diff --git a/lib/services/livesync/playground/preview-app-livesync-service.ts b/lib/services/livesync/playground/preview-app-livesync-service.ts index 4dae4f41e8..2883dd1ccf 100644 --- a/lib/services/livesync/playground/preview-app-livesync-service.ts +++ b/lib/services/livesync/playground/preview-app-livesync-service.ts @@ -43,7 +43,7 @@ export class PreviewAppLiveSyncService extends EventEmitter implements IPreviewA this.deviceInitializationPromise[device.id] = null; } } catch (error) { - this.$logger.error(error); + this.$logger.trace(`Error while sending files on device ${device && device.id}. Error is`, error); this.emit(PreviewAppLiveSyncEvents.PREVIEW_APP_LIVE_SYNC_ERROR, { error, data, From 7c07d2ee7dd2b8feb251a42d0fc263be9777ce18 Mon Sep 17 00:00:00 2001 From: Rosen Vladimirov Date: Mon, 7 Jan 2019 21:52:07 +0200 Subject: [PATCH 2/2] 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. --- lib/commands/preview.ts | 14 ++++---------- lib/definitions/livesync.d.ts | 7 +++++++ lib/services/livesync/livesync-service.ts | 11 ++++++----- test/stubs.ts | 4 ++++ 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/commands/preview.ts b/lib/commands/preview.ts index f3bf388a30..7d8a4af6ec 100644 --- a/lib/commands/preview.ts +++ b/lib/commands/preview.ts @@ -19,17 +19,11 @@ export class PreviewCommand implements ICommand { this.$logger.info(message); }); - await this.$liveSyncService.liveSync([], { - syncToPreviewApp: true, - projectDir: this.$projectData.projectDir, - skipWatcher: !this.$options.watch, - watchAllFiles: this.$options.syncAllFiles, - clean: this.$options.clean, + await this.$liveSyncService.liveSyncToPreviewApp({ bundle: !!this.$options.bundle, - release: this.$options.release, - env: this.$options.env, - timeout: this.$options.timeout, - useHotModuleReload: this.$options.hmr + useHotModuleReload: this.$options.hmr, + projectDir: this.$projectData.projectDir, + env: this.$options.env }); await this.$previewQrCodeService.printLiveSyncQrCode({ useHotModuleReload: this.$options.hmr, link: this.$options.link }); diff --git a/lib/definitions/livesync.d.ts b/lib/definitions/livesync.d.ts index a4274c147a..212474a508 100644 --- a/lib/definitions/livesync.d.ts +++ b/lib/definitions/livesync.d.ts @@ -242,6 +242,13 @@ interface ILiveSyncService { */ liveSync(deviceDescriptors: ILiveSyncDeviceInfo[], liveSyncData: ILiveSyncInfo): Promise; + /** + * Starts LiveSync operation to Preview app. + * @param {IPreviewAppLiveSyncData} data Describes information about the current operation. + * @returns {Promise} Data of the QR code that should be used to start the LiveSync operation. + */ + liveSyncToPreviewApp(data: IPreviewAppLiveSyncData): Promise; + /** * Stops LiveSync operation for specified directory. * @param {string} projectDir The directory for which to stop the operation. diff --git a/lib/services/livesync/livesync-service.ts b/lib/services/livesync/livesync-service.ts index 54df97043e..38e3b8603d 100644 --- a/lib/services/livesync/livesync-service.ts +++ b/lib/services/livesync/livesync-service.ts @@ -137,12 +137,13 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi return currentDescriptors || []; } - @cache() private attachToPreviewAppLiveSyncError(): void { - this.$previewAppLiveSyncService.on(LiveSyncEvents.previewAppLiveSyncError, liveSyncData => { - this.$logger.error(liveSyncData.error); - this.emit(LiveSyncEvents.previewAppLiveSyncError, liveSyncData); - }); + if (!this.$usbLiveSyncService.isInitialized) { + this.$previewAppLiveSyncService.on(LiveSyncEvents.previewAppLiveSyncError, liveSyncData => { + this.$logger.error(liveSyncData.error); + this.emit(LiveSyncEvents.previewAppLiveSyncError, liveSyncData); + }); + } } private handleWarnings(liveSyncData: ILiveSyncInfo, projectData: IProjectData) { diff --git a/test/stubs.ts b/test/stubs.ts index 28a0ce35f8..b06e61e7b8 100644 --- a/test/stubs.ts +++ b/test/stubs.ts @@ -661,6 +661,10 @@ export class DebugServiceStub extends EventEmitter implements IDeviceDebugServic } export class LiveSyncServiceStub implements ILiveSyncService { + public async liveSyncToPreviewApp(data: IPreviewAppLiveSyncData): Promise { + return; + } + public async liveSync(deviceDescriptors: ILiveSyncDeviceInfo[], liveSyncData: ILiveSyncInfo): Promise { return; }