mirror of
https://github.com/immich-app/immich.git
synced 2024-12-17 12:22:31 +02:00
17 lines
537 B
Dart
17 lines
537 B
Dart
|
import 'dart:async';
|
||
|
|
||
|
/// Async mutex to guarantee actions are performed sequentially and do not interleave
|
||
|
class AsyncMutex {
|
||
|
Future _running = Future.value(null);
|
||
|
|
||
|
/// 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>();
|
||
|
_running.whenComplete(() {
|
||
|
completer.complete(Future<T>.sync(operation));
|
||
|
});
|
||
|
return _running = completer.future;
|
||
|
}
|
||
|
}
|