| 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 /// A collection of futures waits until all added [Future]s complete. | 7 /// A collection of futures waits until all added [Future]s complete. |
| 8 /// | 8 /// |
| 9 /// Futures are added to the group with [add]. Once you're finished adding | 9 /// Futures are added to the group with [add]. Once you're finished adding |
| 10 /// futures, signal that by calling [close]. Then, once all added futures have | 10 /// futures, signal that by calling [close]. Then, once all added futures have |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 if (_closed) throw new StateError("The FutureGroup is closed."); | 60 if (_closed) throw new StateError("The FutureGroup is closed."); |
| 61 | 61 |
| 62 // Ensure that future values are put into [values] in the same order they're | 62 // Ensure that future values are put into [values] in the same order they're |
| 63 // added to the group by pre-allocating a slot for them and recording its | 63 // added to the group by pre-allocating a slot for them and recording its |
| 64 // index. | 64 // index. |
| 65 var index = _values.length; | 65 var index = _values.length; |
| 66 _values.add(null); | 66 _values.add(null); |
| 67 | 67 |
| 68 _pending++; | 68 _pending++; |
| 69 task.then((value) { | 69 task.then((value) { |
| 70 if (_completer.isCompleted) return; | 70 if (_completer.isCompleted) return null; |
| 71 | 71 |
| 72 _pending--; | 72 _pending--; |
| 73 _values[index] = value; | 73 _values[index] = value; |
| 74 | 74 |
| 75 if (_pending != 0) return; | 75 if (_pending != 0) return null; |
| 76 if (_onIdleController != null) _onIdleController.add(null); | 76 if (_onIdleController != null) _onIdleController.add(null); |
| 77 | 77 |
| 78 if (!_closed) return; | 78 if (!_closed) return null; |
| 79 if (_onIdleController != null) _onIdleController.close(); | 79 if (_onIdleController != null) _onIdleController.close(); |
| 80 _completer.complete(_values); | 80 _completer.complete(_values); |
| 81 }).catchError((error, stackTrace) { | 81 }).catchError((error, stackTrace) { |
| 82 if (_completer.isCompleted) return; | 82 if (_completer.isCompleted) return null; |
| 83 _completer.completeError(error, stackTrace); | 83 _completer.completeError(error, stackTrace); |
| 84 }); | 84 }); |
| 85 } | 85 } |
| 86 | 86 |
| 87 /// Signals to the group that the caller is done adding futures, and so | 87 /// Signals to the group that the caller is done adding futures, and so |
| 88 /// [future] should fire once all added futures have completed. | 88 /// [future] should fire once all added futures have completed. |
| 89 void close() { | 89 void close() { |
| 90 _closed = true; | 90 _closed = true; |
| 91 if (_pending != 0) return; | 91 if (_pending != 0) return; |
| 92 if (_completer.isCompleted) return; | 92 if (_completer.isCompleted) return; |
| 93 _completer.complete(_values); | 93 _completer.complete(_values); |
| 94 } | 94 } |
| 95 } | 95 } |
| 96 | 96 |
| OLD | NEW |