| 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 // TODO(lrn): This should be in package:async? | 5 // TODO(lrn): This should be in package:async? |
| 6 /// Helper functions for working with errors. | 6 /// Helper functions for working with errors. |
| 7 /// | 7 /// |
| 8 /// The [MultiError] class combines multiple errors into one object, | 8 /// The [MultiError] class combines multiple errors into one object, |
| 9 /// and the [MultiError.wait] function works like [Future.wait] except | 9 /// and the [MultiError.wait] function works like [Future.wait] except |
| 10 /// that it returns all the errors. | 10 /// that it returns all the errors. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 /// with a [MultiError] if more than one future completes with an error. | 39 /// with a [MultiError] if more than one future completes with an error. |
| 40 /// | 40 /// |
| 41 /// The order of values is not preserved (if that is needed, use | 41 /// The order of values is not preserved (if that is needed, use |
| 42 /// [wait]). | 42 /// [wait]). |
| 43 static Future<List> waitUnordered(Iterable<Future> futures, | 43 static Future<List> waitUnordered(Iterable<Future> futures, |
| 44 {cleanUp(successResult)}) { | 44 {cleanUp(successResult)}) { |
| 45 Completer completer; | 45 Completer completer; |
| 46 int count = 0; | 46 int count = 0; |
| 47 int errors = 0; | 47 int errors = 0; |
| 48 int values = 0; | 48 int values = 0; |
| 49 // Initilized to `new List(count)` when count is known. | 49 // Initialized to `new List(count)` when count is known. |
| 50 // Filled up with values on the left, errors on the right. | 50 // Filled up with values on the left, errors on the right. |
| 51 // Order is not preserved. | 51 // Order is not preserved. |
| 52 List results; | 52 List results; |
| 53 void checkDone() { | 53 void checkDone() { |
| 54 if (errors + values < count) return; | 54 if (errors + values < count) return; |
| 55 if (errors == 0) { | 55 if (errors == 0) { |
| 56 completer.complete(results); | 56 completer.complete(results); |
| 57 return; | 57 return; |
| 58 } | 58 } |
| 59 var errorList = results.sublist(results.length - errors); | 59 var errorList = results.sublist(results.length - errors); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 /// with a [MultiError] if more than one future completes with an error. | 96 /// with a [MultiError] if more than one future completes with an error. |
| 97 /// | 97 /// |
| 98 /// The order of values is preserved, and if any error occurs, the | 98 /// The order of values is preserved, and if any error occurs, the |
| 99 /// [MultiError.errors] list will have errors in the corresponding slots, | 99 /// [MultiError.errors] list will have errors in the corresponding slots, |
| 100 /// and `null` for non-errors. | 100 /// and `null` for non-errors. |
| 101 Future<List> wait(Iterable<Future> futures, {cleanUp(successResult)}) { | 101 Future<List> wait(Iterable<Future> futures, {cleanUp(successResult)}) { |
| 102 Completer completer; | 102 Completer completer; |
| 103 int count = 0; | 103 int count = 0; |
| 104 bool hasError = false; | 104 bool hasError = false; |
| 105 int completed = 0; | 105 int completed = 0; |
| 106 // Initalized to `new List(count)` when count is known. | 106 // Initialized to `new List(count)` when count is known. |
| 107 // Filled with values until the first error, then cleared | 107 // Filled with values until the first error, then cleared |
| 108 // and filled with errors. | 108 // and filled with errors. |
| 109 List results; | 109 List results; |
| 110 void checkDone() { | 110 void checkDone() { |
| 111 completed++; | 111 completed++; |
| 112 if (completed < count) return; | 112 if (completed < count) return; |
| 113 if (!hasError) { | 113 if (!hasError) { |
| 114 completer.complete(results); | 114 completer.complete(results); |
| 115 return; | 115 return; |
| 116 } | 116 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 } | 169 } |
| 170 buffer.write("#$index: "); | 170 buffer.write("#$index: "); |
| 171 buffer.write(errorString.substring(0, end)); | 171 buffer.write(errorString.substring(0, end)); |
| 172 if (end < errorString.length) { | 172 if (end < errorString.length) { |
| 173 buffer.write("...\n"); | 173 buffer.write("...\n"); |
| 174 } | 174 } |
| 175 } | 175 } |
| 176 return buffer.toString(); | 176 return buffer.toString(); |
| 177 } | 177 } |
| 178 } | 178 } |
| OLD | NEW |