Skip to content

Commit 1dcab43

Browse files
committed
Fix ui desync issue. Fix disconnection issue.
1 parent ce40b4c commit 1dcab43

File tree

2 files changed

+10
-26
lines changed

2 files changed

+10
-26
lines changed

examples/device/webusb_serial/website/application.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,11 @@
331331
uiStatusSpan.className = 'status status-' + level;
332332
}
333333

334-
updateUIConnectionState() {
335-
if (this.currentPort && this.currentPort.isConnected) {
334+
/// force_connected is used to instantly change the UI to the connected state while the device is still connecting
335+
/// Otherwise we would have to wait for the connection to be established.
336+
/// This can take until the device sends the first data packet.
337+
updateUIConnectionState(force_connected = false) {
338+
if (force_connected || (this.currentPort && this.currentPort.isConnected)) {
336339
uiConnectWebUsbSerialBtn.style.display = 'none';
337340
uiConnectSerialBtn.style.display = 'none';
338341
uiDisconnectBtn.style.display = 'block';
@@ -397,6 +400,7 @@
397400
try {
398401
this.setStatus('Requesting device...', 'info');
399402
this.currentPort = await serial.requestSerialPort();
403+
this.updateUIConnectionState(true);
400404
this.currentPort.onReceiveError = error => this.onReceiveError(error);
401405
this.currentPort.onReceive = dataView => this.onReceive(dataView);
402406
await this.currentPort.connect();
@@ -449,6 +453,7 @@
449453
this.currentPort.onReceive = dataView => this.onReceive(dataView);
450454

451455
try {
456+
this.updateUIConnectionState(true);
452457
await this.currentPort.connect();
453458

454459
// save the port to localStorage

examples/device/webusb_serial/website/serial.js

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,9 @@ class SerialPort {
4242
}
4343
}
4444

45-
async _waitForReadLoopToFinish() {
46-
if (this.readLoop) {
47-
try {
48-
await this.readLoop;
49-
} catch (error) {}
50-
this.readLoop = null;
51-
}
52-
}
53-
5445
/// Stop reading and release port
5546
async disconnect() {
5647
this.isConnected = false;
57-
await this._waitForReadLoopToFinish();
5848

5949
if (this.reader) {
6050
try {
@@ -66,7 +56,8 @@ class SerialPort {
6656
if (this.writer) {
6757
try {
6858
await this.writer.close();
69-
} catch (error) {}
59+
} catch (error) { }
60+
this.writer.releaseLock();
7061
}
7162

7263
if (this.readableStreamClosed) {
@@ -160,15 +151,6 @@ class WebUsbSerialPort {
160151
this.readLoop = this._readLoop();
161152
}
162153

163-
async _waitForReadLoopToFinish() {
164-
if (this.readLoop) {
165-
try {
166-
await this.readLoop;
167-
} catch (error) {}
168-
this.readLoop = null;
169-
}
170-
}
171-
172154
/// Internal continuous read loop
173155
async _readLoop() {
174156
while (this.isConnected) {
@@ -189,7 +171,6 @@ class WebUsbSerialPort {
189171
/// Stop reading and release device
190172
async disconnect() {
191173
this.isConnected = false;
192-
await this._waitForReadLoopToFinish();
193174
if (!this.device.opened) return;
194175
try {
195176
await this.device.controlTransferOut({
@@ -199,9 +180,7 @@ class WebUsbSerialPort {
199180
value: 0x00,
200181
index: this.interfaceNumber,
201182
});
202-
} catch (error) {
203-
console.log(error);
204-
}
183+
} catch (error) {}
205184
await this.device.close();
206185
}
207186

0 commit comments

Comments
 (0)