39 lines
896 B
JavaScript
39 lines
896 B
JavaScript
import { useReducer } from 'react';
|
|
import { namedLocalStorage } from '../util/PersistentStorage';
|
|
import { nextState } from '../util/StateHelper';
|
|
|
|
const Persistent = (() => {
|
|
const [getEnabled, setEnabled] = namedLocalStorage('polling.enabled', true);
|
|
|
|
return {
|
|
getEnabled,
|
|
setEnabled
|
|
}
|
|
})(); // <<--- Execute
|
|
|
|
export const pollingInitialState = {
|
|
enabled: Persistent.getEnabled() === 'true',
|
|
|
|
games: false,
|
|
leaderboard: false
|
|
};
|
|
|
|
export function pollingReducer(curntState, action) {
|
|
switch (action.type) {
|
|
|
|
case 'toggleOnOff': return {
|
|
...curntState,
|
|
enabled: Persistent.setEnabled(!curntState.enabled)
|
|
};
|
|
|
|
case 'next':
|
|
return nextState(curntState, action);
|
|
|
|
default:
|
|
throw Error('Unknown action.type:' + action.type);
|
|
}
|
|
}
|
|
|
|
export default function usePollingReducer() {
|
|
return useReducer(pollingReducer, pollingInitialState);
|
|
} |