OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /// Generic utility functions. Stuff that should possibly be in core. | 5 /// Generic utility functions. Stuff that should possibly be in core. |
6 library utils; | 6 library utils; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:crypto'; | 9 import 'dart:crypto'; |
10 import 'dart:isolate'; | 10 import 'dart:isolate'; |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 return completer.future; | 111 return completer.future; |
112 } | 112 } |
113 | 113 |
114 /// Configures [future] so that its result (success or exception) is passed on | 114 /// Configures [future] so that its result (success or exception) is passed on |
115 /// to [completer]. | 115 /// to [completer]. |
116 void chainToCompleter(Future future, Completer completer) { | 116 void chainToCompleter(Future future, Completer completer) { |
117 future.then((value) => completer.complete(value), | 117 future.then((value) => completer.complete(value), |
118 onError: (e) => completer.completeError(e.error, e.stackTrace)); | 118 onError: (e) => completer.completeError(e.error, e.stackTrace)); |
119 } | 119 } |
120 | 120 |
121 // TODO(nweiz): remove this when issue 7790 is fixed. | |
122 /// Like [Future.whenComplete], except that [action] may return a [Future]. If | |
123 /// it does, the returned [Future] won't complete until [action]'s [Future] | |
124 /// completes. | |
125 /// | |
126 /// The returned [Future] always has the same value as [future]. | |
127 Future asyncWhenComplete(Future future, Future action()) { | |
128 Future futurify(Future future) { | |
129 if (future != null) return future; | |
130 return new Future.immediate(null); | |
131 } | |
132 | |
133 return future.then((result) => futurify(action()).then((_) => result), | |
134 onError: (e) { | |
135 return futurify(action()).then((_) { | |
136 throw e; | |
137 }); | |
138 }); | |
139 } | |
140 | |
141 // TODO(nweiz): unify the following functions with the utility functions in | 121 // TODO(nweiz): unify the following functions with the utility functions in |
142 // pkg/http. | 122 // pkg/http. |
143 | 123 |
144 /// Like [String.split], but only splits on the first occurrence of the pattern. | 124 /// Like [String.split], but only splits on the first occurrence of the pattern. |
145 /// This will always return an array of two elements or fewer. | 125 /// This will always return an array of two elements or fewer. |
146 List<String> split1(String toSplit, String pattern) { | 126 List<String> split1(String toSplit, String pattern) { |
147 if (toSplit.isEmpty) return <String>[]; | 127 if (toSplit.isEmpty) return <String>[]; |
148 | 128 |
149 var index = toSplit.indexOf(pattern); | 129 var index = toSplit.indexOf(pattern); |
150 if (index == -1) return [toSplit]; | 130 if (index == -1) return [toSplit]; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 /// AsyncErrors. This unwraps them to find the real originating stack trace. | 194 /// AsyncErrors. This unwraps them to find the real originating stack trace. |
215 getRealStackTrace(error) { | 195 getRealStackTrace(error) { |
216 var trace = null; | 196 var trace = null; |
217 while (error is AsyncError) { | 197 while (error is AsyncError) { |
218 trace = error.stackTrace; | 198 trace = error.stackTrace; |
219 error = error.error; | 199 error = error.error; |
220 } | 200 } |
221 | 201 |
222 return trace; | 202 return trace; |
223 } | 203 } |
OLD | NEW |