Skip to content

Commit cb8a666

Browse files
committed
feat: use long living socket connection for sync
1 parent 9dd6f20 commit cb8a666

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

lib/definitions/livesync.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,11 @@ interface IAndroidNativeScriptDeviceLiveSyncService extends INativeScriptDeviceL
398398
}
399399

400400
interface IAndroidLivesyncTool {
401+
/**
402+
* The protocol version the current app(adnroid runtime) is using.
403+
*/
404+
protocolVersion: string;
405+
401406
/**
402407
* Creates new socket connection.
403408
* @param configuration - The configuration to the socket connection.
@@ -457,6 +462,11 @@ interface IAndroidLivesyncTool {
457462
* @param error - Optional error for rejecting pending sync operations
458463
*/
459464
end(error?: Error): void;
465+
466+
/**
467+
* Returns true if a connection has been already established
468+
*/
469+
hasConnection(): boolean;
460470
}
461471

462472
interface IAndroidLivesyncToolConfiguration {

lib/services/livesync/android-device-livesync-sockets-service.ts

+26-11
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import { LiveSyncPaths } from "../../common/constants";
66
import { AndroidLivesyncTool } from "./android-livesync-tool";
77
import * as path from "path";
88
import * as temp from "temp";
9+
import * as semver from "semver";
910

1011
export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBase implements IAndroidNativeScriptDeviceLiveSyncService, INativeScriptDeviceLiveSyncService {
1112
private livesyncTool: IAndroidLivesyncTool;
1213
private static STATUS_UPDATE_INTERVAL = 10000;
14+
private static MINIMAL_VERSION_LONG_LIVING_CONNECTION = "0.2.0";
1315

1416
constructor(
1517
private data: IProjectData,
@@ -20,19 +22,18 @@ export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBa
2022
protected device: Mobile.IAndroidDevice,
2123
private $options: ICommonOptions,
2224
private $processService: IProcessService,
23-
private $fs: IFileSystem) {
25+
private $fs: IFileSystem,
26+
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants) {
2427
super($platformsData, device);
2528
this.livesyncTool = this.$injector.resolve(AndroidLivesyncTool);
2629
}
2730

2831
public async beforeLiveSyncAction(deviceAppData: Mobile.IDeviceAppData): Promise<void> {
29-
const platformData = this.$platformsData.getPlatformData(deviceAppData.platform, this.data);
30-
const projectFilesPath = path.join(platformData.appDestinationDirectoryPath, APP_FOLDER_NAME);
3132
const pathToLiveSyncFile = temp.path({ prefix: "livesync" });
3233
this.$fs.writeFile(pathToLiveSyncFile, "");
3334
await this.device.fileSystem.putFile(pathToLiveSyncFile, this.getPathToLiveSyncFileOnDevice(deviceAppData.appIdentifier), deviceAppData.appIdentifier);
3435
await this.device.applicationManager.startApplication({ appId: deviceAppData.appIdentifier, projectName: this.data.projectName, justLaunch: true });
35-
await this.connectLivesyncTool(projectFilesPath, this.data.projectId);
36+
await this.connectLivesyncTool(this.data.projectId);
3637
}
3738

3839
private getPathToLiveSyncFileOnDevice(appIdentifier: string): string {
@@ -42,8 +43,11 @@ export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBa
4243
public async finalizeSync(liveSyncInfo: ILiveSyncResultInfo, projectData: IProjectData): Promise<IAndroidLivesyncSyncOperationResult> {
4344
try {
4445
const result = await this.doSync(liveSyncInfo, projectData);
46+
if (!semver.gte(this.livesyncTool.protocolVersion, AndroidDeviceSocketsLiveSyncService.MINIMAL_VERSION_LONG_LIVING_CONNECTION)) {
47+
this.livesyncTool.end();
48+
}
4549
return result;
46-
} finally {
50+
} catch (e) {
4751
this.livesyncTool.end();
4852
}
4953
}
@@ -85,6 +89,13 @@ export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBa
8589
const canExecuteFastSync = !liveSyncInfo.isFullSync && this.canExecuteFastSyncForPaths(liveSyncInfo.modifiedFilesData, projectData, this.device.deviceInfo.platform);
8690
if (!canExecuteFastSync || !liveSyncInfo.didRefresh) {
8791
await this.device.applicationManager.restartApplication({ appId: liveSyncInfo.deviceAppData.appIdentifier, projectName: projectData.projectName });
92+
if (this.livesyncTool.protocolVersion && semver.gte(this.livesyncTool.protocolVersion, AndroidDeviceSocketsLiveSyncService.MINIMAL_VERSION_LONG_LIVING_CONNECTION)) {
93+
try {
94+
await this.connectLivesyncTool(liveSyncInfo.deviceAppData.appIdentifier);
95+
} catch (e) {
96+
this.$logger.trace("Failed to connect after app restart.");
97+
}
98+
}
8899
}
89100
}
90101

@@ -143,12 +154,16 @@ export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBa
143154
return transferredLocalToDevicePaths;
144155
}
145156

146-
private async connectLivesyncTool(projectFilesPath: string, appIdentifier: string) {
147-
await this.livesyncTool.connect({
148-
appIdentifier,
149-
deviceIdentifier: this.device.deviceInfo.identifier,
150-
appPlatformsPath: projectFilesPath
151-
});
157+
private async connectLivesyncTool(appIdentifier: string) {
158+
const platformData = this.$platformsData.getPlatformData(this.$devicePlatformsConstants.Android, this.data);
159+
const projectFilesPath = path.join(platformData.appDestinationDirectoryPath, APP_FOLDER_NAME);
160+
if (!this.livesyncTool.hasConnection()) {
161+
await this.livesyncTool.connect({
162+
appIdentifier,
163+
deviceIdentifier: this.device.deviceInfo.identifier,
164+
appPlatformsPath: projectFilesPath
165+
});
166+
}
152167
}
153168

154169
public getDeviceHashService(appIdentifier: string): Mobile.IAndroidDeviceHashService {

lib/services/livesync/android-livesync-tool.ts

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const TRY_CONNECT_TIMEOUT = 30000;
2020
const DEFAULT_LOCAL_HOST_ADDRESS = "127.0.0.1";
2121

2222
export class AndroidLivesyncTool implements IAndroidLivesyncTool {
23+
public protocolVersion: string;
2324
private operationPromises: IDictionary<any>;
2425
private socketError: string | Error;
2526
private socketConnection: INetSocket;
@@ -179,6 +180,10 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
179180
}
180181
}
181182

183+
public hasConnection(): boolean {
184+
return !!this.socketConnection;
185+
}
186+
182187
private sendFileHeader(filePath: string): Promise<void> {
183188
return new Promise((resolve, reject) => {
184189
let error;
@@ -292,6 +297,7 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
292297
const protocolVersion = versionBuffer.toString();
293298
const appIdentifier = appIdentifierBuffer.toString();
294299
this.$logger.trace(`Handle socket connection for app identifier: ${appIdentifier} with protocol version: ${protocolVersion}.`);
300+
this.protocolVersion = protocolVersion;
295301

296302
this.socketConnection.on("data", (connectionData: NodeBuffer) => this.handleData(socket.uid, connectionData));
297303
this.socketConnection.on("close", (hasError: boolean) => this.handleSocketClose(socket.uid, hasError));

0 commit comments

Comments
 (0)