acquire method

Future<void> acquire({
  1. Duration poll = const Duration(seconds: 5),
  2. Duration reportEvery = const Duration(minutes: 1),
  3. void onWait(
    1. Duration waited
    )?,
})

Takes the lock, waiting while somebody else holds it.

onWait is called with the time waited so far: once when the wait begins, then every reportEvery, so a person watching the console sees why nothing is happening and whose lock it is.

Implementation

Future<void> acquire({
  Duration poll = const Duration(seconds: 5),
  Duration reportEvery = const Duration(minutes: 1),
  void Function(Duration waited)? onWait,
}) async {
  final watch = Stopwatch()..start();
  var nextReport = Duration.zero;
  while (!_tryAcquire()) {
    if (onWait != null && watch.elapsed >= nextReport) {
      onWait(watch.elapsed);
      nextReport = watch.elapsed + reportEvery;
    }
    await Future<void>.delayed(poll);
  }
}