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

Side by Side Diff: quiver/lib/src/async/future_group.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 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 unified diff | Download patch
« no previous file with comments | « quiver/lib/src/async/countdown_timer.dart ('k') | quiver/lib/src/async/future_stream.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 part of quiver.async;
16
17 /**
18 * A collection of [Future]s that signals when all added Futures complete. New
19 * Futures can be added to the group as long as it hasn't completed.
20 *
21 * FutureGroup is useful for managing a set of async tasks that may spawn new
22 * async tasks as they execute.
23 */
24 class FutureGroup<E> {
25 static const _FINISHED = -1;
26
27 int _pending = 0;
28 Future _failedTask;
29 final Completer<List> _completer = new Completer<List>();
30 final List results = [];
31
32 /** Gets the task that failed, if any. */
33 Future get failedTask => _failedTask;
34
35 /**
36 * Wait for [task] to complete.
37 *
38 * If this group has already been marked as completed, a [StateError] will be
39 * thrown.
40 *
41 * If this group has a [failedTask], new tasks will be ignored, because the
42 * error has already been signaled.
43 */
44 void add(Future task) {
45 if (_failedTask != null) return;
46 if (_pending == _FINISHED) throw new StateError("Future already completed");
47
48 _pending++;
49 var i = results.length;
50 results.add(null);
51 task.then((res) {
52 results[i] = res;
53 if (_failedTask != null) return;
54 _pending--;
55 if (_pending == 0) {
56 _pending = _FINISHED;
57 _completer.complete(results);
58 }
59 }, onError: (e, s) {
60 if (_failedTask != null) return;
61 _failedTask = task;
62 _completer.completeError(e, s);
63 });
64 }
65
66 /**
67 * A Future that complets with a List of the values from all the added
68 * tasks, when they have all completed.
69 *
70 * If any task fails, this Future will receive the error. Only the first
71 * error will be sent to the Future.
72 */
73 Future<List<E>> get future => _completer.future;
74 }
OLDNEW
« no previous file with comments | « quiver/lib/src/async/countdown_timer.dart ('k') | quiver/lib/src/async/future_stream.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698