Skip to content

Commit fdfa5a6

Browse files
committed
fix: remove application start when refreshing application
1 parent 19c85bf commit fdfa5a6

File tree

6 files changed

+43
-78
lines changed

6 files changed

+43
-78
lines changed

lib/common/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export class EmulatorDiscoveryNames {
5656
export const DEVICE_LOG_EVENT_NAME = "deviceLogData";
5757
export const IOS_LOG_PREDICATE = 'senderImagePath contains "NativeScript" || eventMessage contains[c] "NativeScript"';
5858
export const IOS_APP_CRASH_LOG_REG_EXP = /Fatal JavaScript exception \- application has been terminated/;
59+
export const FAIL_LIVESYNC_LOG_REGEX = /Failed to refresh the application with RefreshRequest./;
5960

6061
export const TARGET_FRAMEWORK_IDENTIFIERS = {
6162
Cordova: "Cordova",

lib/declarations.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,11 +756,12 @@ interface IiOSNotification extends NodeJS.EventEmitter {
756756
getAttachRequest(appId: string, deviceId: string): string;
757757
getReadyForAttach(appId: string): string;
758758
getRefreshRequest(appId: string): string;
759+
getAppRefreshStarted(appId: string): string;
759760
}
760761

761762
interface IiOSSocketRequestExecutor {
762763
executeAttachRequest(device: Mobile.IiOSDevice, timeout: number, projectId: string): Promise<void>;
763-
executeRefreshRequest(device: Mobile.IiOSDevice, appId: string): Promise<void>;
764+
executeRefreshRequest(device: Mobile.IiOSDevice, appId: string): Promise<boolean>;
764765
}
765766

766767
/**

lib/device-sockets/ios/notification.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ATTACH_REQUEST_EVENT_NAME } from "../../common/constants";
33

44
export class IOSNotification extends EventEmitter implements IiOSNotification {
55
private static REFRESH_REQUEST_NOTIFICATION_NAME = "RefreshRequest";
6+
private static APP_REFRESH_STARTED_NOTIFICATION_NAME = "AppRefreshStarted";
67
private static ATTACH_REQUEST_NOTIFICATION_NAME = "AttachRequest";
78
private static READY_FOR_ATTACH_NOTIFICATION_NAME = "ReadyForAttach";
89

@@ -21,6 +22,10 @@ export class IOSNotification extends EventEmitter implements IiOSNotification {
2122
return this.formatNotification(IOSNotification.REFRESH_REQUEST_NOTIFICATION_NAME, appId);
2223
}
2324

25+
public getAppRefreshStarted(appId: string): string {
26+
return this.formatNotification(IOSNotification.APP_REFRESH_STARTED_NOTIFICATION_NAME, appId);
27+
}
28+
2429
private formatNotification(notification: string, appId: string) {
2530
return `${appId}:NativeScript.Debug.${notification}`;
2631
}

lib/device-sockets/ios/socket-request-executor.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,39 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
66
private $iOSNotificationService: IiOSNotificationService) { }
77

88
public async executeAttachRequest(device: Mobile.IiOSDevice, timeout: number, appId: string): Promise<void> {
9-
const deviceIdentifier = device.deviceInfo.identifier;
9+
const deviceId = device.deviceInfo.identifier;
10+
const mainRequestName = this.$iOSNotification.getAttachRequest(appId, deviceId);
11+
const readyRequestName = this.$iOSNotification.getReadyForAttach(appId);
12+
await this.executeRequest(mainRequestName, readyRequestName, appId, deviceId, timeout);
13+
}
14+
15+
public async executeRefreshRequest(device: Mobile.IiOSDevice, appId: string): Promise<boolean> {
16+
const deviceId = device.deviceInfo.identifier;
17+
const mainRequestName = this.$iOSNotification.getRefreshRequest(appId);
18+
const readyRequestName = this.$iOSNotification.getAppRefreshStarted(appId);
19+
const timeout = 5;
20+
21+
const result = await this.executeRequest(mainRequestName, readyRequestName, appId, deviceId, timeout);
22+
23+
return result;
24+
}
25+
26+
private async executeRequest(mainRequestName: string, readyRequestName: string, appId: string, deviceId: string, timeout: number): Promise<boolean> {
27+
let isSuccessful = false;
28+
1029
try {
1130
// We should create this promise here because we need to send the ObserveNotification on the device
1231
// before we send the PostNotification.
13-
const readyForAttachSocket = await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getReadyForAttach(appId), constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE);
14-
const readyForAttachPromise = this.$iOSNotificationService.awaitNotification(deviceIdentifier, +readyForAttachSocket, timeout);
15-
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getAttachRequest(appId, deviceIdentifier));
16-
await readyForAttachPromise;
32+
const socket = await this.$iOSNotificationService.postNotification(deviceId, readyRequestName, constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE);
33+
const notificationPromise = this.$iOSNotificationService.awaitNotification(deviceId, +socket, timeout);
34+
await this.$iOSNotificationService.postNotification(deviceId, mainRequestName);
35+
await notificationPromise;
36+
isSuccessful = true;
1737
} catch (e) {
18-
this.$errors.fail(`The application ${appId} does not appear to be running on ${deviceIdentifier} or is not built with debugging enabled. Try starting the application manually.`);
38+
this.$errors.fail(`The application ${appId} does not appear to be running on ${deviceId} or is not built with debugging enabled. Try starting the application manually.`);
1939
}
20-
}
2140

22-
public async executeRefreshRequest(device: Mobile.IiOSDevice, appId: string): Promise<void> {
23-
await this.$iOSNotificationService.postNotification(
24-
device.deviceInfo.identifier, this.$iOSNotification.getRefreshRequest(appId));
41+
return isSuccessful;
2542
}
2643
}
2744

lib/services/hmr-status-service.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { cache } from "../common/decorators";
2-
import { HmrConstants, IOS_APP_CRASH_LOG_REG_EXP } from "../common/constants";
2+
import { HmrConstants, IOS_APP_CRASH_LOG_REG_EXP, FAIL_LIVESYNC_LOG_REGEX } from "../common/constants";
33

44
export class HmrStatusService implements IHmrStatusService {
55
public static HMR_STATUS_LOG_REGEX = /([a-z A-Z]*) hmr hash ([a-z0-9]*)\./;
@@ -49,6 +49,12 @@ export class HmrStatusService implements IHmrStatusService {
4949
name: "appCrashHmr",
5050
platform: this.$devicePlatformsConstants.iOS.toLowerCase()
5151
});
52+
this.$logParserService.addParseRule({
53+
regex: FAIL_LIVESYNC_LOG_REGEX,
54+
handler: this.handleAppCrash.bind(this),
55+
name: "failedLiveSync",
56+
platform: this.$devicePlatformsConstants.iOS.toLowerCase()
57+
});
5258
}
5359

5460
private handleAppCrash(matches: RegExpMatchArray, deviceId: string): void {

lib/services/livesync/ios-device-livesync-service.ts

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import * as constants from "../../constants";
22
import * as minimatch from "minimatch";
3-
import { cache } from "../../common/decorators";
4-
import { IOS_APP_CRASH_LOG_REG_EXP } from "../../common/constants";
53
import * as net from "net";
64
import { DeviceLiveSyncServiceBase } from "./device-livesync-service-base";
75
import { performanceLog } from "../../common/decorators";
@@ -10,10 +8,7 @@ import * as semver from "semver";
108
let currentPageReloadId = 0;
119

1210
export class IOSDeviceLiveSyncService extends DeviceLiveSyncServiceBase implements INativeScriptDeviceLiveSyncService {
13-
public static SUCCESS_LIVESYNC_LOG_REGEX = /Successfully refreshed the application with RefreshRequest./;
14-
public static FAIL_LIVESYNC_LOG_REGEX = /Failed to refresh the application with RefreshRequest./;
1511
private static MIN_RUNTIME_VERSION_WITH_REFRESH_NOTIFICATION = "6.1.0";
16-
private _isLiveSyncSuccessful: boolean = null;
1712

1813
private socket: net.Socket;
1914

@@ -22,12 +17,10 @@ export class IOSDeviceLiveSyncService extends DeviceLiveSyncServiceBase implemen
2217
private $iOSSocketRequestExecutor: IiOSSocketRequestExecutor,
2318
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
2419
private $lockService: ILockService,
25-
private $logParserService: ILogParserService,
2620
protected platformsDataService: IPlatformsDataService,
2721
private $platformCommandHelper: IPlatformCommandHelper,
2822
protected device: Mobile.IiOSDevice) {
2923
super(platformsDataService, device);
30-
3124
}
3225

3326
private canRefreshWithNotification(projectData: IProjectData): boolean {
@@ -123,16 +116,7 @@ export class IOSDeviceLiveSyncService extends DeviceLiveSyncServiceBase implemen
123116
private async refreshWithNotification(projectData: IProjectData) {
124117
let didRefresh = false;
125118
await this.$lockService.executeActionWithLock(async () => {
126-
this._isLiveSyncSuccessful = null;
127-
this.attachToLiveSyncLogs();
128-
// TODO: start the application only when needed when we know the app state
129-
await this.device.applicationManager.startApplication({
130-
appId: projectData.projectIdentifiers.ios,
131-
projectName: projectData.projectName,
132-
projectDir: projectData.projectDir
133-
});
134-
await this.$iOSSocketRequestExecutor.executeRefreshRequest(this.device, projectData.projectIdentifiers.ios);
135-
didRefresh = await this.isLiveSyncSuccessful();
119+
didRefresh = await this.$iOSSocketRequestExecutor.executeRefreshRequest(this.device, projectData.projectIdentifiers.ios);
136120
}, `ios-device-livesync-${this.device.deviceInfo.identifier}-${projectData.projectIdentifiers.ios}.lock`);
137121

138122
return didRefresh;
@@ -147,55 +131,6 @@ export class IOSDeviceLiveSyncService extends DeviceLiveSyncServiceBase implemen
147131
});
148132
}
149133

150-
@cache()
151-
private attachToLiveSyncLogs(): void {
152-
this.$logParserService.addParseRule({
153-
regex: IOSDeviceLiveSyncService.SUCCESS_LIVESYNC_LOG_REGEX,
154-
handler: this.onSuccessfulLiveSync.bind(this),
155-
name: "successfulLiveSync",
156-
platform: this.$devicePlatformsConstants.iOS.toLowerCase()
157-
});
158-
this.$logParserService.addParseRule({
159-
regex: IOSDeviceLiveSyncService.FAIL_LIVESYNC_LOG_REGEX,
160-
handler: this.onFailedLiveSync.bind(this),
161-
name: "failedLiveSync",
162-
platform: this.$devicePlatformsConstants.iOS.toLowerCase()
163-
});
164-
this.$logParserService.addParseRule({
165-
regex: IOS_APP_CRASH_LOG_REG_EXP,
166-
handler: this.onFailedLiveSync.bind(this),
167-
name: "liveSyncCrash",
168-
platform: this.$devicePlatformsConstants.iOS.toLowerCase()
169-
});
170-
}
171-
172-
private onSuccessfulLiveSync(): void {
173-
this._isLiveSyncSuccessful = true;
174-
}
175-
176-
private onFailedLiveSync(): void {
177-
this._isLiveSyncSuccessful = false;
178-
}
179-
180-
private async isLiveSyncSuccessful(): Promise<boolean> {
181-
return new Promise<boolean>((resolve, reject) => {
182-
const retryInterval = 500;
183-
let retryCount = 60 * 1000 / retryInterval;
184-
185-
const interval = setInterval(() => {
186-
if (this._isLiveSyncSuccessful !== null) {
187-
clearInterval(interval);
188-
resolve(this._isLiveSyncSuccessful);
189-
} else if (retryCount === 0) {
190-
clearInterval(interval);
191-
resolve(false);
192-
} else {
193-
retryCount--;
194-
}
195-
}, retryInterval);
196-
});
197-
}
198-
199134
private async reloadPage(): Promise<void> {
200135
const message = JSON.stringify({
201136
method: "Page.reload",

0 commit comments

Comments
 (0)