Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: sdk/lib/async/future_impl.dart

Issue 12091105: Add Future.of that calls a function and captures the result (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Better comment. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/async/future.dart ('k') | tests/lib/async/future_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 _addListener(future._asListener()); 297 _addListener(future._asListener());
298 } else if (_hasValue) { 298 } else if (_hasValue) {
299 future._setValue(_resultOrListeners); 299 future._setValue(_resultOrListeners);
300 } else { 300 } else {
301 assert(_hasError); 301 assert(_hasError);
302 _clearUnhandledError(); 302 _clearUnhandledError();
303 future._setError(_resultOrListeners); 303 future._setError(_resultOrListeners);
304 } 304 }
305 } 305 }
306 306
307 _FutureListener _asListener() => new _FutureListener.wrap(this);
308 }
309
310 /**
311 * Transforming future base class.
312 *
313 * A transforming future is itself a future and a future listener.
314 * Subclasses override [_sendValue]/[_sendError] to intercept
315 * the results of a previous future.
316 */
317 abstract class _TransformFuture<S, T> extends _FutureImpl<T>
318 implements _FutureListener<S> {
319 // _FutureListener implementation.
320 _FutureListener _nextListener;
321
322 void _sendValue(S value);
323
324 void _sendError(AsyncError error);
325
326 void _subscribeTo(_FutureImpl future) {
327 future._addListener(this);
328 }
329
330 /** 307 /**
331 * Helper function to hand the result of transforming an incoming event. 308 * Helper function to handle the result of transforming an incoming event.
332 * 309 *
333 * If the result is itself a [Future], this future is linked to that 310 * If the result is itself a [Future], this future is linked to that
334 * future's output. If not, this future is completed with the result. 311 * future's output. If not, this future is completed with the result.
335 */ 312 */
336 void _setOrChainValue(var result) { 313 void _setOrChainValue(var result) {
337 if (result is Future) { 314 if (result is Future) {
338 // Result should be a Future<T>. 315 // Result should be a Future<T>.
339 if (result is _FutureImpl) { 316 if (result is _FutureImpl) {
340 _FutureImpl chainFuture = result; 317 _FutureImpl chainFuture = result;
341 chainFuture._chain(this); 318 chainFuture._chain(this);
342 return; 319 return;
343 } else { 320 } else {
344 Future future = result; 321 Future future = result;
345 future.then(_setValue, 322 future.then(_setValue,
346 onError: _setError); 323 onError: _setError);
347 return; 324 return;
348 } 325 }
349 } else { 326 } else {
350 // Result must be of type T. 327 // Result must be of type T.
351 _setValue(result); 328 _setValue(result);
352 } 329 }
353 } 330 }
331
332 _FutureListener _asListener() => new _FutureListener.wrap(this);
333 }
334
335 /**
336 * Transforming future base class.
337 *
338 * A transforming future is itself a future and a future listener.
339 * Subclasses override [_sendValue]/[_sendError] to intercept
340 * the results of a previous future.
341 */
342 abstract class _TransformFuture<S, T> extends _FutureImpl<T>
343 implements _FutureListener<S> {
344 // _FutureListener implementation.
345 _FutureListener _nextListener;
346
347 void _sendValue(S value);
348
349 void _sendError(AsyncError error);
350
351 void _subscribeTo(_FutureImpl future) {
352 future._addListener(this);
353 }
354 } 354 }
355 355
356 /** The onValue and onError handlers return either a value or a future */ 356 /** The onValue and onError handlers return either a value or a future */
357 typedef dynamic _FutureOnValue<T>(T value); 357 typedef dynamic _FutureOnValue<T>(T value);
358 typedef dynamic _FutureOnError(AsyncError error); 358 typedef dynamic _FutureOnError(AsyncError error);
359 /** Test used by [Future.catchError] to handle skip some errors. */ 359 /** Test used by [Future.catchError] to handle skip some errors. */
360 typedef bool _FutureErrorTest(var error); 360 typedef bool _FutureErrorTest(var error);
361 /** Used by [WhenFuture]. */ 361 /** Used by [WhenFuture]. */
362 typedef _FutureAction(); 362 typedef _FutureAction();
363 363
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 }
OLDNEW
« no previous file with comments | « sdk/lib/async/future.dart ('k') | tests/lib/async/future_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698