import 'dart:async';


class AsyncLock {
  Future<void>? _lastOp;

  Future<T> run<T>(Future<T> Function() action) async {
    final previous = _lastOp;
    final completer = Completer<void>();
    _lastOp = completer.future;
    if (previous != null) {
      await previous;
    }
    try {
      return await action();
    } finally {
      completer.complete();
    }
  }
}
