Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(preview-api): emit previewAppLiveSyncError when some error is thrown while livesyncing to preview app #4167

Merged
merged 1 commit into from
Nov 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/definitions/preview-app-livesync.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
stopLiveSync(): Promise<void>;
Expand Down
7 changes: 7 additions & 0 deletions lib/services/livesync/livesync-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ 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";

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"
Expand Down Expand Up @@ -54,6 +56,10 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
}

public async liveSyncToPreviewApp(data: IPreviewAppLiveSyncData): Promise<IQrCodeImageData> {
this.$previewAppLiveSyncService.on(LiveSyncEvents.previewAppLiveSyncError, liveSyncData => {
this.emit(LiveSyncEvents.previewAppLiveSyncError, liveSyncData);
});

await this.liveSync([], {
syncToPreviewApp: true,
projectDir: data.projectDir,
Expand Down Expand Up @@ -102,6 +108,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi

if (liveSyncProcessInfo.syncToPreviewApp) {
await this.$previewAppLiveSyncService.stopLiveSync();
this.$previewAppLiveSyncService.removeAllListeners();
}

// Kill typescript watcher
Expand Down
4 changes: 4 additions & 0 deletions lib/services/livesync/playground/preview-app-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
19 changes: 14 additions & 5 deletions lib/services/livesync/playground/preview-app-livesync-service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<Promise<FilesPayload>> = {};
Expand All @@ -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<void> {
await this.$previewSdkService.initialize(async (device: Device) => {
Expand Down Expand Up @@ -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
});
}
}

Expand Down