-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathpreview-sdk-service.ts
102 lines (91 loc) · 3.64 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
import { MessagingService, Config, Device, DeviceConnectedMessage, SdkCallbacks, ConnectedDevices, FilesPayload } from "nativescript-preview-sdk";
import { PubnubKeys } from "./preview-app-constants";
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;
public connectedDevices: Device[] = [];
constructor(private $logger: ILogger,
private $httpClient: Server.IHttpClient,
private $config: IConfiguration) {
}
public getQrCodeUrl(options: IHasUseHotModuleReloadOption): string {
const hmrValue = options.useHotModuleReload ? "1" : "0";
return `nsplay://boot?instanceId=${this.instanceId}&pKey=${PubnubKeys.PUBLISH_KEY}&sKey=${PubnubKeys.SUBSCRIBE_KEY}&template=play-ng&hmr=${hmrValue}`;
}
public async initialize(getInitialFiles: (device: Device) => Promise<FilesPayload>): Promise<void> {
const initConfig = this.getInitConfig(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(getInitialFiles: (device: Device) => Promise<FilesPayload>): Config {
return {
pubnubPublishKey: PubnubKeys.PUBLISH_KEY,
pubnubSubscribeKey: PubnubKeys.SUBSCRIBE_KEY,
msvKey: "cli",
msvEnv: this.$config.PREVIEW_APP_ENVIRONMENT,
callbacks: this.getCallbacks(),
getInitialFiles
};
}
private getCallbacks(): SdkCallbacks {
return {
onLogSdkMessage: (log: string) => {
this.$logger.trace("Received onLogSdkMessage message: ", log);
},
onConnectedDevicesChange: (connectedDevices: ConnectedDevices) => ({ }),
onLogMessage: (log: string, deviceName: string) => {
this.$logger.info(`LOG from device ${deviceName}: ${log}`);
},
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.");
},
onDeviceConnectedMessage: (deviceConnectedMessage: DeviceConnectedMessage) => ({ }),
onDeviceConnected: (device: Device) => {
if (!_.includes(this.connectedDevices, device)) {
this.connectedDevices.push(device);
}
},
onDevicesPresence: (devices: Device[]) => ({ }),
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);