busy handling

This commit is contained in:
Christien Rioux 2024-02-27 12:45:58 -05:00
parent 43b01c7555
commit c6f017b0d1
23 changed files with 307 additions and 179 deletions

View file

@ -10,7 +10,7 @@ class AsyncTransformerCubit<T, S> extends Cubit<AsyncValue<T>> {
_subscription = input.stream.listen(_asyncTransform);
}
void _asyncTransform(AsyncValue<S> newInputState) {
_singleStateProcessor.updateState(newInputState, closure: (newState) async {
_singleStateProcessor.updateState(newInputState, (newState) async {
// Emit the transformed state
try {
if (newState is AsyncLoading<S>) {

View file

@ -17,7 +17,7 @@ class BlocBusyState<S> extends Equatable {
}
mixin BlocBusyWrapper<S> on BlocBase<BlocBusyState<S>> {
Future<T> busy<T>(Future<T> Function(void Function(S) emit) closure) async =>
Future<T> busyValue<T>(Future<T> Function(void Function(S) emit) closure) =>
_mutex.protect(() async {
void busyemit(S state) {
changedState = state;
@ -41,6 +41,27 @@ mixin BlocBusyWrapper<S> on BlocBase<BlocBusyState<S>> {
return out;
});
Future<void> busy(Future<void> Function(void Function(S) emit) closure) =>
_mutex.protect(() async {
void busyemit(S state) {
changedState = state;
}
// Turn on busy state
emit(BlocBusyState._busy(state.state));
// Run the closure
await closure(busyemit);
// If the closure did one or more 'busy emits' then
// take the most recent one and emit it for real
final finalState = changedState;
if (finalState != null && finalState != state.state) {
emit(BlocBusyState._busy(finalState));
} else {
emit(BlocBusyState._busy(state.state));
}
});
void changeState(S state) {
if (_mutex.isLocked) {
changedState = state;
@ -49,6 +70,8 @@ mixin BlocBusyWrapper<S> on BlocBase<BlocBusyState<S>> {
}
}
bool get isBusy => _mutex.isLocked;
final Mutex _mutex = Mutex();
S? changedState;
}

View file

@ -31,7 +31,7 @@ abstract mixin class StateFollower<S extends Object, K, V> {
void _updateFollow(S newInputState) {
_singleStateProcessor.updateState(getStateMap(newInputState),
closure: (newStateMap) async {
(newStateMap) async {
for (final k in _lastInputStateMap.keys) {
if (!newStateMap.containsKey(k)) {
// deleted