import 'dart:async'; /// The machines' terminal lists being read, so the surface can wait until /// every one has arrived (to start a terminal only when there is none). class TerminalListing { int _pending = 0; final List> _waiters = []; /// Runs [read], counted as pending until it ends. Future track(Future Function() read) async { _pending++; try { await read(); } finally { _pending--; if (_pending == 0) { for (final waiter in _waiters) { waiter.complete(); } _waiters.clear(); } } } /// Completes once nothing is pending. Future idle() { if (_pending == 0) return Future.value(); final waiter = Completer(); _waiters.add(waiter); return waiter.future; } }