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 $hasStarted = false; | |||||
private $hasEnded = false; | |||||
private $result; | private $result; | ||||
private $exception; | private $exception; | ||||
private $futureKey; | |||||
/** | /** | ||||
* 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->hasException()) { | |||||
throw $this->getException(); | |||||
} | |||||
if (!$this->hasResult()) { | if (!$this->hasResult()) { | ||||
$graph = new FutureIterator(array($this)); | $graph = new FutureIterator(array($this)); | ||||
$graph->resolveAll(); | $graph->resolveAll(); | ||||
} | } | ||||
if ($this->hasException()) { | return $this->getResult(); | ||||
throw $this->getException(); | |||||
} | } | ||||
return $this->getResult(); | final public function startFuture() { | ||||
if ($this->hasStarted) { | |||||
throw new Exception( | |||||
pht( | |||||
'Future has already started; futures can not start more '. | |||||
'than once.')); | |||||
} | |||||
$this->hasStarted = true; | |||||
$this->isReady(); | |||||
} | } | ||||
final public function updateFuture() { | final public function updateFuture() { | ||||
if ($this->hasException()) { | if ($this->hasException()) { | ||||
return; | return; | ||||
} | } | ||||
if ($this->hasResult()) { | if ($this->hasResult()) { | ||||
return; | return; | ||||
} | } | ||||
try { | try { | ||||
$this->isReady(); | $this->isReady(); | ||||
} catch (Exception $ex) { | } catch (Exception $ex) { | ||||
$this->setException($ex); | $this->setException($ex); | ||||
} catch (Throwable $ex) { | } catch (Throwable $ex) { | ||||
$this->setException($ex); | $this->setException($ex); | ||||
} | } | ||||
} | } | ||||
final public function endFuture() { | |||||
if (!$this->hasException() && !$this->hasResult()) { | |||||
throw new Exception( | |||||
pht( | |||||
'Trying to end a future which has no exception and no result. '. | |||||
'Futures must resolve before they can be ended.')); | |||||
} | |||||
if ($this->hasEnded) { | |||||
throw new Exception( | |||||
pht( | |||||
'Future has already ended; futures can not end more '. | |||||
'than once.')); | |||||
} | |||||
$this->hasEnded = true; | |||||
} | |||||
/** | /** | ||||
* 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. | ||||
* | * | ||||
* @return list A list of sockets which we expect to become readable. | * @return list A list of sockets which we expect to become readable. | ||||
*/ | */ | ||||
public function getReadSockets() { | public function getReadSockets() { | ||||
return array(); | return array(); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | abstract class Future extends Phobject { | ||||
final private function getException() { | final private function getException() { | ||||
return $this->exception; | return $this->exception; | ||||
} | } | ||||
final public function hasException() { | final public function hasException() { | ||||
return ($this->exception !== null); | return ($this->exception !== null); | ||||
} | } | ||||
final public function setFutureKey($key) { | |||||
if ($this->futureKey !== null) { | |||||
throw new Exception( | |||||
pht( | |||||
'Future already has a key ("%s") assigned.', | |||||
$key)); | |||||
} | |||||
$this->futureKey = $key; | |||||
return $this; | |||||
} | |||||
final public function getFutureKey() { | |||||
static $next_key = 1; | |||||
if ($this->futureKey === null) { | |||||
$this->futureKey = sprintf('Future/%d', $next_key++); | |||||
} | |||||
return $this->futureKey; | |||||
} | |||||
} | } |