Minimize the number of writes to local-storage

This commit is contained in:
Ken Hibino 2021-01-22 22:16:48 -08:00
parent 3c064e0eaf
commit 3547d27214
2 changed files with 11 additions and 2 deletions

View File

@ -6,9 +6,17 @@ import App from "./App";
import store from "./store"; import store from "./store";
import * as serviceWorker from "./serviceWorker"; import * as serviceWorker from "./serviceWorker";
import { saveState } from "./localStorage"; import { saveState } from "./localStorage";
import { SettingsState } from "./reducers/settingsReducer";
let currentSettings: SettingsState | undefined = undefined;
store.subscribe(() => { store.subscribe(() => {
const prevSettings = currentSettings;
currentSettings = store.getState().settings;
// Write to local-storage only when settings have changed.
if (prevSettings !== currentSettings) {
saveState(store.getState()); saveState(store.getState());
}
}); });
ReactDOM.render( ReactDOM.render(

View File

@ -9,7 +9,8 @@ export enum ThemePreference {
Always, Always,
Never, Never,
} }
interface SettingsState {
export interface SettingsState {
pollInterval: number; pollInterval: number;
themePreference: ThemePreference; themePreference: ThemePreference;
} }