Skip to content

Commit f36f0e6

Browse files
Fix execution of before-prepare hooks in a long living process (#3140)
When LiveSync operation is started we set `usbLiveSyncService.isInitialized` to true. This variable is used in several `before-prepare` hooks. For example `nativescript-dev-typescript` has the following logic: - When before-prepare hook is executed it checks if the `usbLiveSyncService.isInitialized` property is set to true. In this case it does nothing. The idea is that when LiveSync watch process is started, the plugin has started `tsc --watch` process, so there's no need to do anything on `before-prepare`. However, in a long living process, where the project may be changed, whenever we stop LiveSync process, we must set usbLiveSyncService to false. In case we do not do it, the property remains true and the before-prepare hook of the other project does nothing and the project is not transpiled.
1 parent 63ef7ef commit f36f0e6

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

lib/services/livesync/livesync-service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
3636
private $errors: IErrors,
3737
private $debugDataService: IDebugDataService,
3838
private $analyticsService: IAnalyticsService,
39+
private $usbLiveSyncService: DeprecatedUsbLiveSyncService,
3940
private $injector: IInjector) {
4041
super();
4142
}
@@ -86,6 +87,10 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
8687
projectData
8788
}
8889
});
90+
91+
// In case we are stopping the LiveSync we must set usbLiveSyncService.isInitialized to false,
92+
// as in case we execute nativescript-dev-typescript's before-prepare hook again in the same process, it MUST transpile the files.
93+
this.$usbLiveSyncService.isInitialized = false;
8994
} else if (liveSyncProcessInfo.currentSyncAction && shouldAwaitPendingOperation) {
9095
await liveSyncProcessInfo.currentSyncAction;
9196
}
@@ -321,7 +326,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
321326

322327
if (!liveSyncData.skipWatcher && this.liveSyncProcessesInfo[projectData.projectDir].deviceDescriptors.length) {
323328
// Should be set after prepare
324-
this.$injector.resolve<DeprecatedUsbLiveSyncService>("usbLiveSyncService").isInitialized = true;
329+
this.$usbLiveSyncService.isInitialized = true;
325330

326331
await this.startWatcher(projectData, liveSyncData);
327332
}

test/services/livesync-service.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Yok } from "../../lib/common/yok";
22
import { assert } from "chai";
3-
import { LiveSyncService } from "../../lib/services/livesync/livesync-service";
3+
import { LiveSyncService, DeprecatedUsbLiveSyncService } from "../../lib/services/livesync/livesync-service";
44
import { LoggerStub } from "../stubs";
55

66
const createTestInjector = (): IInjector => {
@@ -27,6 +27,9 @@ const createTestInjector = (): IInjector => {
2727
testInjector.register("pluginsService", {});
2828
testInjector.register("analyticsService", {});
2929
testInjector.register("injector", testInjector);
30+
testInjector.register("usbLiveSyncService", {
31+
isInitialized: false
32+
});
3033

3134
return testInjector;
3235
};
@@ -46,6 +49,7 @@ class LiveSyncServiceInheritor extends LiveSyncService {
4649
$errors: IErrors,
4750
$debugDataService: IDebugDataService,
4851
$analyticsService: IAnalyticsService,
52+
$usbLiveSyncService: DeprecatedUsbLiveSyncService,
4953
$injector: IInjector) {
5054

5155
super(
@@ -63,7 +67,9 @@ class LiveSyncServiceInheritor extends LiveSyncService {
6367
$errors,
6468
$debugDataService,
6569
$analyticsService,
66-
$injector
70+
$usbLiveSyncService,
71+
$injector,
72+
6773
);
6874
}
6975

@@ -154,6 +160,34 @@ describe("liveSyncService", () => {
154160
});
155161
}
156162

163+
const prepareTestForUsbLiveSyncService = (): any => {
164+
const testInjector = createTestInjector();
165+
const liveSyncService = testInjector.resolve<LiveSyncServiceInheritor>(LiveSyncServiceInheritor);
166+
const projectDir = "projectDir";
167+
const usbLiveSyncService = testInjector.resolve<DeprecatedUsbLiveSyncService>("usbLiveSyncService");
168+
usbLiveSyncService.isInitialized = true;
169+
170+
// Setup liveSyncProcessesInfo for current test
171+
liveSyncService.liveSyncProcessesInfo[projectDir] = getLiveSyncProcessInfo();
172+
const deviceDescriptors = ["device1", "device2", "device3"].map(d => getDeviceDescriptor(d));
173+
liveSyncService.liveSyncProcessesInfo[projectDir].deviceDescriptors.push(...deviceDescriptors);
174+
return { projectDir, liveSyncService, usbLiveSyncService };
175+
};
176+
177+
it("sets usbLiveSyncService.isInitialized to false when LiveSync is stopped for all devices", async () => {
178+
const { projectDir, liveSyncService, usbLiveSyncService } = prepareTestForUsbLiveSyncService();
179+
await liveSyncService.stopLiveSync(projectDir, ["device1", "device2", "device3"]);
180+
181+
assert.isFalse(usbLiveSyncService.isInitialized, "When the LiveSync process is stopped, we must set usbLiveSyncService.isInitialized to false");
182+
});
183+
184+
it("does not set usbLiveSyncService.isInitialized to false when LiveSync is stopped for some of devices only", async () => {
185+
const { projectDir, liveSyncService, usbLiveSyncService } = prepareTestForUsbLiveSyncService();
186+
await liveSyncService.stopLiveSync(projectDir, ["device1", "device2"]);
187+
188+
assert.isTrue(usbLiveSyncService.isInitialized, "When the LiveSync process is stopped only for some of the devices, we must not set usbLiveSyncService.isInitialized to false");
189+
});
190+
157191
});
158192

159193
});

0 commit comments

Comments
 (0)