protect method

Future<void> protect(
  1. T tag,
  2. Future<void> callback(), {
  3. FutureOr<bool> repeat()?,
  4. List<Object?> values = const [],
})

Executes the provided callback, locking its execution with the provided tag.

Does nothing, if tag is already locked, meaning being executed by another callback.

If repeat is provided, then the callback will be invoked in loop, while repeat returns true.

If values are provided, then lockedWith shall return true only if values contain the specified there value.

Implementation

Future<void> protect(
  T tag,
  Future<void> Function() callback, {
  FutureOr<bool> Function()? repeat,
  List<Object?> values = const [],
}) async {
  _PoolMutex? mutex = _mutexes[tag];
  if (mutex == null) {
    mutex = _PoolMutex();
    _mutexes[tag] = mutex;
  }

  if (!mutex.isLocked) {
    mutex.values = values;
    do {
      await mutex.protect(callback);
    } while (!_disposed && await repeat?.call() == true);
  }
}