-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathpreview-sdk-service.ts
106 lines (94 loc) · 4.03 KB
/
preview-sdk-service.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
import { MessagingService, Config, Device, DeviceConnectedMessage, SdkCallbacks, ConnectedDevices, FilesPayload } from "nativescript-preview-sdk";
const pako = require("pako");
export class PreviewSdkService implements IPreviewSdkService {
private static MAX_FILES_UPLOAD_BYTE_LENGTH = 15 * 1024 * 1024; // In MBs
private messagingService: MessagingService = null;
private instanceId: string = null;
constructor(private $config: IConfiguration,
private $httpClient: Server.IHttpClient,
private $logger: ILogger,
private $previewDevicesService: IPreviewDevicesService,
private $previewAppLogProvider: IPreviewAppLogProvider,
private $previewSchemaService: IPreviewSchemaService) {
}
public getQrCodeUrl(options: IGetQrCodeUrlOptions): string {
const { projectDir, useHotModuleReload } = options;
const schema = this.$previewSchemaService.getSchemaData(projectDir);
const hmrValue = useHotModuleReload ? "1" : "0";
const result = `${schema.name}://boot?instanceId=${this.instanceId}&pKey=${schema.publishKey}&sKey=${schema.subscribeKey}&template=play-ng&hmr=${hmrValue}`;
return result;
}
public async initialize(projectDir: string, getInitialFiles: (device: Device) => Promise<FilesPayload>): Promise<void> {
const initConfig = this.getInitConfig(projectDir, getInitialFiles);
this.messagingService = new MessagingService();
this.instanceId = await this.messagingService.initialize(initConfig);
}
public applyChanges(filesPayload: FilesPayload): Promise<void> {
return new Promise((resolve, reject) => {
this.messagingService.applyChanges(this.instanceId, filesPayload, err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
public stop(): void {
this.messagingService.stop();
}
private getInitConfig(projectDir: string, getInitialFiles: (device: Device) => Promise<FilesPayload>): Config {
const schema = this.$previewSchemaService.getSchemaData(projectDir);
return {
pubnubPublishKey: schema.publishKey,
pubnubSubscribeKey: schema.subscribeKey,
msvKey: schema.msvKey,
msvEnv: this.$config.PREVIEW_APP_ENVIRONMENT,
callbacks: this.getCallbacks(),
getInitialFiles,
previewAppStoreId: schema.previewAppStoreId,
previewAppGooglePlayId: schema.previewAppId
};
}
private getCallbacks(): SdkCallbacks {
return {
onLogSdkMessage: (log: string) => {
this.$logger.trace("Received onLogSdkMessage message: ", log);
},
onLogMessage: (log: string, deviceName: string, deviceId: string) => {
this.$previewAppLogProvider.logData(log, deviceName, deviceId);
},
onRestartMessage: () => {
this.$logger.trace("Received onRestartMessage event.");
},
onUncaughtErrorMessage: () => {
this.$logger.warn("The Preview app has terminated unexpectedly. Please run it again to get a detailed crash report.");
},
onConnectedDevicesChange: (connectedDevices: ConnectedDevices) => ({}),
onDeviceConnectedMessage: (deviceConnectedMessage: DeviceConnectedMessage) => ({}),
onDeviceConnected: (device: Device) => ({}),
onDevicesPresence: (devices: Device[]) => this.$previewDevicesService.updateConnectedDevices(devices),
onSendingChange: (sending: boolean) => ({ }),
onBiggerFilesUpload: async (filesContent, callback) => {
const gzippedContent = Buffer.from(pako.gzip(filesContent));
const byteLength = filesContent.length;
if (byteLength > PreviewSdkService.MAX_FILES_UPLOAD_BYTE_LENGTH) {
this.$logger.warn("The files to upload exceed the maximum allowed size of 15MB. Your app might not work as expected.");
}
const playgroundUploadResponse = await this.$httpClient.httpRequest({
url: this.$config.UPLOAD_PLAYGROUND_FILES_ENDPOINT,
method: "POST",
body: gzippedContent,
headers: {
"Content-Encoding": "gzip",
"Content-Type": "text/plain"
}
});
const responseBody = JSON.parse(playgroundUploadResponse.body);
const location = responseBody && responseBody.location;
callback(location, playgroundUploadResponse.error);
}
};
}
}
$injector.register("previewSdkService", PreviewSdkService);