forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelectron.ts
More file actions
135 lines (119 loc) · 5.24 KB
/
electron.ts
File metadata and controls
135 lines (119 loc) · 5.24 KB
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
import Cloud = pxt.Cloud;
import * as cmds from "./cmds";
import * as core from "./core";
import { ProjectView } from "./srceditor";
const pxtElectron: pxt.electron.PxtElectron = (window as any).pxtElectron;
const downloadingUpdateLoadingName = "pxtelectron-downloadingupdate";
export function initElectron(projectView: ProjectView): void {
if (!pxt.BrowserUtils.isPxtElectron()) {
return;
}
pxtElectron.onTelemetry((ev: pxt.electron.TelemetryEvent) => {
pxt.tickEvent(ev.event, ev.data);
});
pxtElectron.onUpdateInstalled(() => {
core.infoNotification(lf("An update will take effect after the app restarts"))
});
pxtElectron.onDriveDeployResult((isSuccess) => {
if (!deployingDeferred) {
pxt.tickEvent("electron.drivedeploy.unknowndeployoperation");
return;
}
if (isSuccess) {
pxt.tickEvent("electron.drivedeploy.success");
deployingDeferred.resolve();
} else {
pxt.tickEvent("electron.drivedeploy.failure");
const err = new Error("electron drive deploy failed");
deployingDeferred.reject(err);
}
});
const criticalUpdateFailedPromise = new Promise((resolve) => {
pxtElectron.onCriticalUpdateFailed(() => {
pxt.tickEvent("electron.criticalupdate.failed");
resolve();
});
});
// Asynchronously check what the update status is, which will let us know if the current version is banned
pxtElectron.onUpdateStatus((status) => {
pxt.debug(`Electron app update status: ${status}`);
pxt.tickEvent(`electron.updatestatus.${status}`);
if (status === pxt.electron.UpdateStatus.UpdatingCritical || status === pxt.electron.UpdateStatus.BannedWithoutUpdate) {
projectView.stopSimulator();
}
switch (status) {
case pxt.electron.UpdateStatus.Ok:
// No update available; nothing to do
return;
case pxt.electron.UpdateStatus.UpdatingCritical:
// App is installing a critical update; show a dialog asking the user to wait
core.confirmAsync({
header: lf("Critical update required"),
body: lf("A critical update is installing. Please do not quit the app. It will automatically restart when the update has completed."),
hideAgree: true,
disagreeLbl: lf("Ok"),
disagreeClass: "green",
size: "medium"
}).then(() => {
core.showLoading("pxt-electron-update", lf("Installing update..."));
});
criticalUpdateFailedPromise
.then(() => {
core.hideLoading("pxt-electron-update");
core.hideDialog();
core.confirmAsync({
header: lf("Critical update failed"),
body: lf("There was an error installing the critical update. Please ensure you are connected to the Internet and try again later."),
hideAgree: true,
disagreeLbl: lf("Quit"),
disagreeClass: "red",
size: "medium"
}).then(b => {
pxtElectron.sendQuit();
});
});
// Don't do anything; app will quit and restart once the update is ready
break;
case pxt.electron.UpdateStatus.BannedWithoutUpdate:
// Current version is banned and there are no updates available; show a dialog explaining the
// situation and quit
core.confirmAsync({
header: lf("Critical update required"),
body: lf("We have disabled this app for security reasons. Please ensure you are connected to the Internet and try again later. An update will be automatically installed as soon as it is available."),
hideAgree: true,
disagreeLbl: lf("Quit"),
disagreeClass: "red",
size: "medium"
}).then(b => {
pxtElectron.sendQuit();
});
default:
// Unknown status; no-op
return;
}
});
pxtElectron.sendUpdateStatusCheck();
}
let deployingDeferred: Promise.Resolver<void> = null;
export function driveDeployAsync(compileResult: pxtc.CompileResult): Promise<void> {
if (!pxt.BrowserUtils.isPxtElectron()) {
return cmds.browserDownloadDeployCoreAsync(compileResult);
}
if (!deployingDeferred) {
deployingDeferred = Promise.defer<void>();
pxtElectron.sendDriveDeploy(compileResult);
}
return deployingDeferred.promise
.catch((e) => {
pxt.tickEvent("electron.drivedeploy.browserdownloadinstead");
return cmds.browserDownloadDeployCoreAsync(compileResult);
})
.finally(() => {
deployingDeferred = null;
});
}
export function openDevTools(): void {
if (pxtElectron) {
pxtElectron.sendOpenDevTools();
}
}