Skip to content

Commit 01503dc

Browse files
authored
Fix missing callback (#4509)
* Add missing callback * Add missing callback for bluetooth * Fix async error handling and timing issues * Fix bytesSent
1 parent 16ae2ac commit 01503dc

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

src/js/protocols/WebBluetooth.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,24 +343,44 @@ class WebBluetooth extends EventTarget {
343343
}
344344
}
345345

346-
async send(data) {
346+
async send(data, cb) {
347347
if (!this.writeCharacteristic) {
348+
if (cb) {
349+
cb({
350+
error: "No write characteristic available",
351+
bytesSent: 0,
352+
});
353+
}
354+
console.error(`${this.logHead} No write characteristic available`);
348355
return;
349356
}
350357

351358
// There is no writable stream in the bluetooth API
352-
this.bytesSent += data.byteLength;
353-
354359
const dataBuffer = new Uint8Array(data);
355360

356361
try {
357362
if (this.lastWrite) {
358363
await this.lastWrite;
359364
}
360-
} catch (error) {
361-
console.error(error);
365+
this.lastWrite = this.writeCharacteristic.writeValue(dataBuffer);
366+
await this.lastWrite;
367+
this.bytesSent += data.byteLength;
368+
369+
if (cb) {
370+
cb({
371+
error: null,
372+
bytesSent: data.byteLength,
373+
});
374+
}
375+
} catch (e) {
376+
console.error(`${this.logHead} Failed to send data:`, e);
377+
if (cb) {
378+
cb({
379+
error: e,
380+
bytesSent: 0,
381+
});
382+
}
362383
}
363-
this.lastWrite = this.writeCharacteristic.writeValue(dataBuffer);
364384

365385
return {
366386
bytesSent: data.byteLength,

src/js/protocols/WebSocket.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,22 @@ class Websocket extends EventTarget {
112112
try {
113113
this.ws.send(data);
114114
this.bytesSent += data.byteLength;
115+
116+
if (cb) {
117+
cb({
118+
error: null,
119+
bytesSent: data.byteLength,
120+
});
121+
}
115122
} catch (e) {
116123
console.error(`${this.logHead}Failed to send data e: ${e}`);
124+
125+
if (cb) {
126+
cb({
127+
error: e,
128+
bytesSent: 0,
129+
});
130+
}
117131
}
118132
}
119133

0 commit comments

Comments
 (0)