Skip to content

Commit 8b9b33b

Browse files
authored
Add event details on close (#106)
1 parent be4a85d commit 8b9b33b

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ It is possible to customize the button and the message. You do this by putting y
5252
</improv-wifi-serial-launch-button>
5353
```
5454

55+
### Events
56+
57+
When the dialog is closed, a `closed` event will be fired on both `<improv-wifi-serial-launch-button>` and `<improv-wifi-serial-provision-dialog>`. This event will have a `detail` property with the following properties:
58+
59+
- `improv`: Boolean indicating if we connected to a device running Improv.
60+
- `provisioned`: Boolean indicating if the device is connected to Wi-Fi.
61+
5562
## Browser Support
5663

5764
This SDK requires a browser with support for WebSerial. Currently this is supported by Google Chrome, Microsoft Edge and other browsers based on the Blink engine.

src/provision.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SerialLaunchButton } from "./serial-launch-button.js";
2+
import { fireEvent } from "./util/fire-event.js";
23

34
export const startProvisioning = async (button: SerialLaunchButton) => {
45
import("./serial-provision-dialog.js");
@@ -23,8 +24,9 @@ export const startProvisioning = async (button: SerialLaunchButton) => {
2324
el.port = port;
2425
el.addEventListener(
2526
"closed",
26-
() => {
27-
port!.close();
27+
async (ev: any) => {
28+
await port!.close();
29+
fireEvent(button, "closed" as any, ev.detail);
2830
},
2931
{ once: true }
3032
);

src/serial-provision-dialog.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,18 @@ class SerialProvisionDialog extends LitElement {
437437
}
438438

439439
private async _handleClose() {
440-
await this._client?.close();
441-
this._client = undefined;
442-
fireEvent(this, "closed" as any);
440+
const eventData = {
441+
improv: false,
442+
provisioned: false,
443+
};
444+
if (this._client) {
445+
eventData.improv = true;
446+
eventData.provisioned =
447+
this._client.state === ImprovSerialCurrentState.PROVISIONED;
448+
await this._client?.close();
449+
this._client = undefined;
450+
}
451+
fireEvent(this, "closed" as any, eventData);
443452
this.parentNode!.removeChild(this);
444453
}
445454

0 commit comments

Comments
 (0)