immich/mobile/lib/utils/async_mutex.dart
Mert 0121043d7d
refactor(mobile): sqlite-based map view (#20665)
* feat(mobile): drift map page

* refactor: map query

* perf: do not filter markers

* fix: refresh timeline by key

* chore: rename

* remove ref listen and global key

* clean code

* remove locked and favorite

* temporary change for stress test

* optimizations

* fix bottom sheet

* cleaner bounds check

* cleanup

* feat: back button

---------

Co-authored-by: wuzihao051119 <wuzihao051119@outlook.com>
Co-authored-by: Alex <alex.tran1502@gmail.com>
2025-08-06 10:05:49 -05:00

22 lines
628 B
Dart

import 'dart:async';
/// Async mutex to guarantee actions are performed sequentially and do not interleave
class AsyncMutex {
Future _running = Future.value(null);
int _enqueued = 0;
int get enqueued => _enqueued;
/// 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>();
_enqueued++;
_running.whenComplete(() {
_enqueued--;
completer.complete(Future<T>.sync(operation));
});
return _running = completer.future;
}
}