Adjust debouncers

This commit is contained in:
Hillel Coren 2021-04-29 17:33:04 +03:00
parent a857a7970b
commit 7412fa8f48
1 changed files with 4 additions and 28 deletions

View File

@ -372,9 +372,6 @@ Middleware<AppState> _createUserLoggedIn(
};
}
final _persistDataDebouncer =
Debouncer(milliseconds: kMillisecondsToDebounceSave);
Middleware<AppState> _createPersistData(
List<PersistenceRepository> companyRepositories,
) {
@ -383,40 +380,19 @@ Middleware<AppState> _createPersistData(
next(action);
void saveState() {
final AppState state = store.state;
final index = state.uiState.selectedCompanyIndex;
companyRepositories[index]
.saveCompanyState(state.userCompanyStates[index]);
}
// When a company is deleted the app switches to another company
// so we need to save it immediately before the selection changes
if (action is DeleteCompanySuccess) {
saveState();
} else {
_persistDataDebouncer.run(() {
if (!store.state.isLoaded) {
return;
}
saveState();
});
}
final state = store.state;
final index = state.uiState.selectedCompanyIndex;
companyRepositories[index].saveCompanyState(state.userCompanyStates[index]);
};
}
final _persistUIDebouncer =
Debouncer(milliseconds: kMillisecondsToDebounceSave);
Middleware<AppState> _createPersistUI(PersistenceRepository uiRepository) {
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
final action = dynamicAction as PersistUI;
next(action);
_persistUIDebouncer.run(() {
uiRepository.saveUIState(store.state.uiState);
});
uiRepository.saveUIState(store.state.uiState);
};
}