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'; import useUserApi from './api/user';
function App() { function App() {
const pollingFlux = useReducer(Polling.reducer, Polling.restoreState); const pollingFlux = useReducer(Polling.reducer, Polling.initialState);
const userFlux = useReducer(User.reducer, User.initialState); const userFlux = useReducer(User.reducer, User.initialState);
const leaderboardApi = useLeaderboardApi(pollingFlux); const leaderboardApi = useLeaderboardApi(pollingFlux);

View File

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

View File

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