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

Side by Side Diff: lib/coreimpl/future_implementation.dart

Issue 11231043: Move FutureImpl and CompleterImpl from coreimpl to core, while also making them private. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 2 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 | « lib/coreimpl/corelib_impl_sources.gypi ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2012 Google Inc. All Rights Reserved.
2 // Dart core library.
3
4 class FutureImpl<T> implements Future<T> {
5
6 bool _isComplete = false;
7
8 /**
9 * Value that was provided to this Future by the Completer
10 */
11 T _value;
12
13 /**
14 * Exception that occured, if there was a problem providing
15 * Value.
16 */
17 Object _exception;
18
19 /**
20 * Stack trace associated with [_exception], if one was provided.
21 */
22 Object _stackTrace;
23
24 /**
25 * true, if any onException handler handled the exception.
26 */
27 bool _exceptionHandled = false;
28
29 /**
30 * Listeners waiting to receive the value of this future.
31 */
32 final List<Function> _successListeners;
33
34 /**
35 * Exception handlers waiting for exceptions.
36 */
37 final List<Function> _exceptionHandlers;
38
39 /**
40 * Listeners waiting to be called when the future completes.
41 */
42 final List<Function> _completionListeners;
43
44 FutureImpl()
45 : _successListeners = [],
46 _exceptionHandlers = [],
47 _completionListeners = [];
48
49 factory FutureImpl.immediate(T value) {
50 final res = new FutureImpl();
51 res._setValue(value);
52 return res;
53 }
54
55 T get value {
56 if (!isComplete) {
57 throw new FutureNotCompleteException();
58 }
59 if (_exception !== null) {
60 throw new FutureUnhandledException(_exception, stackTrace);
61 }
62 return _value;
63 }
64
65 Object get exception {
66 if (!isComplete) {
67 throw new FutureNotCompleteException();
68 }
69 return _exception;
70 }
71
72 Object get stackTrace {
73 if (!isComplete) {
74 throw new FutureNotCompleteException();
75 }
76 return _stackTrace;
77 }
78
79 bool get isComplete {
80 return _isComplete;
81 }
82
83 bool get hasValue {
84 return isComplete && _exception === null;
85 }
86
87 void then(void onSuccess(T value)) {
88 if (hasValue) {
89 onSuccess(value);
90 } else if (!isComplete) {
91 _successListeners.add(onSuccess);
92 } else if (!_exceptionHandled) {
93 throw new FutureUnhandledException(_exception, stackTrace);
94 }
95 }
96
97 void handleException(bool onException(Object exception)) {
98 if (_exceptionHandled) return;
99 if (_isComplete) {
100 if (_exception != null) {
101 _exceptionHandled = onException(_exception);
102 }
103 } else {
104 _exceptionHandlers.add(onException);
105 }
106 }
107
108 void onComplete(void complete(Future<T> future)) {
109 if (_isComplete) {
110 try {
111 complete(this);
112 } catch (e) {}
113 } else {
114 _completionListeners.add(complete);
115 }
116 }
117
118 void _complete() {
119 _isComplete = true;
120
121 try {
122 if (_exception !== null) {
123 for (Function handler in _exceptionHandlers) {
124 // Explicitly check for true here so that if the handler returns null,
125 // we don't get an exception in checked mode.
126 if (handler(_exception) == true) {
127 _exceptionHandled = true;
128 break;
129 }
130 }
131 }
132
133 if (hasValue) {
134 for (Function listener in _successListeners) {
135 listener(value);
136 }
137 } else {
138 if (!_exceptionHandled && _successListeners.length > 0) {
139 throw new FutureUnhandledException(_exception, stackTrace);
140 }
141 }
142 } finally {
143 for (Function listener in _completionListeners) {
144 try {
145 listener(this);
146 } catch (e) {}
147 }
148 }
149 }
150
151 void _setValue(T value) {
152 if (_isComplete) {
153 throw new FutureAlreadyCompleteException();
154 }
155 _value = value;
156 _complete();
157 }
158
159 void _setException(Object exception, Object stackTrace) {
160 if (exception === null) {
161 // null is not a legal value for the exception of a Future.
162 throw new ArgumentError(null);
163 }
164 if (_isComplete) {
165 throw new FutureAlreadyCompleteException();
166 }
167 _exception = exception;
168 _stackTrace = stackTrace;
169 _complete();
170 }
171
172 Future transform(Function transformation) {
173 final completer = new Completer();
174
175 _forwardException(this, completer);
176
177 then((v) {
178 var transformed = null;
179 try {
180 transformed = transformation(v);
181 } catch (e, stackTrace) {
182 completer.completeException(e, stackTrace);
183 return;
184 }
185 completer.complete(transformed);
186 });
187
188 return completer.future;
189 }
190
191 Future chain(Function transformation) {
192 final completer = new Completer();
193
194 _forwardException(this, completer);
195 then((v) {
196 var future = null;
197 try {
198 future = transformation(v);
199 } catch (ex, stackTrace) {
200 completer.completeException(ex, stackTrace);
201 return;
202 }
203
204 _forward(future, completer);
205 });
206 return completer.future;
207 }
208
209 Future transformException(transformation(Object exception)) {
210 final completer = new Completer();
211
212 handleException((ex) {
213 try {
214 final result = transformation(ex);
215
216 // If the transformation itself returns a future, then we will
217 // complete to what that completes to.
218 if (result is Future) {
219 _forward(result, completer);
220 } else {
221 completer.complete(result);
222 }
223 } catch (innerException, stackTrace) {
224 completer.completeException(innerException, stackTrace);
225 }
226 return true;
227 });
228
229 then(completer.complete);
230
231 return completer.future;
232 }
233
234 /**
235 * Forwards the success or error completion from [future] to [completer].
236 */
237 _forward(Future future, Completer completer) {
238 _forwardException(future, completer);
239 future.then(completer.complete);
240 }
241
242 /**
243 * Forwards the exception completion from [future] to [completer].
244 */
245 _forwardException(Future future, Completer completer) {
246 future.handleException((e) {
247 completer.completeException(e, future.stackTrace);
248 return true;
249 });
250 }
251 }
252
253 class CompleterImpl<T> implements Completer<T> {
254
255 final FutureImpl<T> _futureImpl;
256
257 CompleterImpl() : _futureImpl = new FutureImpl() {}
258
259 Future<T> get future {
260 return _futureImpl;
261 }
262
263 void complete(T value) {
264 _futureImpl._setValue(value);
265 }
266
267 void completeException(Object exception, [Object stackTrace]) {
268 _futureImpl._setException(exception, stackTrace);
269 }
270 }
OLDNEW
« no previous file with comments | « lib/coreimpl/corelib_impl_sources.gypi ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698