pollingFlux

This commit is contained in:
djmil 2023-11-07 12:58:09 +01:00
parent d92a3df32b
commit 9ec2059c4a
3 changed files with 16 additions and 19 deletions

View File

@ -13,7 +13,7 @@ import useLeaderboardApi from './api/leaderboard';
import useUserApi from './api/user';
function App() {
const pollingFlux = useReducer(Polling.reducer, Polling.restoreState);
const pollingFlux = useReducer(Polling.reducer, Polling.initialState);
const userFlux = useReducer(User.reducer, User.initialState);
const leaderboardApi = useLeaderboardApi(pollingFlux);

View File

@ -12,7 +12,7 @@ export default function useLeaderboardApi([polling, dispatchPolling]) {
const [leaderboard, isFetching] = usePolling(uri, mode);
if (polling.leaderboard !== isFetching) {
dispatchPolling({ type: 'setLeaderboard', value: isFetching });
dispatchPolling({ type: 'next', leaderboard: isFetching });
}
return leaderboard;

View File

@ -1,4 +1,5 @@
import { useLocalStorage } from '../util/PersistentStorage'
import { nextState } from '../util/StateHelper';
const Persistent = (() => {
const [getEnabled, setEnabled] = useLocalStorage('polling.enabled', true);
@ -9,36 +10,32 @@ const Persistent = (() => {
}
})(); // <<--- Execute
const restoreState = {
enabled: Persistent.getEnabled() === 'true',
export const pollingInitialState = {
enabled: Persistent.getEnabled() === 'true',
games: false,
leaderboard: false
games: false,
leaderboard: false
};
function reducer(state, action) {
export function pollingReducer(curntState, action) {
switch (action.type) {
case 'toggleOnOff': return {
...state,
enabled: Persistent.setEnabled(!state.enabled)
...curntState,
enabled: Persistent.setEnabled(!curntState.enabled)
};
case 'setGames': return {
...state,
games: action.value
};
case 'setLeaderboard': return {
...state,
leaderboard: action.value
};
case 'next':
return nextState(curntState, action);
default:
throw Error('Unknown action.type:' + action.type);
}
}
const Polling = { reducer, restoreState }
const Polling = {
reducer: pollingReducer,
initialState: pollingInitialState
};
export default Polling