Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 // Dart core library. | |
| 6 | |
| 7 | |
| 8 /** | |
| 9 * A Future is used to obtain a value sometime in the | |
| 10 * future. | |
| 11 * | |
| 12 * Receivers of a Future obtain the value by passing | |
| 13 * a callback to the 'then' method of Future. | |
| 14 * | |
| 15 * For example: | |
| 16 * | |
| 17 * Future<int> future = getFutureFromSomewhere(); | |
| 18 * future.then((value) { | |
| 19 * print("I received the number " + value); | |
| 20 * }); | |
| 21 * | |
| 22 */ | |
| 23 interface Future<T> factory FutureImpl<T> { | |
| 24 | |
| 25 /** | |
| 26 * The value this future provided. (If called when hasValue | |
| 27 * is false, then throws an exception.) | |
| 28 */ | |
| 29 T get value(); | |
| 30 | |
| 31 /** | |
| 32 * Exception that occurred (null if no exception occured). (If called | |
| 33 * before [isComplete] is true, then this exception property itself | |
| 34 * throws a FutureNotCompleteException.) | |
| 35 */ | |
| 36 Object get exception(); | |
| 37 | |
| 38 /** | |
| 39 * Whether the future is complete (either the value is available or there | |
| 40 * was an exception). | |
| 41 */ | |
| 42 bool get isComplete(); | |
| 43 | |
| 44 /** | |
| 45 * Whether the value is available (meaning isComplete is true, and there | |
| 46 * was no exception). | |
| 47 */ | |
| 48 bool get hasValue(); | |
| 49 | |
| 50 /** | |
| 51 * When this future is complete and has a value, then call | |
| 52 * the onComplete callback function with the value. | |
| 53 */ | |
| 54 void then(void onComplete(T value)); | |
| 55 | |
| 56 /** | |
| 57 * If this future gets an exception, then call onException. | |
| 58 * | |
| 59 * If onException returns true, then the exception is considered | |
| 60 * handled. | |
| 61 * | |
| 62 * If onException does not return true (or handleException was never called), | |
| 63 * then the exception is not considered handled. In that case, if there were | |
| 64 * any calls to [then] (meaning that there are onComplete callbacks waiting | |
| 65 * for the value), then the exception will be thrown when it is set. | |
|
MarkM
2011/10/13 22:35:35
"...when it is set."
When what is set?
| |
| 66 * | |
| 67 * (In most cases it should not be necessary to call handleException, | |
| 68 * because the exception associated with this Future will propagate naturally | |
|
MarkM
2011/10/13 22:35:35
If the future is being consumed only via "then", I
| |
| 69 * if the future's value is being consumed. Only call handleException if you | |
| 70 * need to do some special local exception handling related to this | |
| 71 * particular Future's value.) | |
| 72 */ | |
| 73 void handleException(bool onException(Object exception)); | |
| 74 } | |
| 75 | |
| 76 | |
| 77 /** | |
| 78 * A Completer is used to produce Future objects, and supply | |
| 79 * a value to the Future object when the value becomes available. | |
| 80 * | |
| 81 * A service that provides values to callers, and wants to return Future objects | |
| 82 * rather than returning the values immediately, can use a Completer as follows: | |
| 83 * | |
| 84 * Completer completer = new Completer(); | |
| 85 * Future future = completer.future; | |
| 86 * | |
| 87 * // send [future] object back to client... | |
| 88 * | |
| 89 * // later when value is available, call: | |
| 90 * completer.complete(value); | |
| 91 * | |
| 92 * // alternatively, if the service cannot produce the value, it | |
| 93 * // can provide an exception: | |
| 94 * completer.completeException(exception); | |
| 95 * | |
| 96 */ | |
| 97 interface Completer<T> factory CompleterImpl<T> { | |
| 98 | |
| 99 /** Create a completer */ | |
| 100 Completer(); | |
| 101 | |
| 102 Future get future(); | |
| 103 | |
| 104 /** | |
| 105 * Called when value is available. | |
| 106 */ | |
| 107 void complete(T value); | |
| 108 | |
| 109 /** | |
| 110 * Called if an exception occured while trying to produce value. | |
| 111 */ | |
| 112 void completeException(Object exception); | |
|
arv (Not doing code reviews)
2011/10/13 21:15:46
Can this be called fail or reject instead?
Siggi Cherem (dart-lang)
2011/10/13 21:51:48
One reason we were leaning towards 'completeExcept
| |
| 113 } | |
| OLD | NEW |