live WEB search
This commit is contained in:
parent
47945dbec7
commit
4ade7f1682
19 changed files with 503 additions and 59 deletions
25
lib/features/core/utils/mutex.dart
Normal file
25
lib/features/core/utils/mutex.dart
Normal file
|
@ -0,0 +1,25 @@
|
|||
import 'dart:async';
|
||||
import 'dart:collection';
|
||||
import 'dart:ui';
|
||||
|
||||
/// A simple Mutex implementation using a [Completer].
|
||||
class Mutex {
|
||||
final _completerQueue = Queue<Completer>();
|
||||
|
||||
/// Runs the given [run] function-block in a thread-safe/blocked zone. A convenient `unlock()`
|
||||
/// is provided, which can be called anywhere to signal re-entry.
|
||||
FutureOr<T> lockAndRun<T>({
|
||||
required FutureOr<T> Function(VoidCallback unlock) run,
|
||||
}) async {
|
||||
final completer = Completer();
|
||||
_completerQueue.add(completer);
|
||||
if (_completerQueue.first != completer) {
|
||||
await _completerQueue.removeFirst().future;
|
||||
}
|
||||
final value = await run(() => completer.complete());
|
||||
return value;
|
||||
}
|
||||
|
||||
Future<void> get lastOperationCompletionAwaiter =>
|
||||
_completerQueue.isNotEmpty ? _completerQueue.last.future : Future.value();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue