Changeset View
Changeset View
Standalone View
Standalone View
src/future/Future.php
| <?php | <?php | ||||
| /** | /** | ||||
| * A 'future' or 'promise' is an object which represents the result of some | * A 'future' or 'promise' is an object which represents the result of some | ||||
| * pending computation. For a more complete overview of futures, see | * pending computation. For a more complete overview of futures, see | ||||
| * @{article:Using Futures}. | * @{article:Using Futures}. | ||||
| */ | */ | ||||
| abstract class Future extends Phobject { | abstract class Future extends Phobject { | ||||
| private $hasResult = false; | private $hasResult = false; | ||||
| private $result; | private $result; | ||||
| private $exception; | |||||
| protected $exception; | |||||
| /** | /** | ||||
| * Is this future's process complete? Specifically, can this future be | * Is this future's process complete? Specifically, can this future be | ||||
| * resolved without blocking? | * resolved without blocking? | ||||
| * | * | ||||
| * @return bool If true, the external process is complete and resolving this | * @return bool If true, the external process is complete and resolving this | ||||
| * future will not block. | * future will not block. | ||||
| */ | */ | ||||
| Show All 10 Lines | public function resolve() { | ||||
| if (count($args)) { | if (count($args)) { | ||||
| throw new Exception( | throw new Exception( | ||||
| pht( | pht( | ||||
| 'Parameter "timeout" to "Future->resolve()" is no longer '. | 'Parameter "timeout" to "Future->resolve()" is no longer '. | ||||
| 'supported. Update the caller so it no longer passes a '. | 'supported. Update the caller so it no longer passes a '. | ||||
| 'timeout.')); | 'timeout.')); | ||||
| } | } | ||||
| if (!$this->hasResult()) { | |||||
| $graph = new FutureIterator(array($this)); | $graph = new FutureIterator(array($this)); | ||||
| $graph->resolveAll(); | $graph->resolveAll(); | ||||
| } | |||||
| if ($this->exception) { | if ($this->hasException()) { | ||||
| throw $this->exception; | throw $this->getException(); | ||||
| } | } | ||||
| return $this->getResult(); | return $this->getResult(); | ||||
| } | } | ||||
| public function setException(Exception $ex) { | final public function updateFuture() { | ||||
| $this->exception = $ex; | if ($this->hasException()) { | ||||
| return $this; | return; | ||||
| } | } | ||||
| public function getException() { | if ($this->hasResult()) { | ||||
| return $this->exception; | return; | ||||
| } | |||||
| try { | |||||
| $this->isReady(); | |||||
| } catch (Exception $ex) { | |||||
| $this->setException($ex); | |||||
| } catch (Throwable $ex) { | |||||
| $this->setException($ex); | |||||
| } | |||||
| } | } | ||||
| /** | /** | ||||
| * Retrieve a list of sockets which we can wait to become readable while | * Retrieve a list of sockets which we can wait to become readable while | ||||
| * a future is resolving. If your future has sockets which can be | * a future is resolving. If your future has sockets which can be | ||||
| * `select()`ed, return them here (or in @{method:getWriteSockets}) to make | * `select()`ed, return them here (or in @{method:getWriteSockets}) to make | ||||
| * the resolve loop do a `select()`. If you do not return sockets in either | * the resolve loop do a `select()`. If you do not return sockets in either | ||||
| * case, you'll get a busy wait. | * case, you'll get a busy wait. | ||||
| ▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | final protected function setResult($result) { | ||||
| } | } | ||||
| $this->hasResult = true; | $this->hasResult = true; | ||||
| $this->result = $result; | $this->result = $result; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| final protected function hasResult() { | final public function hasResult() { | ||||
| return $this->hasResult; | return $this->hasResult; | ||||
| } | } | ||||
| final private function setException($exception) { | |||||
| // NOTE: The parameter may be an Exception or a Throwable. | |||||
| $this->exception = $exception; | |||||
| return $this; | |||||
| } | |||||
| final private function getException() { | |||||
| return $this->exception; | |||||
| } | |||||
| final public function hasException() { | |||||
| return ($this->exception !== null); | |||||
| } | |||||
| } | } | ||||