mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-15 02:54:07 +00:00
The pattern we were using for actions required actions to be defined in three places, an enum member containing the type string, an type definition and a function. This combines all three of these into one by using a helper function based on the ideas from [1]. [1]: https://phryneas.de/redux-typescript-no-discriminating-union
32 lines
748 B
TypeScript
32 lines
748 B
TypeScript
// SPDX-License-Identifier: MIT
|
|
// Copyright (c) 2021-2022 The Pybricks Authors
|
|
|
|
import { Reducer, combineReducers } from 'redux';
|
|
import { didFailToFinish, didFinish, didProgress, didStart } from './actions';
|
|
|
|
const flashing: Reducer<boolean> = (state = false, action) => {
|
|
if (didStart.matches(action)) {
|
|
return true;
|
|
}
|
|
|
|
if (didFinish.matches(action) || didFailToFinish.matches(action)) {
|
|
return false;
|
|
}
|
|
|
|
return state;
|
|
};
|
|
|
|
const progress: Reducer<number | null> = (state = null, action) => {
|
|
if (didStart.matches(action)) {
|
|
return null;
|
|
}
|
|
|
|
if (didProgress.matches(action)) {
|
|
return action.value;
|
|
}
|
|
|
|
return state;
|
|
};
|
|
|
|
export default combineReducers({ flashing, progress });
|