mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-12 09:36:27 +00:00
Add timeout while waiting for checksum
This adds a timeout while waiting for the checksum from the hub when downloading and running a program. Without this, we can get stuck in the "loading" state if the hub doesn't respond as expected. This can happen, for example, if a program is started with the hub button at the same time as the start button in Pybricks Code is pressed. Issue: https://github.com/pybricks/support/issues/301
This commit is contained in:
+28
-2
@@ -4,6 +4,7 @@
|
||||
import {
|
||||
SagaGenerator,
|
||||
actionChannel,
|
||||
delay,
|
||||
getContext,
|
||||
put,
|
||||
race,
|
||||
@@ -108,7 +109,19 @@ function* downloadAndRun(_action: HubDownloadAndRunAction): Generator {
|
||||
return;
|
||||
}
|
||||
|
||||
const checksumAction = yield* take(checksumChannel);
|
||||
const { checksumAction, checksumTimeout } = yield* race({
|
||||
checksumAction: take(checksumChannel),
|
||||
checksumTimeout: delay(1000),
|
||||
});
|
||||
|
||||
if (checksumTimeout) {
|
||||
console.error(`timeout waiting for checksum`);
|
||||
yield* put(didFailToFinishDownload());
|
||||
return;
|
||||
}
|
||||
|
||||
defined(checksumAction);
|
||||
|
||||
if (checksumAction.checksum !== (0xff ^ xor8(sizeBuf))) {
|
||||
console.error(
|
||||
`bad checksum ${checksumAction.checksum} vs ${0xff ^ xor8(sizeBuf)}`,
|
||||
@@ -136,7 +149,20 @@ function* downloadAndRun(_action: HubDownloadAndRunAction): Generator {
|
||||
}
|
||||
// TODO: dispatch progress
|
||||
}
|
||||
const checksumAction = yield* take(checksumChannel);
|
||||
|
||||
const { checksumAction, checksumTimeout } = yield* race({
|
||||
checksumAction: take(checksumChannel),
|
||||
checksumTimeout: delay(1000),
|
||||
});
|
||||
|
||||
if (checksumTimeout) {
|
||||
console.error(`timeout waiting for checksum`);
|
||||
yield* put(didFailToFinishDownload());
|
||||
return;
|
||||
}
|
||||
|
||||
defined(checksumAction);
|
||||
|
||||
if (checksumAction.checksum !== (0xff ^ xor8(chunk))) {
|
||||
console.error(
|
||||
`bad checksum ${checksumAction.checksum} vs ${0xff ^ xor8(chunk)}`,
|
||||
|
||||
Reference in New Issue
Block a user