lockAndRun<T> method

FutureOr<T> lockAndRun<T>({
  1. required FutureOr<T> run(
    1. VoidCallback unlock
    ),
})

Locks the mutex, runs the provided function, and releases the lock.

The provided run function receives an unlock callback that must be called when the operation is complete to release the lock.

Parameters:

  • run - Function to execute with the lock

Returns the result of the run function

Implementation

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());
  _completerQueue.remove(completer);
  return value;
}