Skip to content

Commit 8e72b52

Browse files
Fatme HavaluovaFatme Havaluova
Fatme Havaluova
authored and
Fatme Havaluova
committed
Fast livesync for iOS simulator + some code review changes
1 parent ee3be6f commit 8e72b52

File tree

5 files changed

+24
-48
lines changed

5 files changed

+24
-48
lines changed

lib/device-sockets/ios/socket-proxy-factory.ts

+5-19
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
import { PacketStream } from "./packet-stream";
55
import * as net from "net";
6-
import semver = require("semver");
6+
import * as semver from "semver";
7+
import * as ws from "ws";
78
import temp = require("temp");
8-
import ws = require("ws");
9+
import * as helpers from "../../common/helpers";
910

1011
export class SocketProxyFactory implements ISocketProxyFactory {
1112
constructor(private $logger: ILogger,
@@ -14,7 +15,7 @@ export class SocketProxyFactory implements ISocketProxyFactory {
1415

1516
public createSocketProxy(factory: () => net.Socket): IFuture<any> {
1617
return (() => {
17-
let socketFactory = (callback: (_socket: net.Socket) => void) => this.connectEventually(factory, callback);
18+
let socketFactory = (callback: (_socket: net.Socket) => void) => helpers.connectEventually(factory, callback);
1819

1920
this.$projectDataService.initialize(this.$projectData.projectDir);
2021
let frameworkVersion = this.$projectDataService.getValue("tns-ios").wait().version;
@@ -30,21 +31,6 @@ export class SocketProxyFactory implements ISocketProxyFactory {
3031
}).future<any>()();
3132
}
3233

33-
private connectEventually(factory: () => net.Socket, handler: (_socket: net.Socket) => void) { // TODO: Add socketProxyBase
34-
function tryConnect() {
35-
let tryConnectAfterTimeout = setTimeout.bind(undefined, tryConnect, 1000);
36-
37-
let socket = factory();
38-
socket.on("connect", () => {
39-
socket.removeListener("error", tryConnectAfterTimeout);
40-
handler(socket);
41-
});
42-
socket.on("error", tryConnectAfterTimeout);
43-
}
44-
45-
tryConnect();
46-
}
47-
4834
private createWebSocketProxy(socketFactory: (handler: (socket: net.Socket) => void) => void): ws.Server {
4935
// NOTE: We will try to provide command line options to select ports, at least on the localhost.
5036
let localPort = 8080;
@@ -58,7 +44,7 @@ export class SocketProxyFactory implements ISocketProxyFactory {
5844

5945
let server = ws.createServer(<any>{
6046
port: localPort,
61-
verifyClient: (info: any, callback: any) => {
47+
verifyClient: (info: any, callback: Function) => {
6248
this.$logger.info("Frontend client connected.");
6349
socketFactory((_socket: any) => {
6450
this.$logger.info("Backend socket created.");

lib/services/ios-debug-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as iOSDevice from "../common/mobile/ios/ios-device";
55

66
import * as net from "net";
77
import * as path from "path";
8-
import semver = require("semver");
8+
import * as semver from "semver";
99
import byline = require("byline");
1010

1111
let InspectorBackendPort = 18181;

lib/services/ios-notification-service.ts

-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
import Future = require("fibers/future");
55

66
export class IOSNotificationService implements IiOSNotificationService {
7-
constructor(private $errors: IErrors,
8-
private $projectData: IProjectData) { }
9-
107
public awaitNotification(npc: Mobile.INotificationProxyClient, notification: string, timeout: number): IFuture<string> {
118
let future = new Future<string>();
129

lib/services/test-execution-service.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,18 @@ class TestExecutionService implements ITestExecutionService {
6767
}
6868
};
6969

70-
let notInstalledAppOnDeviceAction = (device: Mobile.IDevice): IFuture<boolean> => {
70+
let notInstalledAppOnDeviceAction = (device: Mobile.IDevice): IFuture<void> => {
7171
return (() => {
7272
this.$platformService.installOnDevice(platform).wait();
7373
this.detourEntryPoint(projectFilesPath).wait();
74-
return true;
75-
}).future<boolean>()();
74+
}).future<void>()();
7675
};
7776

78-
let notRunningiOSSimulatorAction = (): IFuture<boolean> => {
77+
let notRunningiOSSimulatorAction = (): IFuture<void> => {
7978
return (() => {
8079
this.$platformService.deployOnEmulator(this.$devicePlatformsConstants.iOS.toLowerCase()).wait();
8180
this.detourEntryPoint(projectFilesPath).wait();
82-
return true;
83-
}).future<boolean>()();
81+
}).future<void>()();
8482
};
8583

8684
let beforeBatchLiveSyncAction = (filePath: string): IFuture<string> => {

lib/services/usb-livesync-service.ts

+14-19
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as path from "path";
88
import * as semver from "semver";
99
import * as net from "net";
1010
import Future = require("fibers/future");
11+
import * as helpers from "../common/helpers";
1112

1213
export class UsbLiveSyncService extends usbLivesyncServiceBaseLib.UsbLiveSyncServiceBase implements IUsbLiveSyncService {
1314

@@ -49,7 +50,7 @@ export class UsbLiveSyncService extends usbLivesyncServiceBaseLib.UsbLiveSyncSer
4950
this.$projectDataService.initialize(this.$projectData.projectDir);
5051
let frameworkVersion = this.$projectDataService.getValue(platformData.frameworkPackageName).wait().version;
5152
if (semver.lt(frameworkVersion, "1.2.1")) {
52-
let shouldUpdate = this.$prompter.confirm("You need Android Runtime 1.2.1 or later for LiveSync to work properly. Do you want to update your runtime now?").wait();
53+
let shouldUpdate = this.$prompter.confirm(" You need Android Runtime 1.2.1 or later for LiveSync to work properly. Do you want to update your runtime now?").wait();
5354
if(shouldUpdate) {
5455
this.$platformService.updatePlatforms([this.$devicePlatformsConstants.Android.toLowerCase()]).wait();
5556
} else {
@@ -62,19 +63,9 @@ export class UsbLiveSyncService extends usbLivesyncServiceBaseLib.UsbLiveSyncSer
6263

6364
let projectFilesPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME);
6465

65-
let notInstalledAppOnDeviceAction = (device: Mobile.IDevice): IFuture<boolean> => {
66-
return (() => {
67-
this.$platformService.deployOnDevice(platform).wait();
68-
return false;
69-
}).future<boolean>()();
70-
};
66+
let notInstalledAppOnDeviceAction = (device: Mobile.IDevice): IFuture<void> => this.$platformService.deployOnDevice(platform);
7167

72-
let notRunningiOSSimulatorAction = (): IFuture<boolean> => {
73-
return (() => {
74-
this.$platformService.deployOnEmulator(this.$devicePlatformsConstants.iOS.toLowerCase()).wait();
75-
return false;
76-
}).future<boolean>()();
77-
};
68+
let notRunningiOSSimulatorAction = (): IFuture<void> => this.$platformService.deployOnEmulator(this.$devicePlatformsConstants.iOS.toLowerCase());
7869

7970
let beforeLiveSyncAction = (device: Mobile.IDevice, deviceAppData: Mobile.IDeviceAppData): IFuture<void> => {
8071
let platformSpecificUsbLiveSyncService = this.resolveUsbLiveSyncService(platform || this.$devicesService.platform, device);
@@ -128,6 +119,8 @@ export class UsbLiveSyncService extends usbLivesyncServiceBaseLib.UsbLiveSyncSer
128119
let mappedFilePath = beforeBatchLiveSyncAction(filePath).wait();
129120

130121
if (this.shouldSynciOSSimulator(platform).wait()) {
122+
this.$iOSEmulatorServices.transferFiles(this.$projectData.projectId, [filePath], iOSSimulatorRelativeToProjectBasePathAction).wait();
123+
131124
let platformSpecificUsbLiveSyncService = <IiOSUsbLiveSyncService>this.resolvePlatformSpecificLiveSyncService(platform || this.$devicesService.platform, null, platformSpecificLiveSyncServices);
132125
platformSpecificUsbLiveSyncService.sendPageReloadMessageToSimulator().wait();
133126
} else {
@@ -192,7 +185,11 @@ export class IOSUsbLiveSyncService implements IiOSUsbLiveSyncService {
192185
constructor(private _device: Mobile.IDevice,
193186
private $iOSSocketRequestExecutor: IiOSSocketRequestExecutor,
194187
private $iOSNotification: IiOSNotification,
195-
private $iOSEmulatorServices: Mobile.IiOSSimulatorService) { }
188+
private $iOSEmulatorServices: Mobile.IiOSSimulatorService,
189+
private $injector: IInjector,
190+
private $iOSNotificationService: IiOSNotificationService,
191+
private $errors: IErrors,
192+
private $projectData: IProjectData) { }
196193

197194
private get device(): Mobile.IiOSDevice {
198195
return <Mobile.IiOSDevice>this._device;
@@ -212,11 +209,8 @@ export class IOSUsbLiveSyncService implements IiOSUsbLiveSyncService {
212209
}
213210

214211
public sendPageReloadMessageToSimulator(): IFuture<void> {
215-
return (() => {
216-
this.$iOSEmulatorServices.postDarwinNotification(this.$iOSNotification.attachRequest).wait();
217-
let socket = net.connect(IOSUsbLiveSyncService.BACKEND_PORT);
218-
this.sendReloadMessageCore(socket);
219-
}).future<void>()();
212+
helpers.connectEventually(() => net.connect(IOSUsbLiveSyncService.BACKEND_PORT), (socket: net.Socket) => this.sendReloadMessageCore(socket));
213+
return this.$iOSEmulatorServices.postDarwinNotification(this.$iOSNotification.attachRequest);
220214
}
221215

222216
private sendReloadMessageCore(socket: net.Socket): void {
@@ -226,6 +220,7 @@ export class IOSUsbLiveSyncService implements IiOSUsbLiveSyncService {
226220
payload.writeInt32BE(length, 0);
227221
payload.write(message, 4, length, "utf16le");
228222
socket.write(payload);
223+
socket.destroy();
229224
}
230225
}
231226
$injector.register("iosUsbLiveSyncServiceLocator", {factory: IOSUsbLiveSyncService});

0 commit comments

Comments
 (0)