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 * as serviceWorker from "./serviceWorker";
import { saveState } from "./localStorage";
import { SettingsState } from "./reducers/settingsReducer";
let currentSettings: SettingsState | undefined = undefined;
store.subscribe(() => {
saveState(store.getState());
const prevSettings = currentSettings;
currentSettings = store.getState().settings;
// Write to local-storage only when settings have changed.
if (prevSettings !== currentSettings) {
saveState(store.getState());
}
});
ReactDOM.render(

View File

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