lock<T> method

  1. @override
Future<T> lock<T>(
  1. Future<T> callback(), {
  2. Duration? timeout,
})
override

timeout is a timeout for acquiring the lock, not for the callback

Implementation

@override
Future<T> lock<T>(Future<T> Function() callback, {Duration? timeout}) async {
  if (Zone.current[this] != null) {
    throw LockError('Recursive lock is not allowed');
  }
  return runZoned(() async {
    if (closed) {
      throw const ClosedException();
    }
    await _acquire(timeout: timeout);
    try {
      final T result = await callback();
      return result;
    } finally {
      _unlock();
    }
  }, zoneValues: {this: true});
}