loading state and error handling refactor
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:easy_debounce/easy_debounce.dart';
|
||||
@@ -9,14 +10,23 @@ import '../../app_modules.dart';
|
||||
|
||||
class SettingsCubit extends HydratedCubit<Settings> {
|
||||
static const _debounceTag = 'settings_persist';
|
||||
bool _emitScheduled = false;
|
||||
|
||||
SettingsCubit() : super(DefaultSettings.get());
|
||||
|
||||
Settings val({bool write = false}) {
|
||||
if (write) {
|
||||
// Notify listeners immediately so the UI reflects the mutation right away;
|
||||
// debounce the actual persistence to disk to avoid hammering on rapid edits.
|
||||
_emitFreshInstance();
|
||||
// Defer the emit until the synchronous mutation on the returned object
|
||||
// has finished. Without this scheduleMicrotask the cubit emits a copy
|
||||
// captured *before* the assignment runs, so listeners (and HydratedBloc
|
||||
// persistence) see the old value on the first emit.
|
||||
if (!_emitScheduled) {
|
||||
_emitScheduled = true;
|
||||
scheduleMicrotask(() {
|
||||
_emitScheduled = false;
|
||||
_emitFreshInstance();
|
||||
});
|
||||
}
|
||||
EasyDebounce.debounce(_debounceTag, const Duration(milliseconds: 500), _emitFreshInstance);
|
||||
}
|
||||
return state;
|
||||
|
||||
Reference in New Issue
Block a user