Skip to content

Commit f3f0436

Browse files
committed
CR review
1 parent 188a51b commit f3f0436

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/js/protocols/CapacitorSerial.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ class CapacitorSerialProtocol extends EventTarget {
4747
UsbSerial.addListener("error", this.handleErrorEvent);
4848
}
4949

50+
cleanup() {
51+
UsbSerial.removeAllListeners("data");
52+
UsbSerial.removeAllListeners("connected");
53+
UsbSerial.removeAllListeners("attached");
54+
UsbSerial.removeAllListeners("detached");
55+
UsbSerial.removeAllListeners("error");
56+
}
57+
5058
handleDataEvent(event) {
5159
if (event?.data) {
5260
// Convert hex string from plugin to Uint8Array
@@ -61,10 +69,7 @@ class CapacitorSerialProtocol extends EventTarget {
6169
}
6270

6371
handleAttachedEvent(event) {
64-
const added = this.handleNewDevice(event);
65-
if (added) {
66-
this.dispatchEvent(new CustomEvent("addedDevice", { detail: added }));
67-
}
72+
this.handleNewDevice(event);
6873
}
6974

7075
handleDetachedEvent(event) {
@@ -81,7 +86,7 @@ class CapacitorSerialProtocol extends EventTarget {
8186
const uint8Array = new Uint8Array(length);
8287

8388
for (let i = 0; i < length; i++) {
84-
uint8Array[i] = parseInt(hexString.substr(i * 2, 2), 16);
89+
uint8Array[i] = parseInt(hexString.slice(i * 2, i * 2 + 2), 16);
8590
}
8691

8792
return uint8Array;
@@ -260,6 +265,7 @@ class CapacitorSerialProtocol extends EventTarget {
260265
console.error(`${logHead} Error closing serial connection:`, error);
261266
closeError = error;
262267
} finally {
268+
this.cleanup();
263269
this.isOpen = false;
264270
this.connected = false;
265271
this.port = null;

src/js/utils/checkCompatibility.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export function checkCompatibility() {
9595
errorMessage += "<br/>- Serial API support is disabled.";
9696
}
9797

98-
if (!hasUSBSupport) {
98+
if (!hasUsbSupport) {
9999
errorMessage += "<br/>- Web USB API support is disabled.";
100100
}
101101

0 commit comments

Comments
 (0)