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 // TODO(lrn): This should be in package:async? | 5 // TODO(lrn): This should be in package:async? |
| 6 /** | 6 /** |
| 7 * Helper functions for working with errors. | 7 * Helper functions for working with errors. |
| 8 * | 8 * |
| 9 * The [MultiError] class combines multiple errors into one object, | 9 * The [MultiError] class combines multiple errors into one object, |
| 10 * and the [MultiError.wait] function works like [Future.wait] except | 10 * and the [MultiError.wait] function works like [Future.wait] except |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 40 * Waits for all [futures] to complete, like [Future.wait]. | 40 * Waits for all [futures] to complete, like [Future.wait]. |
| 41 * | 41 * |
| 42 * Where `Future.wait` only reports one error, even if multiple | 42 * Where `Future.wait` only reports one error, even if multiple |
| 43 * futures complete with errors, this function will complete | 43 * futures complete with errors, this function will complete |
| 44 * with a [MultiError] if more than one future completes with an error. | 44 * with a [MultiError] if more than one future completes with an error. |
| 45 * | 45 * |
| 46 * The order of values is not preserved (if that is needed, use | 46 * The order of values is not preserved (if that is needed, use |
| 47 * [wait]). | 47 * [wait]). |
| 48 */ | 48 */ |
| 49 static Future<List> waitUnordered(Iterable<Future> futures, | 49 static Future<List> waitUnordered(Iterable<Future> futures, |
| 50 {cleanUp(successResult)}) { | 50 {cleanUp(successResult)}) { |
|
Lasse Reichstein Nielsen
2015/02/26 10:59:15
Indent as original.
| |
| 51 Completer completer; | 51 Completer completer; |
| 52 int count = 0; | 52 int count = 0; |
| 53 int errors = 0; | 53 int errors = 0; |
| 54 int values = 0; | 54 int values = 0; |
| 55 // Initilized to `new List(count)` when count is known. | 55 // Initilized to `new List(count)` when count is known. |
| 56 // Filled up with values on the left, errors on the right. | 56 // Filled up with values on the left, errors on the right. |
| 57 // Order is not preserved. | 57 // Order is not preserved. |
| 58 List results; | 58 List results; |
| 59 void checkDone() { | 59 void checkDone() { |
| 60 if (errors + values < count) return; | 60 if (errors + values < count) return; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 99 * Waits for all [futures] to complete, like [Future.wait]. | 99 * Waits for all [futures] to complete, like [Future.wait]. |
| 100 * | 100 * |
| 101 * Where `Future.wait` only reports one error, even if multiple | 101 * Where `Future.wait` only reports one error, even if multiple |
| 102 * futures complete with errors, this function will complete | 102 * futures complete with errors, this function will complete |
| 103 * with a [MultiError] if more than one future completes with an error. | 103 * with a [MultiError] if more than one future completes with an error. |
| 104 * | 104 * |
| 105 * The order of values is preserved, and if any error occurs, the | 105 * The order of values is preserved, and if any error occurs, the |
| 106 * [MultiError.errors] list will have errors in the corresponding slots, | 106 * [MultiError.errors] list will have errors in the corresponding slots, |
| 107 * and `null` for non-errors. | 107 * and `null` for non-errors. |
| 108 */ | 108 */ |
| 109 Future<List> wait(Iterable<Future> futures, | 109 Future<List> wait(Iterable<Future> futures, {cleanUp(successResult)}) { |
| 110 {cleanUp(successResult)}) { | |
| 111 Completer completer; | 110 Completer completer; |
| 112 int count = 0; | 111 int count = 0; |
| 113 bool hasError = false; | 112 bool hasError = false; |
| 114 int completed = 0; | 113 int completed = 0; |
| 115 // Initalized to `new List(count)` when count is known. | 114 // Initalized to `new List(count)` when count is known. |
| 116 // Filled with values until the first error, then cleared | 115 // Filled with values until the first error, then cleared |
| 117 // and filled with errors. | 116 // and filled with errors. |
| 118 List results; | 117 List results; |
| 119 void checkDone() { | 118 void checkDone() { |
| 120 completed++; | 119 completed++; |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 149 results[i] = e; | 148 results[i] = e; |
| 150 checkDone(); | 149 checkDone(); |
| 151 }); | 150 }); |
| 152 } | 151 } |
| 153 if (count == 0) return new Future.value(new List(0)); | 152 if (count == 0) return new Future.value(new List(0)); |
| 154 results = new List(count); | 153 results = new List(count); |
| 155 completer = new Completer(); | 154 completer = new Completer(); |
| 156 return completer.future; | 155 return completer.future; |
| 157 } | 156 } |
| 158 | 157 |
| 159 | |
| 160 String toString() { | 158 String toString() { |
| 161 StringBuffer buffer = new StringBuffer(); | 159 StringBuffer buffer = new StringBuffer(); |
| 162 buffer.write("Multiple Errors:\n"); | 160 buffer.write("Multiple Errors:\n"); |
| 163 int linesPerError = _MAX_LINES ~/ errors.length; | 161 int linesPerError = _MAX_LINES ~/ errors.length; |
| 164 if (linesPerError < _MIN_LINES_PER_ERROR) { | 162 if (linesPerError < _MIN_LINES_PER_ERROR) { |
| 165 linesPerError = _MIN_LINES_PER_ERROR; | 163 linesPerError = _MIN_LINES_PER_ERROR; |
| 166 } | 164 } |
| 167 | 165 |
| 168 for (int index = 0; index < errors.length; index++) { | 166 for (int index = 0; index < errors.length; index++) { |
| 169 var error = errors[index]; | 167 var error = errors[index]; |
| 170 if (error == null) continue; | 168 if (error == null) continue; |
| 171 String errorString = error.toString(); | 169 String errorString = error.toString(); |
| 172 int end = 0; | 170 int end = 0; |
| 173 for (int i = 0; i < linesPerError; i++) { | 171 for (int i = 0; i < linesPerError; i++) { |
| 174 end = errorString.indexOf('\n', end) + 1; | 172 end = errorString.indexOf('\n', end) + 1; |
| 175 if (end == 0) { | 173 if (end == 0) { |
| 176 end = errorString.length; | 174 end = errorString.length; |
| 177 break; | 175 break; |
| 178 } | 176 } |
| 179 } | 177 } |
| 180 buffer.write("#$index: "); | 178 buffer.write("#$index: "); |
| 181 buffer.write(errorString.substring(0, end)); | 179 buffer.write(errorString.substring(0, end)); |
| 182 if (end < errorString.length) { | 180 if (end < errorString.length) { |
| 183 buffer.write("...\n"); | 181 buffer.write("...\n"); |
| 184 } | 182 } |
| 185 } | 183 } |
| 186 return buffer.toString(); | 184 return buffer.toString(); |
| 187 } | 185 } |
| 188 } | 186 } |
| OLD | NEW |