Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(284)

Unified Diff: lib/src/future_group.dart

Issue 1215063008: Add FutureGroup.onIdle and FutureGroup.isIdle. (Closed) Base URL: git@github.com:dart-lang/async.git@master
Patch Set: Code review changes Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « CHANGELOG.md ('k') | test/future_group_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/future_group.dart
diff --git a/lib/src/future_group.dart b/lib/src/future_group.dart
index 81ba4fae78767787b172801b578ad7b94f0de079..02ff185ca74adc9261ec5cb58cd69249ae336206 100644
--- a/lib/src/future_group.dart
+++ b/lib/src/future_group.dart
@@ -35,6 +35,22 @@ class FutureGroup<T> implements Sink<Future<T>> {
Future<List<T>> get future => _completer.future;
final _completer = new Completer<List<T>>();
+ /// Whether this group is waiting on any futures.
+ bool get isIdle => _pending == 0;
+
+ /// A broadcast stream that emits a `null` event whenever the last pending
+ /// future in this group completes.
+ ///
+ /// Once this group isn't waiting on any futures *and* [close] has been
+ /// called, this stream will close.
+ Stream get onIdle {
+ if (_onIdleController == null) {
+ _onIdleController = new StreamController.broadcast(sync: true);
+ }
+ return _onIdleController.stream;
+ }
+ StreamController _onIdleController;
+
/// The values emitted by the futures that have been added to the group, in
/// the order they were added.
///
@@ -58,7 +74,12 @@ class FutureGroup<T> implements Sink<Future<T>> {
_pending--;
_values[index] = value;
- if (_pending == 0 && _closed) _completer.complete(_values);
+ if (_pending != 0) return;
+ if (_onIdleController != null) _onIdleController.add(null);
+
+ if (!_closed) return;
+ if (_onIdleController != null) _onIdleController.close();
+ _completer.complete(_values);
}).catchError((error, stackTrace) {
if (_completer.isCompleted) return;
_completer.completeError(error, stackTrace);
« no previous file with comments | « CHANGELOG.md ('k') | test/future_group_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698