1 | import type { InstallButton } from "./install-button.js";
|
2 |
|
3 | export const connect = async (button: InstallButton) => {
|
4 | import("./install-dialog.js");
|
5 | let port: SerialPort | undefined;
|
6 | try {
|
7 | port = await navigator.serial.requestPort();
|
8 | } catch (err: any) {
|
9 | if ((err as DOMException).name === "NotFoundError") {
|
10 | import("./no-port-picked/index").then((mod) =>
|
11 | mod.openNoPortPickedDialog(() => connect(button)),
|
12 | );
|
13 | return;
|
14 | }
|
15 | alert(`Error: ${err.message}`);
|
16 | return;
|
17 | }
|
18 |
|
19 | if (!port) {
|
20 | return;
|
21 | }
|
22 |
|
23 | try {
|
24 | await port.open({ baudRate: 115200 });
|
25 | } catch (err: any) {
|
26 | alert(err.message);
|
27 | return;
|
28 | }
|
29 |
|
30 | const el = document.createElement("ewt-install-dialog");
|
31 | el.port = port;
|
32 | el.manifestPath = button.manifest || button.getAttribute("manifest")!;
|
33 | el.overrides = button.overrides;
|
34 | el.addEventListener(
|
35 | "closed",
|
36 | () => {
|
37 | port!.close();
|
38 | },
|
39 | { once: true },
|
40 | );
|
41 | document.body.appendChild(el);
|
42 | };
|