OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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 // Dart core library. | 5 // Dart core library. |
6 | 6 |
7 class PromiseImpl<T> implements Promise<T> { | 7 class PromiseImpl<T> implements Promise<T> { |
8 | 8 |
9 // Enumeration of possible states: | 9 // Enumeration of possible states: |
10 static final int CREATED = 0; | 10 static final int CREATED = 0; |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 addCompleteHandler((val) { | 255 addCompleteHandler((val) { |
256 promises.forEach((promise) { | 256 promises.forEach((promise) { |
257 if (!promise.isDone()) { | 257 if (!promise.isDone()) { |
258 promise.cancel(); | 258 promise.cancel(); |
259 } | 259 } |
260 }); | 260 }); |
261 }); | 261 }); |
262 } | 262 } |
263 } | 263 } |
264 | 264 |
265 class ProxyBase { | 265 // For now, extend Promise<bool> rather than either |
| 266 // a) create a new base, Completable, for Promise and Proxy, or |
| 267 // b) extend Promise<SendPort> which would expose the port. |
| 268 class ProxyBase extends PromiseImpl<bool> { |
266 | 269 |
267 ProxyBase.forPort(SendPort port) { | 270 ProxyBase.forPort(SendPort port) { |
268 _promise = new Promise<SendPort>(); | 271 _promise = new Promise<SendPort>(); |
269 _promise.complete(port); | 272 _promise.complete(port); |
| 273 complete(true); |
270 } | 274 } |
271 | 275 |
272 // Construct a proxy for a message reply; see the [Proxy.forReply] | 276 // Construct a proxy for a message reply; see the [Proxy.forReply] |
273 // documentation for more details. | 277 // documentation for more details. |
274 ProxyBase.forReply(Promise<SendPort> port) { | 278 ProxyBase.forReply(Promise<SendPort> port) { |
275 _promise = port; | 279 _promise = port; |
| 280 port.addCompleteHandler((_) => complete(true)); |
276 } | 281 } |
277 | 282 |
278 // Note that comparing proxies or using them in maps is illegal | 283 // Note that comparing proxies or using them in maps or sets is |
279 // until they complete. | 284 // illegal until they complete. |
280 bool operator ==(var other) { | 285 bool operator ==(var other) { |
281 return (other is ProxyBase) && _promise.value == other._promise.value; | 286 return (other is ProxyBase) && _promise.value == other._promise.value; |
282 } | 287 } |
283 | 288 |
284 int hashCode() => _promise.value.hashCode(); | 289 int hashCode() => _promise.value.hashCode(); |
285 | 290 |
286 // TODO: consider making this extend Promise<SendPort> instead? | |
287 void addCompleteHandler(void completeHandler()) { | |
288 _promise.addCompleteHandler((_) => completeHandler()); | |
289 } | |
290 | |
291 static ReceivePort register(Dispatcher dispatcher) { | 291 static ReceivePort register(Dispatcher dispatcher) { |
292 if (_dispatchers === null) { | 292 if (_dispatchers === null) { |
293 _dispatchers = new Map<SendPort, Dispatcher>(); | 293 _dispatchers = new Map<SendPort, Dispatcher>(); |
294 } | 294 } |
295 ReceivePort result = new ReceivePort(); | 295 ReceivePort result = new ReceivePort(); |
296 _dispatchers[result.toSendPort()] = dispatcher; | 296 _dispatchers[result.toSendPort()] = dispatcher; |
297 return result; | 297 return result; |
298 } | 298 } |
299 | 299 |
300 get local() { | 300 get local() { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 } | 370 } |
371 } | 371 } |
372 return process(marshalled); | 372 return process(marshalled); |
373 }).flatten(); | 373 }).flatten(); |
374 } | 374 } |
375 | 375 |
376 Promise<SendPort> _promise; | 376 Promise<SendPort> _promise; |
377 static Map<SendPort, Dispatcher> _dispatchers; | 377 static Map<SendPort, Dispatcher> _dispatchers; |
378 | 378 |
379 } | 379 } |
OLD | NEW |