synchronized<T> method

Future<T> synchronized<T>(
  1. Future<T> func()
)

Implementation

Future<T> synchronized<T>(
  Future<T> Function() func,
) async {
  final prev = last;
  final completer = Completer<void>.sync();
  last = completer.future;
  try {
    if (prev != null) {
      await prev;
    }

    final result = func();
    return result;
  } finally {
    if (identical(last, completer.future)) {
      last = null;
    }
    completer.complete();
  }
}