From a081c4a798a3b78409e85482ff9526c7c1f33df5 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Wed, 7 Apr 2021 11:27:40 -0500 Subject: [PATCH] Fix download and run race condition This fixes a race condition where the hub program running status flag is received before the last checksum from the download process. Issue: https://github.com/pybricks/support/issues/290 --- src/hub/reducers.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/hub/reducers.ts b/src/hub/reducers.ts index ea66cbc4..ae8ce5ba 100644 --- a/src/hub/reducers.ts +++ b/src/hub/reducers.ts @@ -62,12 +62,15 @@ const runtime: Reducer = ( return state; } case BlePybricksServiceEventActionType.StatusReport: - if (action.statusFlags & statusToFlag(Status.UserProgramRunning)) { - return HubRuntimeState.Running; - } - // TODO: Status report flags should probably separated from hub runtime state. + // TODO: Status report flags need to be separated from hub runtime state. // For now, we have this hack to ensure status updates don't interfere with - // download and run + // download and run. Loading state should really be actions like didStartLoading, + // didLoad and didFailToLoad. + if (state !== HubRuntimeState.Loading) { + if (action.statusFlags & statusToFlag(Status.UserProgramRunning)) { + return HubRuntimeState.Running; + } + } if (state !== HubRuntimeState.Loading && state !== HubRuntimeState.Loaded) { return HubRuntimeState.Idle; }