Skip to content

Commit 5faf40d

Browse files
Merge pull request #4790 from NativeScript/vladimirov/fix-preview
fix: handle preview issues
2 parents ece5c31 + d6af7bf commit 5faf40d

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

lib/controllers/prepare-controller.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,24 @@ export class PrepareController extends EventEmitter {
8080
nativeFilesWatcher: null,
8181
webpackCompilerProcess: null
8282
};
83-
}
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 };
8486

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

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

94-
this.isInitialPrepareReady = true;
95+
if (this.persistedData && this.persistedData.length) {
96+
this.emitPrepareEvent({ files: [], hasOnlyHotUpdateFiles: false, hasNativeChanges: result.hasNativeChanges, hmrData: null, platform: platformData.platformNameLowerCase });
97+
}
9598

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

103103
private async startJSWatcherWithPrepare(platformData: IPlatformData, projectData: IProjectData, prepareData: IPrepareData): Promise<void> {

lib/controllers/preview-app-controller.ts

+22-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { PrepareDataService } from "../services/prepare-data-service";
99
import { PreviewAppLiveSyncEvents } from "../services/livesync/playground/preview-app-constants";
1010

1111
export class PreviewAppController extends EventEmitter implements IPreviewAppController {
12-
private deviceInitializationPromise: IDictionary<Promise<FilesPayload>> = {};
12+
private deviceInitializationPromise: IDictionary<boolean> = {};
13+
private platformPrepareHandlers: IDictionary<boolean> = {};
1314
private promise = Promise.resolve();
1415

1516
constructor(
@@ -49,9 +50,14 @@ export class PreviewAppController extends EventEmitter implements IPreviewAppCon
4950
}
5051

5152
if (this.deviceInitializationPromise[device.id]) {
52-
return this.deviceInitializationPromise[device.id];
53+
// In some cases devices are reported several times during initialization.
54+
// In case we are already preparing the sending of initial files, disregard consecutive requests for initial files
55+
// until we send the files we are currently preparing.
56+
return null;
5357
}
5458

59+
this.deviceInitializationPromise[device.id] = true;
60+
5561
if (device.uniqueId) {
5662
await this.$analyticsService.trackEventActionInGoogleAnalytics({
5763
action: TrackActionNames.PreviewAppData,
@@ -68,20 +74,25 @@ export class PreviewAppController extends EventEmitter implements IPreviewAppCon
6874

6975
await this.$previewAppPluginsService.comparePluginsOnDevice(data, device);
7076

71-
this.$prepareController.on(PREPARE_READY_EVENT_NAME, async currentPrepareData => {
72-
await this.handlePrepareReadyEvent(data, currentPrepareData.hmrData, currentPrepareData.files, device.platform);
73-
});
77+
if (!this.platformPrepareHandlers[device.platform]) {
78+
// TODO: Unset this property once the preview operation for this platform is stopped
79+
this.platformPrepareHandlers[device.platform] = true;
80+
81+
// 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);
84+
});
7485

75-
if (!data.env) { data.env = { }; }
86+
}
87+
88+
data.env = data.env || {};
7689
data.env.externals = this.$previewAppPluginsService.getExternalPlugins(device);
7790

78-
const prepareData = this.$prepareDataService.getPrepareData(data.projectDir, device.platform.toLowerCase(), { ...data, nativePrepare: { skipNativePrepare: true }, watch: true });
91+
const prepareData = this.$prepareDataService.getPrepareData(data.projectDir, device.platform.toLowerCase(), { ...data, nativePrepare: { skipNativePrepare: true }, watch: true });
7992
await this.$prepareController.prepare(prepareData);
8093

81-
this.deviceInitializationPromise[device.id] = this.getInitialFilesForPlatformSafe(data, device.platform);
82-
8394
try {
84-
const payloads = await this.deviceInitializationPromise[device.id];
95+
const payloads = await this.getInitialFilesForPlatformSafe(data, device.platform);
8596
return payloads;
8697
} finally {
8798
this.deviceInitializationPromise[device.id] = null;
@@ -116,7 +127,7 @@ export class PreviewAppController extends EventEmitter implements IPreviewAppCon
116127
if (status === HmrConstants.HMR_ERROR_STATUS) {
117128
const originalUseHotModuleReload = data.useHotModuleReload;
118129
data.useHotModuleReload = false;
119-
await this.syncFilesForPlatformSafe(data, { filesToSync: platformHmrData.fallbackFiles }, platform, previewDevice.id );
130+
await this.syncFilesForPlatformSafe(data, { filesToSync: platformHmrData.fallbackFiles }, platform, previewDevice.id);
120131
data.useHotModuleReload = originalUseHotModuleReload;
121132
}
122133
}));

lib/services/livesync/playground/preview-qr-code-service.ts

-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ export class PreviewQrCodeService implements IPreviewQrCodeService {
4444
const qrCodeUrl = this.$previewSdkService.getQrCodeUrl(options);
4545
const url = await this.getShortenUrl(qrCodeUrl);
4646

47-
this.$logger.info("======== qrCodeUrl ======== ", qrCodeUrl);
48-
4947
this.$logger.info();
5048
const message = `${EOL} Generating qrcode for url ${url}.`;
5149
this.$logger.trace(message);

0 commit comments

Comments
 (0)