-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathpreview-app-controller.ts
243 lines (211 loc) · 10.6 KB
/
preview-app-controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { TrackActionNames, PREPARE_READY_EVENT_NAME } from "../constants";
import { PrepareController } from "./prepare-controller";
import { Device, FilesPayload } from "nativescript-preview-sdk";
import { performanceLog } from "../common/decorators";
import { stringify, deferPromise } from "../common/helpers";
import { HmrConstants } from "../common/constants";
import { EventEmitter } from "events";
import { PrepareDataService } from "../services/prepare-data-service";
import { PreviewAppLiveSyncEvents } from "../services/livesync/playground/preview-app-constants";
export class PreviewAppController extends EventEmitter implements IPreviewAppController {
private prepareReadyEventHandler: any = null;
private deviceInitializationPromise: IDictionary<boolean> = {};
private devicesLiveSyncChain: IDictionary<Promise<void>> = {};
private devicesCanExecuteHmr: IDictionary<boolean> = {};
// holds HMR files per device in order to execute batch upload on fast updates
private devicesHmrFiles: IDictionary<string[]> = {};
// holds app files per device in order to execute batch upload on fast updates on failed HMR or --no-hmr
private devicesAppFiles: IDictionary<string[]> = {};
// holds the current HMR hash per device in order to watch the proper hash status on fast updates
private devicesCurrentHmrHash: IDictionary<string> = {};
constructor(
private $analyticsService: IAnalyticsService,
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
private $errors: IErrors,
private $hmrStatusService: IHmrStatusService,
private $logger: ILogger,
public $hooksService: IHooksService,
private $mobileHelper: Mobile.IMobileHelper,
private $pluginsService: IPluginsService,
private $prepareController: PrepareController,
private $previewAppFilesService: IPreviewAppFilesService,
private $previewAppPluginsService: IPreviewAppPluginsService,
private $previewDevicesService: IPreviewDevicesService,
private $previewQrCodeService: IPreviewQrCodeService,
private $previewSdkService: IPreviewSdkService,
private $prepareDataService: PrepareDataService,
private $projectDataService: IProjectDataService,
private $markingModeService: IMarkingModeService
) { super(); }
public async startPreview(data: IPreviewAppLiveSyncData): Promise<IQrCodeImageData> {
await this.previewCore(data);
const url = this.$previewSdkService.getQrCodeUrl({ projectDir: data.projectDir, useHotModuleReload: data.useHotModuleReload });
const result = await this.$previewQrCodeService.getLiveSyncQrCode(url);
return result;
}
public async stopPreview(data: IProjectDir): Promise<void> {
this.$previewSdkService.stop();
this.$previewDevicesService.updateConnectedDevices([]);
await this.$prepareController.stopWatchers(data.projectDir, this.$devicePlatformsConstants.Android);
await this.$prepareController.stopWatchers(data.projectDir, this.$devicePlatformsConstants.iOS);
if (this.prepareReadyEventHandler) {
this.$prepareController.removeListener(PREPARE_READY_EVENT_NAME, this.prepareReadyEventHandler);
this.prepareReadyEventHandler = null;
}
}
private async previewCore(data: IPreviewAppLiveSyncData): Promise<void> {
const projectData = this.$projectDataService.getProjectData(data.projectDir);
await this.$pluginsService.ensureAllDependenciesAreInstalled(projectData);
await this.$previewSdkService.initialize(data.projectDir, async (device: Device) => {
if (this.$mobileHelper.isAndroidPlatform(device.platform)) {
await this.$markingModeService.handleMarkingModeFullDeprecation({ projectDir: projectData.projectDir });
}
try {
if (!device) {
this.$errors.fail("Sending initial preview files without a specified device is not supported.");
}
if (this.deviceInitializationPromise[device.id]) {
// In some cases devices are reported several times during initialization.
// In case we are already preparing the sending of initial files, disregard consecutive requests for initial files
// until we send the files we are currently preparing.
return null;
}
this.deviceInitializationPromise[device.id] = true;
if (device.uniqueId) {
await this.$analyticsService.trackEventActionInGoogleAnalytics({
action: TrackActionNames.PreviewAppData,
platform: device.platform,
additionalData: device.uniqueId
});
}
await this.$hooksService.executeBeforeHooks("preview-sync", { hookArgs: { ...data, platform: device.platform, projectData } });
if (data.useHotModuleReload) {
this.$hmrStatusService.attachToHmrStatusEvent();
this.devicesCanExecuteHmr[device.id] = true;
}
await this.$previewAppPluginsService.comparePluginsOnDevice(data, device);
if (!this.prepareReadyEventHandler) {
const handler = async (currentPrepareData: IFilesChangeEventData) => {
await this.handlePrepareReadyEvent(data, currentPrepareData);
};
this.prepareReadyEventHandler = handler.bind(this);
this.$prepareController.on(PREPARE_READY_EVENT_NAME, this.prepareReadyEventHandler);
}
data.env = data.env || {};
data.env.externals = this.$previewAppPluginsService.getExternalPlugins(device);
const prepareData = this.$prepareDataService.getPrepareData(data.projectDir, device.platform.toLowerCase(), { ...data, nativePrepare: { skipNativePrepare: true }, watch: true, watchNative: false });
await this.$prepareController.prepare(prepareData);
try {
const payloads = await this.getInitialFilesForDeviceSafe(data, device);
return payloads;
} finally {
this.deviceInitializationPromise[device.id] = null;
}
} catch (error) {
this.$logger.trace(`Error while sending files on device '${device && device.id}'. Error is`, error);
this.emit(PreviewAppLiveSyncEvents.PREVIEW_APP_LIVE_SYNC_ERROR, {
error,
data,
platform: device.platform,
deviceId: device.id
});
}
});
return null;
}
@performanceLog()
private async handlePrepareReadyEvent(data: IPreviewAppLiveSyncData, currentPrepareData: IFilesChangeEventData) {
const { hmrData, files, platform } = currentPrepareData;
const platformHmrData = _.cloneDeep(hmrData);
const connectedDevices = this.$previewDevicesService.getDevicesForPlatform(platform);
if (!connectedDevices || !connectedDevices.length) {
this.$logger.warn(`Unable to find any connected devices for platform '${platform}'. In order to execute live sync, open your Preview app and optionally re-scan the QR code using the Playground app.`);
return;
}
await Promise.all(_.map(connectedDevices, async (device) => {
const previousSync = this.devicesLiveSyncChain[device.id] || Promise.resolve();
const currentSyncDeferPromise = deferPromise<void>();
this.devicesLiveSyncChain[device.id] = currentSyncDeferPromise.promise;
this.devicesCurrentHmrHash[device.id] = this.devicesCurrentHmrHash[device.id] || platformHmrData.hash;
if (data.useHotModuleReload) {
this.devicesHmrFiles[device.id] = this.devicesHmrFiles[device.id] || [];
this.devicesHmrFiles[device.id].push(...files);
this.devicesAppFiles[device.id] = platformHmrData.fallbackFiles;
} else {
this.devicesHmrFiles[device.id] = [];
this.devicesAppFiles[device.id] = files;
}
await previousSync;
try {
let canExecuteHmrSync = false;
const hmrHash = this.devicesCurrentHmrHash[device.id];
this.devicesCurrentHmrHash[device.id] = null;
if (data.useHotModuleReload && hmrHash) {
if (this.devicesCanExecuteHmr[device.id]) {
canExecuteHmrSync = true;
}
}
const filesToSync = canExecuteHmrSync ? this.devicesHmrFiles[device.id] : this.devicesAppFiles[device.id];
if (!filesToSync || !filesToSync.length) {
this.$logger.info(`Skipping files sync for device ${this.getDeviceDisplayName(device)}. The changes are already batch transferred in a previous sync.`);
currentSyncDeferPromise.resolve();
return;
}
this.devicesHmrFiles[device.id] = [];
this.devicesAppFiles[device.id] = [];
if (canExecuteHmrSync) {
this.$hmrStatusService.watchHmrStatus(device.id, hmrHash);
await this.syncFilesForPlatformSafe(data, { filesToSync }, platform, device);
const status = await this.$hmrStatusService.getHmrStatus(device.id, hmrHash);
if (!status) {
this.devicesCanExecuteHmr[device.id] = false;
this.$logger.warn(`Unable to get LiveSync status from the Preview app for device ${this.getDeviceDisplayName(device)}. Ensure the app is running in order to sync changes.`);
} else {
this.devicesCanExecuteHmr[device.id] = status === HmrConstants.HMR_SUCCESS_STATUS;
}
} else {
const noHmrData = _.assign({}, data, { useHotModuleReload: false });
await this.syncFilesForPlatformSafe(noHmrData, { filesToSync }, platform, device);
this.devicesCanExecuteHmr[device.id] = true;
}
currentSyncDeferPromise.resolve();
} catch (e) {
currentSyncDeferPromise.resolve();
}
}));
}
private getDeviceDisplayName(device: Device) {
return `${device.name} (${device.id})`.cyan;
}
private async getInitialFilesForDeviceSafe(data: IPreviewAppLiveSyncData, device: Device): Promise<FilesPayload> {
const platform = device.platform;
this.$logger.info(`Start sending initial files for device ${this.getDeviceDisplayName(device)}.`);
try {
const payloads = this.$previewAppFilesService.getInitialFilesPayload(data, platform);
this.$logger.info(`Successfully sent initial files for device ${this.getDeviceDisplayName(device)}.`);
return payloads;
} catch (err) {
this.$logger.warn(`Unable to apply changes for device ${this.getDeviceDisplayName(device)}. Error is: ${err}, ${stringify(err)}`);
}
}
private async syncFilesForPlatformSafe(data: IPreviewAppLiveSyncData, filesData: IPreviewAppFilesData, platform: string, device: Device): Promise<void> {
const deviceId = device && device.id || "";
try {
const payloads = this.$previewAppFilesService.getFilesPayload(data, filesData, platform);
payloads.deviceId = deviceId;
if (payloads && payloads.files && payloads.files.length) {
this.$logger.info(`Start syncing changes for device ${this.getDeviceDisplayName(device)}.`);
await this.$previewSdkService.applyChanges(payloads);
this.$logger.info(`Successfully synced '${payloads.files.map(filePayload => filePayload.file.yellow)}' for device ${this.getDeviceDisplayName(device)}.`);
}
} catch (error) {
this.$logger.warn(`Unable to apply changes for device ${this.getDeviceDisplayName(device)}. Error is: ${error}, ${JSON.stringify(error, null, 2)}.`);
this.emit(PreviewAppLiveSyncEvents.PREVIEW_APP_LIVE_SYNC_ERROR, {
error,
data,
deviceId
});
}
}
}
$injector.register("previewAppController", PreviewAppController);