Skip to content

Commit

Permalink
Add BadgeAPI.onConnectionLost
Browse files Browse the repository at this point in the history
  • Loading branch information
Pwuts committed Mar 30, 2023
1 parent 447161a commit 4b5b173
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: Checks

on:
push:
tags-ignore: '*'
paths:
- 'src/**'
- 'tsconfig.json'
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ interface BadgeAPI {
syncConnection()
assertConnected()

hasConnectedBadge
set onConnectionLost
get hasConnectedBadge

transaction(cmd, payload, timeout)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@badge.team/badge-webusb",
"version": "1.0.1",
"version": "1.0.2",
"description": "Web API for the Badge.team MCH2022 badge",
"keywords": [
"USB", "WebUSB",
Expand Down
8 changes: 8 additions & 0 deletions src/badge-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export class BadgeAPI {
async connect() {
this.badge = await BadgeUSB.connect();
this.badge.onConnectionLost = () => delete this.badge;

if (this._onConnectionLost) this._onConnectionLost();
}

async disconnect(reset = false) {
Expand All @@ -38,6 +40,12 @@ export class BadgeAPI {
badge.assertConnected();
}

set onConnectionLost(callback: () => void) {
this._onConnectionLost = callback;
}

private _onConnectionLost?: () => void;

async syncConnection(): Promise<void> {
try {
this.assertConnected(this.badge);
Expand Down

0 comments on commit 4b5b173

Please sign in to comment.