Skip to content

Commit 8b71556

Browse files
Expose getLiveSyncDeviceDescriptors method
Add new public method `getLiveSyncDeviceDescriptors` to `liveSyncService` - its purpose is to get information for current LiveSync operation.
1 parent b72bdbc commit 8b71556

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

PublicAPI.md

+23
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,29 @@ tns.liveSyncService.stopLiveSync(projectDir, deviceIdentifiers)
623623
});
624624
```
625625
626+
### getLiveSyncDeviceDescriptors
627+
Gives information for currently running LiveSync operation and parameters used to start it on each device.
628+
629+
* Definition
630+
```TypeScript
631+
/**
632+
* Returns the device information for current LiveSync operation of specified project.
633+
* In case LiveSync has been started on many devices, but stopped for some of them at a later point,
634+
* calling the method after that will return information only for devices for which LiveSync operation is in progress.
635+
* @param {string} projectDir The path to project for which the LiveSync operation is executed
636+
* @returns {ILiveSyncDeviceInfo[]} Array of elements describing parameters used to start LiveSync on each device.
637+
*/
638+
getLiveSyncDeviceDescriptors(projectDir: string): ILiveSyncDeviceInfo[];
639+
```
640+
641+
* Usage
642+
```JavaScript
643+
const projectDir = "myProjectDir";
644+
const deviceIdentifiers = [ "4df18f307d8a8f1b", "12318af23ebc0e25" ];
645+
const currentlyRunningDescriptors = tns.liveSyncService.getLiveSyncDeviceDescriptors(projectDir);
646+
console.log(`LiveSync for ${projectDir} is currently running on the following devices: ${currentlyRunningDescriptors.map(descriptor => descriptor.identifier)}`);
647+
```
648+
626649
### Events
627650
`liveSyncService` raises several events in order to provide information for current state of the operation.
628651
* liveSyncStarted - raised whenever CLI starts a LiveSync operation for specific device. When `liveSync` method is called, the initial LiveSync operation will emit `liveSyncStarted` for each specified device. After that the event will be emitted only in case when liveSync method is called again with different device instances. The event is raised with the following data:

lib/definitions/livesync.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ interface ILiveSyncService {
185185
* @returns {Promise<void>}
186186
*/
187187
stopLiveSync(projectDir: string, deviceIdentifiers?: string[], stopOptions?: { shouldAwaitAllActions: boolean }): Promise<void>;
188+
189+
/**
190+
* Returns the device information for current LiveSync operation of specified project.
191+
* In case LiveSync has been started on many devices, but stopped for some of them at a later point,
192+
* calling the method after that will return information only for devices for which LiveSync operation is in progress.
193+
* @param {string} projectDir The path to project for which the LiveSync operation is executed
194+
* @returns {ILiveSyncDeviceInfo[]} Array of elements describing parameters used to start LiveSync on each device.
195+
*/
196+
getLiveSyncDeviceDescriptors(projectDir: string): ILiveSyncDeviceInfo[];
188197
}
189198

190199
/**

lib/services/livesync/livesync-service.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class LiveSyncService extends EventEmitter implements ILiveSyncService {
5050

5151
const deviceIdentifiersToRemove = deviceIdentifiers || _.map(liveSyncProcessInfo.deviceDescriptors, d => d.identifier);
5252

53-
const removedDeviceIdentifiers = _.remove(liveSyncProcessInfo.deviceDescriptors, descriptor => _.indexOf(deviceIdentifiersToRemove, descriptor.identifier) !== -1)
53+
const removedDeviceIdentifiers = _.remove(liveSyncProcessInfo.deviceDescriptors, descriptor => _.includes(deviceIdentifiersToRemove, descriptor.identifier))
5454
.map(descriptor => descriptor.identifier);
5555

5656
// In case deviceIdentifiers are not passed, we should stop the whole LiveSync.
@@ -90,6 +90,12 @@ export class LiveSyncService extends EventEmitter implements ILiveSyncService {
9090
}
9191
}
9292

93+
public getLiveSyncDeviceDescriptors(projectDir: string): ILiveSyncDeviceInfo[] {
94+
const liveSyncProcessesInfo = this.liveSyncProcessesInfo[projectDir] || <ILiveSyncProcessInfo>{};
95+
const currentDescriptors = liveSyncProcessesInfo.deviceDescriptors;
96+
return currentDescriptors || [];
97+
}
98+
9399
protected async refreshApplication(projectData: IProjectData, liveSyncResultInfo: ILiveSyncResultInfo): Promise<void> {
94100
const platformLiveSyncService = this.getLiveSyncService(liveSyncResultInfo.deviceAppData.platform);
95101
try {
@@ -124,7 +130,8 @@ export class LiveSyncService extends EventEmitter implements ILiveSyncService {
124130
const isAlreadyLiveSyncing = this.liveSyncProcessesInfo[projectData.projectDir] && !this.liveSyncProcessesInfo[projectData.projectDir].isStopped;
125131

126132
// Prevent cases where liveSync is called consecutive times with the same device, for example [ A, B, C ] and then [ A, B, D ] - we want to execute initialSync only for D.
127-
const deviceDescriptorsForInitialSync = isAlreadyLiveSyncing ? _.differenceBy(deviceDescriptors, this.liveSyncProcessesInfo[projectData.projectDir].deviceDescriptors, deviceDescriptorPrimaryKey) : deviceDescriptors;
133+
const currentlyRunningDeviceDescriptors = this.getLiveSyncDeviceDescriptors(projectData.projectDir);
134+
const deviceDescriptorsForInitialSync = isAlreadyLiveSyncing ? _.differenceBy(deviceDescriptors, currentlyRunningDeviceDescriptors, deviceDescriptorPrimaryKey) : deviceDescriptors;
128135
this.setLiveSyncProcessInfo(liveSyncData.projectDir, deviceDescriptors);
129136

130137
await this.initialSync(projectData, deviceDescriptorsForInitialSync, liveSyncData);
@@ -143,7 +150,7 @@ export class LiveSyncService extends EventEmitter implements ILiveSyncService {
143150
this.liveSyncProcessesInfo[projectDir].currentSyncAction = this.liveSyncProcessesInfo[projectDir].actionsChain;
144151
this.liveSyncProcessesInfo[projectDir].isStopped = false;
145152

146-
const currentDeviceDescriptors = this.liveSyncProcessesInfo[projectDir].deviceDescriptors || [];
153+
const currentDeviceDescriptors = this.getLiveSyncDeviceDescriptors(projectDir);
147154
this.liveSyncProcessesInfo[projectDir].deviceDescriptors = _.uniqBy(currentDeviceDescriptors.concat(deviceDescriptors), deviceDescriptorPrimaryKey);
148155
}
149156

test/stubs.ts

+4
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,10 @@ export class LiveSyncServiceStub implements ILiveSyncService {
500500
public async stopLiveSync(projectDir: string): Promise<void> {
501501
return;
502502
}
503+
504+
public getLiveSyncDeviceDescriptors(projectDir: string): ILiveSyncDeviceInfo[] {
505+
return [];
506+
}
503507
}
504508

505509
export class AndroidToolsInfoStub implements IAndroidToolsInfo {

0 commit comments

Comments
 (0)