| OLD | NEW |
| 1 // Copyright 2013 Google Inc. All Rights Reserved. | 1 // Copyright 2013 Google Inc. All Rights Reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 part of quiver.async; | 15 part of quiver.async; |
| 16 | 16 |
| 17 /** | 17 /// A collection of [Future]s that signals when all added Futures complete. New |
| 18 * A collection of [Future]s that signals when all added Futures complete. New | 18 /// Futures can be added to the group as long as it hasn't completed. |
| 19 * Futures can be added to the group as long as it hasn't completed. | 19 /// |
| 20 * | 20 /// FutureGroup is useful for managing a set of async tasks that may spawn new |
| 21 * FutureGroup is useful for managing a set of async tasks that may spawn new | 21 /// async tasks as they execute. |
| 22 * async tasks as they execute. | 22 /// |
| 23 */ | 23 /// DEPRECATED: use `FutureGroup` from `package:async`. Note that it requires a |
| 24 /// `close()` call before auto-completion will be triggered upon the count of |
| 25 /// pending tasks dropping to 0. |
| 26 @deprecated |
| 24 class FutureGroup<E> { | 27 class FutureGroup<E> { |
| 25 static const _FINISHED = -1; | 28 static const _FINISHED = -1; |
| 26 | 29 |
| 27 int _pending = 0; | 30 int _pending = 0; |
| 28 Future _failedTask; | 31 Future _failedTask; |
| 29 final Completer<List> _completer = new Completer<List>(); | 32 final Completer<List> _completer = new Completer<List>(); |
| 30 final List results = []; | 33 final List results = []; |
| 31 | 34 |
| 32 /** Gets the task that failed, if any. */ | 35 /// Gets the task that failed, if any. |
| 33 Future get failedTask => _failedTask; | 36 Future get failedTask => _failedTask; |
| 34 | 37 |
| 35 /** | 38 /// Wait for [task] to complete. |
| 36 * Wait for [task] to complete. | 39 /// |
| 37 * | 40 /// If this group has already been marked as completed, a [StateError] will |
| 38 * If this group has already been marked as completed, a [StateError] will be | 41 /// be thrown. |
| 39 * thrown. | 42 /// |
| 40 * | 43 /// If this group has a [failedTask], new tasks will be ignored, because the |
| 41 * If this group has a [failedTask], new tasks will be ignored, because the | 44 /// error has already been signaled. |
| 42 * error has already been signaled. | |
| 43 */ | |
| 44 void add(Future task) { | 45 void add(Future task) { |
| 45 if (_failedTask != null) return; | 46 if (_failedTask != null) return; |
| 46 if (_pending == _FINISHED) throw new StateError("Future already completed"); | 47 if (_pending == _FINISHED) throw new StateError("Future already completed"); |
| 47 | 48 |
| 48 _pending++; | 49 _pending++; |
| 49 var i = results.length; | 50 var i = results.length; |
| 50 results.add(null); | 51 results.add(null); |
| 51 task.then((res) { | 52 task.then((res) { |
| 52 results[i] = res; | 53 results[i] = res; |
| 53 if (_failedTask != null) return; | 54 if (_failedTask != null) return; |
| 54 _pending--; | 55 _pending--; |
| 55 if (_pending == 0) { | 56 if (_pending == 0) { |
| 56 _pending = _FINISHED; | 57 _pending = _FINISHED; |
| 57 _completer.complete(results); | 58 _completer.complete(results); |
| 58 } | 59 } |
| 59 }, onError: (e, s) { | 60 }, onError: (e, s) { |
| 60 if (_failedTask != null) return; | 61 if (_failedTask != null) return; |
| 61 _failedTask = task; | 62 _failedTask = task; |
| 62 _completer.completeError(e, s); | 63 _completer.completeError(e, s); |
| 63 }); | 64 }); |
| 64 } | 65 } |
| 65 | 66 |
| 66 /** | 67 /// A Future that complets with a List of the values from all the added |
| 67 * A Future that complets with a List of the values from all the added | 68 /// tasks, when they have all completed. |
| 68 * tasks, when they have all completed. | 69 /// |
| 69 * | 70 /// If any task fails, this Future will receive the error. Only the first |
| 70 * If any task fails, this Future will receive the error. Only the first | 71 /// error will be sent to the Future. |
| 71 * error will be sent to the Future. | |
| 72 */ | |
| 73 Future<List<E>> get future => _completer.future; | 72 Future<List<E>> get future => _completer.future; |
| 74 } | 73 } |
| OLD | NEW |