Skip to content

Commit 3bc3814

Browse files
author
Fatme
authored
Merge pull request #4167 from NativeScript/fatme/preview-app-livesync-error
feat(preview-api): emit previewAppLiveSyncError when some error is thrown while livesyncing to preview app
2 parents 52d7a4f + 83577ed commit 3bc3814

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

lib/definitions/preview-app-livesync.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { FilePayload, Device, FilesPayload } from "nativescript-preview-sdk";
22
import { EventEmitter } from "events";
33

44
declare global {
5-
interface IPreviewAppLiveSyncService {
5+
interface IPreviewAppLiveSyncService extends EventEmitter {
66
initialize(data: IPreviewAppLiveSyncData): void;
77
syncFiles(data: IPreviewAppLiveSyncData, filesToSync: string[], filesToRemove: string[]): Promise<void>;
88
stopLiveSync(): Promise<void>;

lib/services/livesync/livesync-service.ts

+7
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import { PACKAGE_JSON_FILE_NAME, LiveSyncTrackActionNames, USER_INTERACTION_NEED
77
import { DeviceTypes, DeviceDiscoveryEventNames, HmrConstants } from "../../common/constants";
88
import { cache } from "../../common/decorators";
99
import * as constants from "../../constants";
10+
import { PreviewAppLiveSyncEvents } from "./playground/preview-app-constants";
1011

1112
const deviceDescriptorPrimaryKey = "identifier";
1213

1314
const LiveSyncEvents = {
1415
liveSyncStopped: "liveSyncStopped",
1516
// 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
1617
liveSyncError: "liveSyncError",
18+
previewAppLiveSyncError: PreviewAppLiveSyncEvents.PREVIEW_APP_LIVE_SYNC_ERROR,
1719
liveSyncExecuted: "liveSyncExecuted",
1820
liveSyncStarted: "liveSyncStarted",
1921
liveSyncNotification: "notify"
@@ -54,6 +56,10 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
5456
}
5557

5658
public async liveSyncToPreviewApp(data: IPreviewAppLiveSyncData): Promise<IQrCodeImageData> {
59+
this.$previewAppLiveSyncService.on(LiveSyncEvents.previewAppLiveSyncError, liveSyncData => {
60+
this.emit(LiveSyncEvents.previewAppLiveSyncError, liveSyncData);
61+
});
62+
5763
await this.liveSync([], {
5864
syncToPreviewApp: true,
5965
projectDir: data.projectDir,
@@ -102,6 +108,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
102108

103109
if (liveSyncProcessInfo.syncToPreviewApp) {
104110
await this.$previewAppLiveSyncService.stopLiveSync();
111+
this.$previewAppLiveSyncService.removeAllListeners();
105112
}
106113

107114
// Kill typescript watcher

lib/services/livesync/playground/preview-app-constants.ts

+4
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ export class PluginComparisonMessages {
1818
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.";
1919
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.";
2020
}
21+
22+
export class PreviewAppLiveSyncEvents {
23+
public static PREVIEW_APP_LIVE_SYNC_ERROR = "previewAppLiveSyncError";
24+
}

lib/services/livesync/playground/preview-app-livesync-service.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as path from "path";
22
import { FilePayload, Device, FilesPayload } from "nativescript-preview-sdk";
3-
import { PreviewSdkEventNames } from "./preview-app-constants";
3+
import { PreviewSdkEventNames, PreviewAppLiveSyncEvents } from "./preview-app-constants";
44
import { APP_FOLDER_NAME, APP_RESOURCES_FOLDER_NAME, TNS_MODULES_FOLDER_NAME } from "../../../constants";
55
import { HmrConstants } from "../../../common/constants";
6+
import { EventEmitter } from "events";
67
const isTextOrBinary = require('istextorbinary');
78

89
interface ISyncFilesOptions {
@@ -14,7 +15,7 @@ interface ISyncFilesOptions {
1415
deviceId?: string;
1516
}
1617

17-
export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
18+
export class PreviewAppLiveSyncService extends EventEmitter implements IPreviewAppLiveSyncService {
1819
private excludedFileExtensions = [".ts", ".sass", ".scss", ".less"];
1920
private excludedFiles = [".DS_Store"];
2021
private deviceInitializationPromise: IDictionary<Promise<FilesPayload>> = {};
@@ -31,7 +32,9 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
3132
private $previewDevicesService: IPreviewDevicesService,
3233
private $projectFilesManager: IProjectFilesManager,
3334
private $hmrStatusService: IHmrStatusService,
34-
private $projectFilesProvider: IProjectFilesProvider) { }
35+
private $projectFilesProvider: IProjectFilesProvider) {
36+
super();
37+
}
3538

3639
public async initialize(data: IPreviewAppLiveSyncData): Promise<void> {
3740
await this.$previewSdkService.initialize(async (device: Device) => {
@@ -159,8 +162,14 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
159162
}
160163

161164
return payloads;
162-
} catch (err) {
163-
this.$logger.warn(`Unable to apply changes for platform ${platform}. Error is: ${err}, ${JSON.stringify(err, null, 2)}.`);
165+
} catch (error) {
166+
this.$logger.warn(`Unable to apply changes for platform ${platform}. Error is: ${error}, ${JSON.stringify(error, null, 2)}.`);
167+
this.emit(PreviewAppLiveSyncEvents.PREVIEW_APP_LIVE_SYNC_ERROR, {
168+
error,
169+
data,
170+
platform,
171+
deviceId: opts.deviceId
172+
});
164173
}
165174
}
166175

0 commit comments

Comments
 (0)