2023-03-04 00:38:30 +02:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
/// Async mutex to guarantee actions are performed sequentially and do not interleave
|
|
|
|
class AsyncMutex {
|
|
|
|
Future _running = Future.value(null);
|
2023-03-27 04:35:52 +02:00
|
|
|
int _enqueued = 0;
|
|
|
|
|
|
|
|
get enqueued => _enqueued;
|
2023-03-04 00:38:30 +02:00
|
|
|
|
|
|
|
/// Execute [operation] exclusively, after any currently running operations.
|
|
|
|
/// Returns a [Future] with the result of the [operation].
|
|
|
|
Future<T> run<T>(Future<T> Function() operation) {
|
|
|
|
final completer = Completer<T>();
|
2023-03-27 04:35:52 +02:00
|
|
|
_enqueued++;
|
2023-03-04 00:38:30 +02:00
|
|
|
_running.whenComplete(() {
|
2023-03-27 04:35:52 +02:00
|
|
|
_enqueued--;
|
2023-03-04 00:38:30 +02:00
|
|
|
completer.complete(Future<T>.sync(operation));
|
|
|
|
});
|
|
|
|
return _running = completer.future;
|
|
|
|
}
|
|
|
|
}
|