mirror of
https://github.com/CharaChorder/DeviceManager.git
synced 2026-02-06 09:12:41 +00:00
feat: wait ready
This commit is contained in:
@@ -538,6 +538,7 @@ export class CharaDevice {
|
||||
async updateFirmware(
|
||||
file: ArrayBuffer,
|
||||
progress: (transferred: number, total: number) => void,
|
||||
throttle = false,
|
||||
): Promise<void> {
|
||||
while (this.lock) {
|
||||
await this.lock;
|
||||
@@ -584,16 +585,14 @@ export class CharaDevice {
|
||||
});
|
||||
|
||||
const chunkSize = 1024;
|
||||
const promises = [];
|
||||
for (let i = 0; i < file.byteLength; i += chunkSize) {
|
||||
const chunk = file.slice(i, i + chunkSize);
|
||||
promises.push(
|
||||
writer
|
||||
.write(new Uint8Array(chunk))
|
||||
.then(() => progress(i + chunk.byteLength, file.byteLength)),
|
||||
);
|
||||
if (throttle) {
|
||||
await writer.ready;
|
||||
}
|
||||
await writer.write(new Uint8Array(chunk));
|
||||
progress(i + chunk.byteLength, file.byteLength);
|
||||
}
|
||||
await Promise.all(promises);
|
||||
|
||||
serialLog.update((it) => {
|
||||
it.push({
|
||||
|
||||
@@ -56,9 +56,13 @@
|
||||
retries = 2;
|
||||
while (retries-- > 0 && !success) {
|
||||
try {
|
||||
await port.updateFirmware(file, (transferred, total) => {
|
||||
progress = transferred / total;
|
||||
});
|
||||
await port.updateFirmware(
|
||||
file,
|
||||
(transferred, total) => {
|
||||
progress = transferred / total;
|
||||
},
|
||||
retries === 0,
|
||||
);
|
||||
|
||||
success = true;
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user