From 83577edd22cf8c59134d0381e1fda89fa4624817 Mon Sep 17 00:00:00 2001 From: fatme Date: Thu, 22 Nov 2018 09:25:49 +0200 Subject: [PATCH] feat(preview-api): emit previewAppLiveSyncError when some error is thrown while livesyncing to preview app --- lib/definitions/preview-app-livesync.d.ts | 2 +- lib/services/livesync/livesync-service.ts | 7 +++++++ .../playground/preview-app-constants.ts | 4 ++++ .../preview-app-livesync-service.ts | 19 ++++++++++++++----- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/definitions/preview-app-livesync.d.ts b/lib/definitions/preview-app-livesync.d.ts index bac20cb7b0..cc17e6959b 100644 --- a/lib/definitions/preview-app-livesync.d.ts +++ b/lib/definitions/preview-app-livesync.d.ts @@ -2,7 +2,7 @@ import { FilePayload, Device, FilesPayload } from "nativescript-preview-sdk"; import { EventEmitter } from "events"; declare global { - interface IPreviewAppLiveSyncService { + interface IPreviewAppLiveSyncService extends EventEmitter { initialize(data: IPreviewAppLiveSyncData): void; syncFiles(data: IPreviewAppLiveSyncData, filesToSync: string[], filesToRemove: string[]): Promise; stopLiveSync(): Promise; diff --git a/lib/services/livesync/livesync-service.ts b/lib/services/livesync/livesync-service.ts index ecac15210b..9f9b6283a9 100644 --- a/lib/services/livesync/livesync-service.ts +++ b/lib/services/livesync/livesync-service.ts @@ -7,6 +7,7 @@ import { PACKAGE_JSON_FILE_NAME, LiveSyncTrackActionNames, USER_INTERACTION_NEED import { DeviceTypes, DeviceDiscoveryEventNames, HmrConstants } from "../../common/constants"; import { cache } from "../../common/decorators"; import * as constants from "../../constants"; +import { PreviewAppLiveSyncEvents } from "./playground/preview-app-constants"; const deviceDescriptorPrimaryKey = "identifier"; @@ -14,6 +15,7 @@ const LiveSyncEvents = { liveSyncStopped: "liveSyncStopped", // In case we name it error, EventEmitter expects instance of Error to be raised and will also raise uncaught exception in case there's no handler liveSyncError: "liveSyncError", + previewAppLiveSyncError: PreviewAppLiveSyncEvents.PREVIEW_APP_LIVE_SYNC_ERROR, liveSyncExecuted: "liveSyncExecuted", liveSyncStarted: "liveSyncStarted", liveSyncNotification: "notify" @@ -54,6 +56,10 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi } public async liveSyncToPreviewApp(data: IPreviewAppLiveSyncData): Promise { + this.$previewAppLiveSyncService.on(LiveSyncEvents.previewAppLiveSyncError, liveSyncData => { + this.emit(LiveSyncEvents.previewAppLiveSyncError, liveSyncData); + }); + await this.liveSync([], { syncToPreviewApp: true, projectDir: data.projectDir, @@ -102,6 +108,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi if (liveSyncProcessInfo.syncToPreviewApp) { await this.$previewAppLiveSyncService.stopLiveSync(); + this.$previewAppLiveSyncService.removeAllListeners(); } // Kill typescript watcher diff --git a/lib/services/livesync/playground/preview-app-constants.ts b/lib/services/livesync/playground/preview-app-constants.ts index 49afc03626..fd8ac000e6 100644 --- a/lib/services/livesync/playground/preview-app-constants.ts +++ b/lib/services/livesync/playground/preview-app-constants.ts @@ -18,3 +18,7 @@ export class PluginComparisonMessages { public static LOCAL_PLUGIN_WITH_DIFFERENCE_IN_MAJOR_VERSION = "Local plugin %s differs in major version from plugin in preview app. The local plugin has version %s and the plugin in preview app has version %s. Some features might not work as expected."; public static LOCAL_PLUGIN_WITH_GREATHER_MINOR_VERSION = "Local plugin %s differs in minor version from plugin in preview app. The local plugin has version %s and the plugin in preview app has version %s. Some features might not work as expected."; } + +export class PreviewAppLiveSyncEvents { + public static PREVIEW_APP_LIVE_SYNC_ERROR = "previewAppLiveSyncError"; +} diff --git a/lib/services/livesync/playground/preview-app-livesync-service.ts b/lib/services/livesync/playground/preview-app-livesync-service.ts index 02dd1394df..4f6dbe8655 100644 --- a/lib/services/livesync/playground/preview-app-livesync-service.ts +++ b/lib/services/livesync/playground/preview-app-livesync-service.ts @@ -1,8 +1,9 @@ import * as path from "path"; import { FilePayload, Device, FilesPayload } from "nativescript-preview-sdk"; -import { PreviewSdkEventNames } from "./preview-app-constants"; +import { PreviewSdkEventNames, PreviewAppLiveSyncEvents } from "./preview-app-constants"; import { APP_FOLDER_NAME, APP_RESOURCES_FOLDER_NAME, TNS_MODULES_FOLDER_NAME } from "../../../constants"; import { HmrConstants } from "../../../common/constants"; +import { EventEmitter } from "events"; const isTextOrBinary = require('istextorbinary'); interface ISyncFilesOptions { @@ -14,7 +15,7 @@ interface ISyncFilesOptions { deviceId?: string; } -export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService { +export class PreviewAppLiveSyncService extends EventEmitter implements IPreviewAppLiveSyncService { private excludedFileExtensions = [".ts", ".sass", ".scss", ".less"]; private excludedFiles = [".DS_Store"]; private deviceInitializationPromise: IDictionary> = {}; @@ -31,7 +32,9 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService { private $previewDevicesService: IPreviewDevicesService, private $projectFilesManager: IProjectFilesManager, private $hmrStatusService: IHmrStatusService, - private $projectFilesProvider: IProjectFilesProvider) { } + private $projectFilesProvider: IProjectFilesProvider) { + super(); + } public async initialize(data: IPreviewAppLiveSyncData): Promise { await this.$previewSdkService.initialize(async (device: Device) => { @@ -159,8 +162,14 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService { } return payloads; - } catch (err) { - this.$logger.warn(`Unable to apply changes for platform ${platform}. Error is: ${err}, ${JSON.stringify(err, null, 2)}.`); + } catch (error) { + this.$logger.warn(`Unable to apply changes for platform ${platform}. Error is: ${error}, ${JSON.stringify(error, null, 2)}.`); + this.emit(PreviewAppLiveSyncEvents.PREVIEW_APP_LIVE_SYNC_ERROR, { + error, + data, + platform, + deviceId: opts.deviceId + }); } }