import 'dart:async'; /// Async mutex to guarantee actions are performed sequentially and do not interleave class AsyncMutex { Future _running = Future.value(null); /// Execute [operation] exclusively, after any currently running operations. /// Returns a [Future] with the result of the [operation]. Future run(Future Function() operation) { final completer = Completer(); _running.whenComplete(() { completer.complete(Future.sync(operation)); }); return _running = completer.future; } }