synchronized<T> static method

Future<T> synchronized<T>(
  1. String key,
  2. Future<T> action()
)

Implementation

static Future<T> synchronized<T>(String key, Future<T> Function() action) async {
  final previous = _locks[key] ?? Future.value();
  final completer = Completer<void>();
  _locks[key] = completer.future;
  try {
    await previous;
    return await action();
  } finally {
    completer.complete();
    if (identical(_locks[key], completer.future)) {
      _locks.remove(key);
    }
  }
}