Skip to content

Commit 823f1c5

Browse files
rosen-vladimirovDimitar Kerezov
authored andcommitted
Add events for LiveSync
1 parent 1765397 commit 823f1c5

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

lib/services/livesync/livesync-service.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@ import { exported } from "../../common/decorators";
77
import { hook } from "../../common/helpers";
88

99
const LiveSyncEvents = {
10-
liveSyncStarted: "liveSyncStarted",
1110
liveSyncStopped: "liveSyncStopped",
12-
liveSyncError: "liveSyncError", // Do we need this or we can use liveSyncStopped event?
13-
liveSyncWatcherStarted: "liveSyncWatcherStarted",
14-
liveSyncWatcherStopped: "liveSyncWatcherStopped",
15-
liveSyncFileChangedEvent: "liveSyncFileChangedEvent",
16-
liveSyncOperationStartingEvent: "liveSyncOperationStartedEvent"
11+
liveSyncError: "error", // Do we need this or we can use liveSyncStopped event?
12+
liveSyncFileChangedEvent: "fileChanged",
13+
liveSyncCompleted: "liveSyncCompleted"
1714
};
1815

1916
// TODO: emit events for "successfull livesync", "stoppedLivesync",
@@ -39,8 +36,6 @@ export class LiveSyncService extends EventEmitter implements ILiveSyncService {
3936
@hook("liveSync")
4037
public async liveSync(deviceDescriptors: ILiveSyncDeviceInfo[],
4138
liveSyncData: ILiveSyncInfo): Promise<void> {
42-
43-
this.emit(LiveSyncEvents.liveSyncStarted, { projectDir: liveSyncData.projectDir, deviceIdentifiers: deviceDescriptors.map(dd => dd.identifier) });
4439
// TODO: Initialize devicesService before that.
4540
const projectData = this.$projectDataService.getProjectData(liveSyncData.projectDir);
4641
await this.initialSync(projectData, deviceDescriptors, liveSyncData);
@@ -66,16 +61,30 @@ export class LiveSyncService extends EventEmitter implements ILiveSyncService {
6661
liveSyncProcessInfo.watcher.close();
6762
}
6863

69-
delete this.liveSyncProcessesInfo[projectDir];
64+
if (liveSyncProcessInfo.actionsChain) {
65+
await liveSyncProcessInfo.actionsChain;
66+
}
67+
68+
liveSyncProcessInfo.isStopped = true;
7069

7170
// Kill typescript watcher
71+
// TODO: Pass the projectDir in hooks args.
7272
await this.$hooksService.executeAfterHooks('watch');
73+
74+
this.emit(LiveSyncEvents.liveSyncStopped, { projectDir });
7375
}
7476
}
7577

7678
protected async refreshApplication(projectData: IProjectData, liveSyncResultInfo: ILiveSyncResultInfo): Promise<void> {
7779
const platformLiveSyncService = this.getLiveSyncService(liveSyncResultInfo.deviceAppData.platform);
7880
await platformLiveSyncService.refreshApplication(projectData, liveSyncResultInfo);
81+
82+
this.emit(LiveSyncEvents.liveSyncCompleted, {
83+
projectDir: projectData.projectDir,
84+
applicationIdentifier: projectData.projectId,
85+
syncedFiles: liveSyncResultInfo.modifiedFilesData.map(m => m.getLocalPath()),
86+
deviceIdentifier: liveSyncResultInfo.deviceAppData.device.deviceInfo.identifier
87+
});
7988
}
8089

8190
// TODO: Register both livesync services in injector
@@ -140,7 +149,6 @@ export class LiveSyncService extends EventEmitter implements ILiveSyncService {
140149

141150
const liveSyncResultInfo = await this.getLiveSyncService(platform).fullSync({ projectData, device, syncAllFiles: liveSyncData.watchAllFiles, useLiveEdit: liveSyncData.useLiveEdit });
142151
await this.refreshApplication(projectData, liveSyncResultInfo);
143-
//await device.applicationManager.restartApplication(projectData.projectId, projectData.projectName);
144152
};
145153

146154
await this.$devicesService.execute(deviceAction, (device: Mobile.IDevice) => _.some(deviceDescriptors, deviceDescriptor => deviceDescriptor.identifier === device.deviceInfo.identifier));
@@ -220,9 +228,16 @@ export class LiveSyncService extends EventEmitter implements ILiveSyncService {
220228
this.$logger.info("Try saving it again or restart the livesync operation.");
221229
// we can remove the descriptor from action:
222230
const allErrors = err.allErrors;
223-
console.log(allErrors);
224231
_.each(allErrors, (deviceError: any) => {
225-
console.log("for error: ", deviceError, " device ID: ", deviceError.deviceIdentifier);
232+
this.$logger.warn(`Unable to apply changes for device: ${deviceError.deviceIdentifier}. Error is: ${deviceError.message}.`);
233+
234+
this.emit(LiveSyncEvents.liveSyncError, {
235+
error: deviceError,
236+
deviceIdentifier: deviceError.deviceIdentifier,
237+
projectDir: projectData.projectDir,
238+
applicationIdentifier: projectData.projectId
239+
});
240+
226241
removeDeviceDescriptor(deviceError.deviceIdentifier);
227242
});
228243
}
@@ -245,12 +260,18 @@ export class LiveSyncService extends EventEmitter implements ILiveSyncService {
245260
ignored: ["**/.*", ".*"] // hidden files
246261
};
247262

248-
this.emit(LiveSyncEvents.liveSyncWatcherStarted, { projectDir: liveSyncData.projectDir, deviceIdentifiers: deviceDescriptors.map(dd => dd.identifier), watcherOptions, });
249-
250263
const watcher = choki.watch(pattern, watcherOptions)
251264
.on("all", async (event: string, filePath: string) => {
252265
clearTimeout(timeoutTimer);
253266

267+
this.emit(LiveSyncEvents.liveSyncFileChangedEvent, {
268+
projectDir: liveSyncData.projectDir,
269+
applicationIdentifier: projectData.projectId,
270+
deviceIdentifiers: deviceDescriptors.map(dd => dd.identifier),
271+
modifiedFile: filePath,
272+
event
273+
});
274+
254275
filePath = path.join(liveSyncData.projectDir, filePath);
255276

256277
this.$logger.trace(`Chokidar raised event ${event} for ${filePath}.`);

0 commit comments

Comments
 (0)