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

Unified Diff: pkg/scheduled_test/lib/src/future_group.dart

Issue 12288061: Support nested tasks in scheduled_test. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 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
Index: pkg/scheduled_test/lib/src/future_group.dart
diff --git a/pkg/scheduled_test/lib/src/future_group.dart b/pkg/scheduled_test/lib/src/future_group.dart
new file mode 100644
index 0000000000000000000000000000000000000000..213f0fc4b555a84a9426f71e37712e6f4a2d6e8d
--- /dev/null
+++ b/pkg/scheduled_test/lib/src/future_group.dart
@@ -0,0 +1,49 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library future_group;
+
+import 'dart:async';
+
+/// A completer that waits until all added [Future]s complete.
+// TODO(rnystrom): Copied from web_components. Remove from here when it gets
+// added to dart:core. (See #6626.)
+class FutureGroup<T> {
+ int _pending = 0;
+ Completer<List<T>> _completer = new Completer<List<T>>();
+ final List<Future<T>> futures = <Future<T>>[];
Bob Nystrom 2013/02/19 23:15:04 Is there a reason to store these?
nweiz 2013/02/20 00:23:12 I don't want to change this code here without chan
+ bool completed = false;
+
+ final List<T> _values = <T>[];
+
+ /// Wait for [task] to complete.
+ Future<T> add(Future<T> task) {
+ if (completed) {
+ throw new StateError("The FutureGroup has already completed.");
+ }
+
+ _pending++;
+ futures.add(task.then((value) {
+ if (completed) return;
Bob Nystrom 2013/02/19 23:15:04 It seems weird to swallow this. Doesn't this indic
nweiz 2013/02/20 00:23:12 In this code, the futures passed to FutureGroup ar
+
+ _pending--;
+ _values.add(value);
+
+ if (_pending <= 0) {
+ completed = true;
+ _completer.complete(_values);
+ }
+ }).catchError((e) {
+ if (completed) return;
Bob Nystrom 2013/02/19 23:15:04 Ditto here.
+
+ completed = true;
Bob Nystrom 2013/02/19 23:15:04 It seems like every place we use a completer we en
nweiz 2013/02/20 00:23:12 Filed as issue 8638.
+ _completer.completeError(e.error, e.stackTrace);
+ }));
+
+ return task;
+ }
+
+ Future<List> get future => _completer.future;
+}
+

Powered by Google App Engine
This is Rietveld 408576698