@@ -6,10 +6,12 @@ import { LiveSyncPaths } from "../../common/constants";
66import { AndroidLivesyncTool } from "./android-livesync-tool" ;
77import * as path from "path" ;
88import * as temp from "temp" ;
9+ import * as semver from "semver" ;
910
1011export 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 {
0 commit comments