21 lines
377 B
Dart
21 lines
377 B
Dart
|
|
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();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|