/// Serializes operations without letting a failed operation poison later work. class SerialQueue { Future? _tail; Future run(Future Function() operation) { final result = (_tail ?? Future.value()).then((_) => operation()); late final Future tail; void release() { // An idle queue must not retain a completed future's scheduler/zone. // A later operation may already have replaced this tail. if (identical(_tail, tail)) _tail = null; } tail = result.then( (_) => release(), onError: (Object _, StackTrace _) => release(), ); _tail = tail; return result; } }