-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathios-livesync-service.ts
127 lines (111 loc) · 3.98 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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;
private socket: net.Socket;
constructor(_device: Mobile.IDevice,
private $iOSSocketRequestExecutor: IiOSSocketRequestExecutor,
private $iOSNotification: IiOSNotification,
private $iOSEmulatorServices: Mobile.IiOSSimulatorService,
private $injector: IInjector,
private $logger: ILogger,
private $options: IOptions,
$liveSyncProvider: ILiveSyncProvider) {
super(_device, $liveSyncProvider);
}
public afterInstallApplicationAction(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[]): IFuture<boolean> {
return (() => {
return this.$options.watch;
}).future<boolean>()();
}
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) {
if (!this.socket) {
helpers.connectEventually(() => net.connect(IOSLiveSyncService.BACKEND_PORT), (socket: net.Socket) => {
this.socket = socket;
this.attachEventHandlersIfNecessary();
this.sendPageReloadMessage();
});
} else {
this.sendPageReloadMessage();
}
this.$iOSEmulatorServices.postDarwinNotification(this.$iOSNotification.attachRequest).wait();
} else {
if(!this.socket) {
this.$iOSSocketRequestExecutor.executeAttachRequest(this.device, timeout).wait();
this.socket = this.device.connectToPort(IOSLiveSyncService.BACKEND_PORT);
this.attachEventHandlersIfNecessary();
}
this.sendPageReloadMessage();
}
}).future<void>()();
}
private attachEventHandlersIfNecessary(): void {
if(this.$options.watch) {
this.attachProcessExitHandlers();
this.attachSocketCloseEvent();
}
}
private attachSocketCloseEvent(): void {
this.socket.on("close", (hadError: boolean) => {
this.$logger.trace(`Socket closed, hadError is ${hadError}.`);
this.socket = null;
});
}
private sendPageReloadMessage(): void {
try {
this.sendPageReloadMessageCore();
this.socket.on("data", (data: NodeBuffer|string) => {
this.$logger.trace(`Socket sent data: ${data.toString()}`);
this.destroySocketIfNecessary();
});
} catch(err) {
this.$logger.trace("Error while sending page reload:", err);
this.destroySocketIfNecessary();
}
}
private sendPageReloadMessageCore(): 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");
this.socket.write(payload);
}
private attachProcessExitHandlers(): void {
process.on("exit", (exitCode: number) => {
this.destroySocket();
});
process.on("SIGTERM", () => {
this.destroySocket();
});
process.on("SIGINT", () => {
this.destroySocket();
});
}
private destroySocketIfNecessary(): void {
if(!this.$options.watch) {
this.destroySocket();
}
}
private destroySocket(): void {
if(this.socket) {
this.socket.destroy();
this.socket = null;
}
}
}
$injector.register("iosLiveSyncServiceLocator", {factory: IOSLiveSyncService});