Skip to content

Commit 0126afc

Browse files
Fix multiple typescript/sass watchers (#3332)
* Fix multiple typescript/sass watchers Whenever using CLI as a library, calling livesync multiple times leads to multiple typescript/sass watchers. This happens due to the fact that `currentWatcherInfo.pattern` is an array and checking two arrays for equality with `!==` is bound to return `true` at all times. `toString()` both arrays and check the string literals instead. * Fix comments
1 parent 1658629 commit 0126afc

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

lib/definitions/livesync.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ interface ILiveSyncProcessInfo {
6060
timer: NodeJS.Timer;
6161
watcherInfo: {
6262
watcher: IFSWatcher,
63-
pattern: string | string[]
63+
patterns: string[]
6464
};
6565
actionsChain: Promise<any>;
6666
isStopped: boolean;

lib/services/livesync/livesync-service.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -513,21 +513,21 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
513513
}
514514

515515
private async startWatcher(projectData: IProjectData, liveSyncData: ILiveSyncInfo): Promise<void> {
516-
const pattern = [APP_FOLDER_NAME];
516+
const patterns = [APP_FOLDER_NAME];
517517

518518
if (liveSyncData.watchAllFiles) {
519519
const productionDependencies = this.$nodeModulesDependenciesBuilder.getProductionDependencies(projectData.projectDir);
520-
pattern.push(PACKAGE_JSON_FILE_NAME);
520+
patterns.push(PACKAGE_JSON_FILE_NAME);
521521

522522
// watch only production node_module/packages same one prepare uses
523523
for (const index in productionDependencies) {
524-
pattern.push(productionDependencies[index].directory);
524+
patterns.push(productionDependencies[index].directory);
525525
}
526526
}
527527

528528
const currentWatcherInfo = this.liveSyncProcessesInfo[liveSyncData.projectDir].watcherInfo;
529-
530-
if (!currentWatcherInfo || currentWatcherInfo.pattern !== pattern) {
529+
const areWatcherPatternsDifferent = () => _.xor(currentWatcherInfo.patterns, patterns).length;
530+
if (!currentWatcherInfo || areWatcherPatternsDifferent()) {
531531
if (currentWatcherInfo) {
532532
currentWatcherInfo.watcher.close();
533533
}
@@ -630,7 +630,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
630630
ignored: ["**/.*", ".*"] // hidden files
631631
};
632632

633-
const watcher = choki.watch(pattern, watcherOptions)
633+
const watcher = choki.watch(patterns, watcherOptions)
634634
.on("all", async (event: string, filePath: string) => {
635635
clearTimeout(timeoutTimer);
636636

@@ -650,7 +650,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
650650
}
651651
});
652652

653-
this.liveSyncProcessesInfo[liveSyncData.projectDir].watcherInfo = { watcher, pattern };
653+
this.liveSyncProcessesInfo[liveSyncData.projectDir].watcherInfo = { watcher, patterns };
654654
this.liveSyncProcessesInfo[liveSyncData.projectDir].timer = timeoutTimer;
655655

656656
this.$processService.attachToProcessExitSignals(this, () => {

test/services/livesync-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ describe("liveSyncService", () => {
9494
watcher: <any>{
9595
close: (): any => undefined
9696
},
97-
pattern: "pattern"
97+
patterns: ["pattern"]
9898
},
9999
deviceDescriptors: []
100100
});

0 commit comments

Comments
 (0)