Skip to content

Commit 7f708a4

Browse files
fix: preview and run do not work correctly
In case there are multiple devices with the same platform, `run command` fails with error `Cannot read property hasNativeChanges of undefined`. The problem is that we return incorrect prepare data. Also we have a problem in `tns preview` when multiple platforms are used - all files are send multiple times to each device as we have too many started webpack processes (in case you scan fast with devices) and we are also adding handler of the prepare ready event per each device instead per platform. Fix this by handling the event only once per platform
1 parent 49a148f commit 7f708a4

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

lib/controllers/prepare-controller.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as child_process from "child_process";
21
import * as choki from "chokidar";
32
import { hook } from "../common/helpers";
43
import { performanceLog } from "../common/decorators";
@@ -7,7 +6,7 @@ import * as path from "path";
76
import { PREPARE_READY_EVENT_NAME, WEBPACK_COMPILATION_COMPLETE, PACKAGE_JSON_FILE_NAME, PLATFORMS_DIR_NAME } from "../constants";
87

98
interface IPlatformWatcherData {
10-
webpackCompilerProcess: child_process.ChildProcess;
9+
hasWebpackCompilerProcess: boolean;
1110
nativeFilesWatcher: choki.FSWatcher;
1211
}
1312

@@ -63,9 +62,9 @@ export class PrepareController extends EventEmitter {
6362
this.watchersData[projectDir][platformLowerCase].nativeFilesWatcher = null;
6463
}
6564

66-
if (this.watchersData && this.watchersData[projectDir] && this.watchersData[projectDir][platformLowerCase] && this.watchersData[projectDir][platformLowerCase].webpackCompilerProcess) {
65+
if (this.watchersData && this.watchersData[projectDir] && this.watchersData[projectDir][platformLowerCase] && this.watchersData[projectDir][platformLowerCase].hasWebpackCompilerProcess) {
6766
await this.$webpackCompilerService.stopWebpackCompiler(platform);
68-
this.watchersData[projectDir][platformLowerCase].webpackCompilerProcess = null;
67+
this.watchersData[projectDir][platformLowerCase].hasWebpackCompilerProcess = false;
6968
}
7069
}
7170

@@ -78,38 +77,39 @@ export class PrepareController extends EventEmitter {
7877
if (!this.watchersData[projectData.projectDir][platformData.platformNameLowerCase]) {
7978
this.watchersData[projectData.projectDir][platformData.platformNameLowerCase] = {
8079
nativeFilesWatcher: null,
81-
webpackCompilerProcess: null
80+
hasWebpackCompilerProcess: false
8281
};
83-
await this.startJSWatcherWithPrepare(platformData, projectData, prepareData); // -> start watcher + initial compilation
84-
const hasNativeChanges = await this.startNativeWatcherWithPrepare(platformData, projectData, prepareData); // -> start watcher + initial prepare
85-
const result = { platform: platformData.platformNameLowerCase, hasNativeChanges };
82+
}
8683

87-
const hasPersistedDataWithNativeChanges = this.persistedData.find(data => data.platform === result.platform && data.hasNativeChanges);
88-
if (hasPersistedDataWithNativeChanges) {
89-
result.hasNativeChanges = true;
90-
}
84+
await this.startJSWatcherWithPrepare(platformData, projectData, prepareData); // -> start watcher + initial compilation
85+
const hasNativeChanges = await this.startNativeWatcherWithPrepare(platformData, projectData, prepareData); // -> start watcher + initial prepare
86+
const result = { platform: platformData.platformNameLowerCase, hasNativeChanges };
9187

92-
// TODO: Do not persist this in `this` context. Also it should be per platform.
93-
this.isInitialPrepareReady = true;
88+
const hasPersistedDataWithNativeChanges = this.persistedData.find(data => data.platform === result.platform && data.hasNativeChanges);
89+
if (hasPersistedDataWithNativeChanges) {
90+
result.hasNativeChanges = true;
91+
}
9492

95-
if (this.persistedData && this.persistedData.length) {
96-
this.emitPrepareEvent({ files: [], hasOnlyHotUpdateFiles: false, hasNativeChanges: result.hasNativeChanges, hmrData: null, platform: platformData.platformNameLowerCase });
97-
}
93+
// TODO: Do not persist this in `this` context. Also it should be per platform.
94+
this.isInitialPrepareReady = true;
9895

99-
return result;
96+
if (this.persistedData && this.persistedData.length) {
97+
this.emitPrepareEvent({ files: [], hasOnlyHotUpdateFiles: false, hasNativeChanges: result.hasNativeChanges, hmrData: null, platform: platformData.platformNameLowerCase });
10098
}
99+
100+
return result;
101101
}
102102

103103
private async startJSWatcherWithPrepare(platformData: IPlatformData, projectData: IProjectData, prepareData: IPrepareData): Promise<void> {
104-
if (!this.watchersData[projectData.projectDir][platformData.platformNameLowerCase].webpackCompilerProcess) {
104+
if (!this.watchersData[projectData.projectDir][platformData.platformNameLowerCase].hasWebpackCompilerProcess) {
105105
this.$webpackCompilerService.on(WEBPACK_COMPILATION_COMPLETE, data => {
106106
if (data.platform.toLowerCase() === platformData.platformNameLowerCase) {
107107
this.emitPrepareEvent({ ...data, hasNativeChanges: false });
108108
}
109109
});
110110

111-
const childProcess = await this.$webpackCompilerService.compileWithWatch(platformData, projectData, prepareData);
112-
this.watchersData[projectData.projectDir][platformData.platformNameLowerCase].webpackCompilerProcess = childProcess;
111+
this.watchersData[projectData.projectDir][platformData.platformNameLowerCase].hasWebpackCompilerProcess = true;
112+
await this.$webpackCompilerService.compileWithWatch(platformData, projectData, prepareData);
113113
}
114114
}
115115

lib/controllers/preview-app-controller.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ export class PreviewAppController extends EventEmitter implements IPreviewAppCon
7979
this.platformPrepareHandlers[device.platform] = true;
8080

8181
// TODO: Remove the handler once the preview operation for this platform is stopped
82-
this.$prepareController.on(PREPARE_READY_EVENT_NAME, async currentPrepareData => {
83-
await this.handlePrepareReadyEvent(data, currentPrepareData.hmrData, currentPrepareData.files, device.platform);
82+
this.$prepareController.on(PREPARE_READY_EVENT_NAME, async (currentPrepareData: IFilesChangeEventData) => {
83+
if (currentPrepareData.platform.toLowerCase() === device.platform.toLowerCase()) {
84+
await this.handlePrepareReadyEvent(data, currentPrepareData.hmrData, currentPrepareData.files, device.platform);
85+
}
8486
});
8587

8688
}

0 commit comments

Comments
 (0)