hub/reducers: simplify Disconnected state logic

Reduce duplicate code by only checking for current state == Disconnected
once.
This commit is contained in:
David Lechner
2023-04-01 13:38:19 -05:00
committed by David Lechner
parent 69605ada4a
commit 73248fe6aa
2 changed files with 14 additions and 26 deletions
+5 -1
View File
@@ -56,7 +56,11 @@ describe('runtime', () => {
expect(
reducers({ runtime: startingState } as State, bleDisconnectPybricks())
.runtime,
).toBe(HubRuntimeState.Unknown);
).toBe(
startingState === HubRuntimeState.Disconnected
? HubRuntimeState.Disconnected
: HubRuntimeState.Unknown,
);
},
);
+9 -25
View File
@@ -58,6 +58,15 @@ const runtime: Reducer<HubRuntimeState> = (
state = HubRuntimeState.Disconnected,
action,
) => {
// Disconnect overrides all other states. If the hub is disconnected, we
// can't possibly be in any other state until we get a connect event.
if (
state === HubRuntimeState.Disconnected &&
!bleDidConnectPybricks.matches(action)
) {
return state;
}
if (bleDidConnectPybricks.matches(action)) {
return HubRuntimeState.Unknown;
}
@@ -72,27 +81,15 @@ const runtime: Reducer<HubRuntimeState> = (
}
if (didStartDownload.matches(action)) {
// disconnected overrides download
if (state === HubRuntimeState.Disconnected) {
return state;
}
return HubRuntimeState.Loading;
}
if (didFinishDownload.matches(action)) {
// disconnected overrides download
if (state === HubRuntimeState.Disconnected) {
return state;
}
// state is unknown until we receive a status event
return HubRuntimeState.Unknown;
}
if (didFailToFinishDownload.matches(action)) {
// disconnected overrides download
if (state === HubRuntimeState.Disconnected) {
return state;
}
return HubRuntimeState.Idle;
}
@@ -100,7 +97,6 @@ const runtime: Reducer<HubRuntimeState> = (
// The loading state is determined solely by the IDE, so we can't
// let the hub status interfere with it.
if (
state === HubRuntimeState.Disconnected ||
state === HubRuntimeState.Loading ||
state === HubRuntimeState.StartingRepl ||
state === HubRuntimeState.StoppingUserProgram
@@ -120,17 +116,11 @@ const runtime: Reducer<HubRuntimeState> = (
}
if (hubDidStartRepl.matches(action)) {
if (state === HubRuntimeState.Disconnected) {
return state;
}
// state is unknown until we receive a status event
return HubRuntimeState.Unknown;
}
if (hubDidFailToStartRepl.matches(action)) {
if (state === HubRuntimeState.Disconnected) {
return state;
}
// failed to communicate, so state is unknown
return HubRuntimeState.Unknown;
}
@@ -140,17 +130,11 @@ const runtime: Reducer<HubRuntimeState> = (
}
if (hubDidStopUserProgram.matches(action)) {
if (state === HubRuntimeState.Disconnected) {
return state;
}
// state is unknown until we receive a status event
return HubRuntimeState.Unknown;
}
if (hubDidFailToStopUserProgram.matches(action)) {
if (state === HubRuntimeState.Disconnected) {
return state;
}
// failed to communicate, so state is unknown
return HubRuntimeState.Unknown;
}