mirror of
https://github.com/CharaChorder/DeviceManager.git
synced 2026-02-09 10:42:42 +00:00
feat: wait ready
This commit is contained in:
@@ -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({
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user