| 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 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 | 314 |
| 315 Promise call(List message) { | 315 Promise call(List message) { |
| 316 return _marshal(message, (List marshalled) { | 316 return _marshal(message, (List marshalled) { |
| 317 // TODO(kasperl): For now, the [Promise.then] implementation allows | 317 // TODO(kasperl): For now, the [Promise.then] implementation allows |
| 318 // me to return a promise and it will do the promise chaining. | 318 // me to return a promise and it will do the promise chaining. |
| 319 final result = new Promise(); | 319 final result = new Promise(); |
| 320 // The promise queue implementation guarantees that promise is | 320 // The promise queue implementation guarantees that promise is |
| 321 // resolved at this point. | 321 // resolved at this point. |
| 322 SendPortImpl outgoing = _promise.value; | 322 SendPortImpl outgoing = _promise.value; |
| 323 ReceivePort incoming = outgoing._callNow(marshalled); | 323 ReceivePort incoming = outgoing._callNow(marshalled); |
| 324 incoming.receive((List message, replyTo) { | 324 incoming.receive((List receiveMessage, replyTo) { |
| 325 result.complete(message[0]); | 325 result.complete(receiveMessage[0]); |
| 326 }); | 326 }); |
| 327 return result; | 327 return result; |
| 328 }); | 328 }); |
| 329 } | 329 } |
| 330 | 330 |
| 331 // Marshal the [message] and pass it to the [process] callback | 331 // Marshal the [message] and pass it to the [process] callback |
| 332 // function. Any promises are converted to a port which expects to | 332 // function. Any promises are converted to a port which expects to |
| 333 // receive a port from the other side down which the remote promise | 333 // receive a port from the other side down which the remote promise |
| 334 // can be completed by sending the promise's completion value. | 334 // can be completed by sending the promise's completion value. |
| 335 Promise _marshal(List message, process(List marshalled)) { | 335 Promise _marshal(List message, process(List marshalled)) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 350 // This port will receive a SendPort that can be used to | 350 // This port will receive a SendPort that can be used to |
| 351 // signal completion of this promise to the corresponding | 351 // signal completion of this promise to the corresponding |
| 352 // promise that the other end has created. | 352 // promise that the other end has created. |
| 353 ReceivePort receiveCompleter = new ReceivePort.singleShot(); | 353 ReceivePort receiveCompleter = new ReceivePort.singleShot(); |
| 354 marshalled[i] = receiveCompleter.toSendPort(); | 354 marshalled[i] = receiveCompleter.toSendPort(); |
| 355 Promise<SendPort> completer = new Promise<SendPort>(); | 355 Promise<SendPort> completer = new Promise<SendPort>(); |
| 356 receiveCompleter.receive((var msg, SendPort replyPort) { | 356 receiveCompleter.receive((var msg, SendPort replyPort) { |
| 357 completer.complete(msg[0]); | 357 completer.complete(msg[0]); |
| 358 }); | 358 }); |
| 359 entry.addCompleteHandler((value) { | 359 entry.addCompleteHandler((value) { |
| 360 completer.addCompleteHandler((SendPort port) { | 360 completer.addCompleteHandler((SendPort completePort) { |
| 361 _marshal([value], (List message) => port.send(message, null)); | 361 _marshal([value], (List completeMessage) => completePort.send(comp
leteMessage, null)); |
| 362 }); | 362 }); |
| 363 }); | 363 }); |
| 364 } else { | 364 } else { |
| 365 // FIXME(kasperl, benl): this should probably be a copy? | 365 // FIXME(kasperl, benl): this should probably be a copy? |
| 366 marshalled[i] = entry; | 366 marshalled[i] = entry; |
| 367 } | 367 } |
| 368 if (marshalled[i] is ReceivePort) { | 368 if (marshalled[i] is ReceivePort) { |
| 369 throw new Exception("Despite the documentation, you cannot send a Rece
ivePort"); | 369 throw new Exception("Despite the documentation, you cannot send a Rece
ivePort"); |
| 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 |