Initial commit
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user