-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathios-livesync-service.ts
69 lines (60 loc) · 2.67 KB
/
ios-livesync-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
///<reference path="../../.d.ts"/>
"use strict";
import liveSyncServiceBaseLib = require("./livesync-service-base");
import * as helpers from "../../common/helpers";
import * as net from "net";
let currentPageReloadId = 0;
class IOSLiveSyncService extends liveSyncServiceBaseLib.LiveSyncServiceBase<Mobile.IiOSDevice> implements IPlatformLiveSyncService {
private static BACKEND_PORT = 18181;
constructor(_device: Mobile.IDevice,
private $iOSSocketRequestExecutor: IiOSSocketRequestExecutor,
private $iOSNotification: IiOSNotification,
private $iOSEmulatorServices: Mobile.IiOSSimulatorService,
private $injector: IInjector,
private $logger: ILogger) {
super(_device);
}
public removeFiles(appIdentifier: string, localToDevicePaths: Mobile.ILocalToDevicePathData[]): IFuture<void> {
return (() => {
_.each(localToDevicePaths, localToDevicePathData => this.device.fileSystem.deleteFile(localToDevicePathData.getDevicePath(), appIdentifier));
}).future<void>()();
}
protected restartApplication(deviceAppData: Mobile.IDeviceAppData): IFuture<void> {
let projectData: IProjectData = this.$injector.resolve("projectData");
return this.device.applicationManager.restartApplication(deviceAppData.appIdentifier, projectData.projectName);
}
protected reloadPage(deviceAppData: Mobile.IDeviceAppData): IFuture<void> {
return (() => {
let timeout = 9000;
if (this.device.isEmulator) {
helpers.connectEventually(() => net.connect(IOSLiveSyncService.BACKEND_PORT), (socket: net.Socket) => this.sendPageReloadMessage(socket));
this.$iOSEmulatorServices.postDarwinNotification(this.$iOSNotification.attachRequest).wait();
} else {
this.$iOSSocketRequestExecutor.executeAttachRequest(this.device, timeout).wait();
let socket = this.device.connectToPort(IOSLiveSyncService.BACKEND_PORT);
this.sendPageReloadMessage(socket);
}
}).future<void>()();
}
private sendPageReloadMessage(socket: net.Socket): void {
try {
this.sendPageReloadMessageCore(socket);
socket.once("data", (data: NodeBuffer|string) => {
this.$logger.trace(`Socket sent data: ${data.toString()}`);
socket.destroy();
});
} catch(err) {
this.$logger.trace("Error while sending page reload:", err);
socket.destroy();
}
}
private sendPageReloadMessageCore(socket: net.Socket): void {
let message = `{ "method":"Page.reload","params":{"ignoreCache":false},"id":${++currentPageReloadId} }`;
let length = Buffer.byteLength(message, "utf16le");
let payload = new Buffer(length + 4);
payload.writeInt32BE(length, 0);
payload.write(message, 4, length, "utf16le");
socket.write(payload);
}
}
$injector.register("iosLiveSyncServiceLocator", {factory: IOSLiveSyncService});