feat: wait ready

This commit is contained in:
2026-01-30 17:38:46 +01:00
parent afa0d9ffd7
commit f618ffbada
2 changed files with 13 additions and 10 deletions

View File

@@ -538,6 +538,7 @@ export class CharaDevice {
async updateFirmware( async updateFirmware(
file: ArrayBuffer, file: ArrayBuffer,
progress: (transferred: number, total: number) => void, progress: (transferred: number, total: number) => void,
throttle = false,
): Promise<void> { ): Promise<void> {
while (this.lock) { while (this.lock) {
await this.lock; await this.lock;
@@ -584,16 +585,14 @@ export class CharaDevice {
}); });
const chunkSize = 1024; const chunkSize = 1024;
const promises = [];
for (let i = 0; i < file.byteLength; i += chunkSize) { for (let i = 0; i < file.byteLength; i += chunkSize) {
const chunk = file.slice(i, i + chunkSize); const chunk = file.slice(i, i + chunkSize);
promises.push( if (throttle) {
writer await writer.ready;
.write(new Uint8Array(chunk)) }
.then(() => progress(i + chunk.byteLength, file.byteLength)), await writer.write(new Uint8Array(chunk));
); progress(i + chunk.byteLength, file.byteLength);
} }
await Promise.all(promises);
serialLog.update((it) => { serialLog.update((it) => {
it.push({ it.push({

View File

@@ -56,9 +56,13 @@
retries = 2; retries = 2;
while (retries-- > 0 && !success) { while (retries-- > 0 && !success) {
try { try {
await port.updateFirmware(file, (transferred, total) => { await port.updateFirmware(
progress = transferred / total; file,
}); (transferred, total) => {
progress = transferred / total;
},
retries === 0,
);
success = true; success = true;
} catch (e) { } catch (e) {