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

@ -6,3 +6,4 @@ export 'src/async_value.dart';
export 'src/serial_future.dart';
export 'src/single_future.dart';
export 'src/single_state_processor.dart';
export 'src/single_stateless_processor.dart';

View file

@ -14,8 +14,7 @@ import '../async_tools.dart';
class SingleStateProcessor<State> {
SingleStateProcessor();
void updateState(State newInputState,
{required Future<void> Function(State) closure}) {
void updateState(State newInputState, Future<void> Function(State) closure) {
// Use a singlefuture here to ensure we get dont lose any updates
// If the input stream gives us an update while we are
// still processing the last update, the most recent input state will

View file

@ -0,0 +1,47 @@
import 'dart:async';
import '../async_tools.dart';
// Process a single stateless update at a time ensuring each request
// gets processed asynchronously, and continuously while update is requested.
//
// This is useful for processing updates asynchronously without waiting
// from a synchronous execution context
class SingleStatelessProcessor {
SingleStatelessProcessor();
void update(Future<void> Function() closure) {
singleFuture(this, () async {
do {
_more = false;
await closure();
// See if another update was requested
} while (_more);
}, onBusy: () {
// Keep this state until we process again
_more = true;
});
}
// Like update, but with a busy wrapper that clears once the updating is finished
void busyUpdate<T, S>(
Future<void> Function(Future<void> Function(void Function(S))) busy,
Future<void> Function(void Function(S)) closure) {
singleFuture(
this,
() async => busy((emit) async {
do {
_more = false;
await closure(emit);
// See if another update was requested
} while (_more);
}), onBusy: () {
// Keep this state until we process again
_more = true;
});
}
bool _more = false;
}