Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library async.future_group; | 5 library async.future_group; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 /// A collection of futures waits until all added [Future]s complete. | 9 /// A collection of futures waits until all added [Future]s complete. |
| 10 /// | 10 /// |
| 11 /// Futures are added to the group with [add]. Once you're finished adding | 11 /// Futures are added to the group with [add]. Once you're finished adding |
| 12 /// futures, signal that by calling [close]. Then, once all added futures have | 12 /// futures, signal that by calling [close]. Then, once all added futures have |
| 13 /// completed, [future] will complete with a list of values from the futures in | 13 /// completed, [future] will complete with a list of values from the futures in |
| 14 /// the group, in the order they were added. | 14 /// the group, in the order they were added. |
| 15 /// | 15 /// |
| 16 /// If any added future completes with an error, [future] will emit that error | 16 /// If any added future completes with an error, [future] will emit that error |
| 17 /// and the group will be closed, regardless of the state of other futures in | 17 /// and the group will be closed, regardless of the state of other futures in |
| 18 /// the group. | 18 /// the group. |
| 19 /// | 19 /// |
| 20 /// This is similar to [Future.wait] with `eagerError` set to `true`, except | 20 /// This is similar to [Future.wait] with `eagerError` set to `true`, except |
| 21 /// that a [FutureGroup] can have futures added gradually over time rather than | 21 /// that a [FutureGroup] can have futures added gradually over time rather than |
| 22 /// needing them all at once. | 22 /// needing them all at once. |
| 23 class FutureGroup<T> implements Sink<Future<T>> { | 23 class FutureGroup<T> implements Sink<Future<T>> { |
| 24 /// The number of futures that have yet to complete. | 24 /// The number of futures that have yet to complete. |
| 25 int get pending => _pending; | |
|
Lasse Reichstein Nielsen
2015/07/02 12:25:58
Unrelated change?
nweiz
2015/07/06 21:04:16
Done.
| |
| 25 var _pending = 0; | 26 var _pending = 0; |
| 26 | 27 |
| 27 /// Whether [close] has been called. | 28 /// Whether [close] has been called. |
| 28 var _closed = false; | 29 var _closed = false; |
| 29 | 30 |
| 30 /// The future that fires once [close] has been called and all futures in the | 31 /// The future that fires once [close] has been called and all futures in the |
| 31 /// group have completed. | 32 /// group have completed. |
| 32 /// | 33 /// |
| 33 /// This will also complete with an error if any of the futures in the group | 34 /// This will also complete with an error if any of the futures in the group |
| 34 /// fails, regardless of whether [close] was called. | 35 /// fails, regardless of whether [close] was called. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 68 /// Signals to the group that the caller is done adding futures, and so | 69 /// Signals to the group that the caller is done adding futures, and so |
| 69 /// [future] should fire once all added futures have completed. | 70 /// [future] should fire once all added futures have completed. |
| 70 void close() { | 71 void close() { |
| 71 _closed = true; | 72 _closed = true; |
| 72 if (_pending != 0) return; | 73 if (_pending != 0) return; |
| 73 if (_completer.isCompleted) return; | 74 if (_completer.isCompleted) return; |
| 74 _completer.complete(_values); | 75 _completer.complete(_values); |
| 75 } | 76 } |
| 76 } | 77 } |
| 77 | 78 |
| OLD | NEW |