| 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 part of dart.async; | 5 part of dart.async; |
| 6 | 6 |
| 7 deprecatedFutureValue(_FutureImpl future) => | 7 deprecatedFutureValue(_FutureImpl future) => |
| 8 future._isComplete ? future._resultOrListeners : null; | 8 future._isComplete ? future._resultOrListeners : null; |
| 9 | 9 |
| 10 | 10 |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 return _handleError(f, test); | 172 return _handleError(f, test); |
| 173 } | 173 } |
| 174 } | 174 } |
| 175 | 175 |
| 176 Future<T> whenComplete(action()) { | 176 Future<T> whenComplete(action()) { |
| 177 _WhenFuture<T> whenFuture = new _WhenFuture<T>(action); | 177 _WhenFuture<T> whenFuture = new _WhenFuture<T>(action); |
| 178 if (!_isComplete) { | 178 if (!_isComplete) { |
| 179 _addListener(whenFuture); | 179 _addListener(whenFuture); |
| 180 } else if (_hasValue) { | 180 } else if (_hasValue) { |
| 181 T value = _resultOrListeners; | 181 T value = _resultOrListeners; |
| 182 new Timer(0, (_) { | 182 Timer.run(() { |
| 183 whenFuture._sendValue(value); | 183 whenFuture._sendValue(value); |
| 184 }); | 184 }); |
| 185 } else { | 185 } else { |
| 186 assert(_hasError); | 186 assert(_hasError); |
| 187 _clearUnhandledError(); | 187 _clearUnhandledError(); |
| 188 AsyncError error = _resultOrListeners; | 188 AsyncError error = _resultOrListeners; |
| 189 new Timer(0, (_) { | 189 Timer.run(() { |
| 190 whenFuture._sendError(error); | 190 whenFuture._sendError(error); |
| 191 }); | 191 }); |
| 192 } | 192 } |
| 193 return whenFuture; | 193 return whenFuture; |
| 194 } | 194 } |
| 195 | 195 |
| 196 /** Handle a late listener on a completed future with a value. */ | 196 /** Handle a late listener on a completed future with a value. */ |
| 197 Future _handleValue(onValue(var value)) { | 197 Future _handleValue(onValue(var value)) { |
| 198 assert(_hasValue); | 198 assert(_hasValue); |
| 199 _ThenFuture thenFuture = new _ThenFuture(onValue); | 199 _ThenFuture thenFuture = new _ThenFuture(onValue); |
| 200 T value = _resultOrListeners; | 200 T value = _resultOrListeners; |
| 201 new Timer(0, (_) { thenFuture._sendValue(value); }); | 201 Timer.run(() { thenFuture._sendValue(value); }); |
| 202 return thenFuture; | 202 return thenFuture; |
| 203 } | 203 } |
| 204 | 204 |
| 205 /** Handle a late listener on a completed future with an error. */ | 205 /** Handle a late listener on a completed future with an error. */ |
| 206 Future _handleError(onError(AsyncError error), bool test(error)) { | 206 Future _handleError(onError(AsyncError error), bool test(error)) { |
| 207 assert(_hasError); | 207 assert(_hasError); |
| 208 _clearUnhandledError(); | 208 _clearUnhandledError(); |
| 209 AsyncError error = _resultOrListeners; | 209 AsyncError error = _resultOrListeners; |
| 210 _CatchErrorFuture errorFuture = new _CatchErrorFuture(onError, test); | 210 _CatchErrorFuture errorFuture = new _CatchErrorFuture(onError, test); |
| 211 new Timer(0, (_) { errorFuture._sendError(error); }); | 211 Timer.run(() { errorFuture._sendError(error); }); |
| 212 return errorFuture; | 212 return errorFuture; |
| 213 } | 213 } |
| 214 | 214 |
| 215 Stream<T> asStream() => new Stream.fromFuture(this); | 215 Stream<T> asStream() => new Stream.fromFuture(this); |
| 216 | 216 |
| 217 void _setValue(T value) { | 217 void _setValue(T value) { |
| 218 if (_isComplete) throw new StateError("Future already completed"); | 218 if (_isComplete) throw new StateError("Future already completed"); |
| 219 _FutureListener listeners = _removeListeners(); | 219 _FutureListener listeners = _removeListeners(); |
| 220 _state = _VALUE; | 220 _state = _VALUE; |
| 221 _resultOrListeners = value; | 221 _resultOrListeners = value; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 241 listeners = listener._nextListener; | 241 listeners = listener._nextListener; |
| 242 listener._nextListener = null; | 242 listener._nextListener = null; |
| 243 listener._sendError(error); | 243 listener._sendError(error); |
| 244 } while (listeners != null); | 244 } while (listeners != null); |
| 245 } | 245 } |
| 246 | 246 |
| 247 void _scheduleUnhandledError() { | 247 void _scheduleUnhandledError() { |
| 248 _state |= _UNHANDLED_ERROR; | 248 _state |= _UNHANDLED_ERROR; |
| 249 // Wait for the rest of the current event's duration to see | 249 // Wait for the rest of the current event's duration to see |
| 250 // if a subscriber is added to handle the error. | 250 // if a subscriber is added to handle the error. |
| 251 new Timer(0, (_) { | 251 Timer.run(() { |
| 252 if (_hasUnhandledError) { | 252 if (_hasUnhandledError) { |
| 253 // No error handler has been added since the error was set. | 253 // No error handler has been added since the error was set. |
| 254 _clearUnhandledError(); | 254 _clearUnhandledError(); |
| 255 AsyncError error = _resultOrListeners; | 255 AsyncError error = _resultOrListeners; |
| 256 print("Uncaught Error: ${error.error}"); | 256 print("Uncaught Error: ${error.error}"); |
| 257 if (error.stackTrace != null) { | 257 if (error.stackTrace != null) { |
| 258 print("Stack Trace:\n${error.stackTrace}\n"); | 258 print("Stack Trace:\n${error.stackTrace}\n"); |
| 259 } | 259 } |
| 260 throw error.error; | 260 throw error.error; |
| 261 } | 261 } |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 Future catchError(function(AsyncError error), {bool test(var error)}) { | 521 Future catchError(function(AsyncError error), {bool test(var error)}) { |
| 522 return _future.catchError(function, test: test); | 522 return _future.catchError(function, test: test); |
| 523 } | 523 } |
| 524 | 524 |
| 525 Future<T> whenComplete(action()) { | 525 Future<T> whenComplete(action()) { |
| 526 return _future.whenComplete(action); | 526 return _future.whenComplete(action); |
| 527 } | 527 } |
| 528 | 528 |
| 529 Stream<T> asStream() => new Stream.fromFuture(_future); | 529 Stream<T> asStream() => new Stream.fromFuture(_future); |
| 530 } | 530 } |
| OLD | NEW |