2016-12-29 14:09:49 -06:00
|
|
|
<?php
|
2016-08-15 23:46:26 -05:00
|
|
|
|
2016-05-23 22:48:28 -05:00
|
|
|
namespace Amp\Internal;
|
2016-05-21 09:44:52 -05:00
|
|
|
|
2016-05-23 22:48:28 -05:00
|
|
|
use Amp\Failure;
|
2017-03-10 15:32:58 -06:00
|
|
|
use Amp\Loop;
|
2017-03-10 21:58:46 +01:00
|
|
|
use Amp\Promise;
|
2017-02-20 14:53:58 -06:00
|
|
|
use React\Promise\PromiseInterface as ReactPromise;
|
2017-03-12 12:09:54 -05:00
|
|
|
use function Amp\adapt;
|
2016-05-21 09:44:52 -05:00
|
|
|
|
2016-06-01 12:18:11 -05:00
|
|
|
/**
|
2016-11-14 13:59:21 -06:00
|
|
|
* Trait used by Promise implementations. Do not use this trait in your code, instead compose your class from one of
|
2017-03-10 21:58:46 +01:00
|
|
|
* the available classes implementing \Amp\Promise.
|
2016-06-01 12:18:11 -05:00
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
2016-05-21 09:44:52 -05:00
|
|
|
trait Placeholder {
|
2016-08-17 22:25:54 -05:00
|
|
|
/** @var bool */
|
2016-05-21 09:44:52 -05:00
|
|
|
private $resolved = false;
|
|
|
|
|
2016-08-17 22:25:54 -05:00
|
|
|
/** @var mixed */
|
2016-05-21 09:44:52 -05:00
|
|
|
private $result;
|
2016-12-23 23:48:24 +01:00
|
|
|
|
2016-08-17 22:25:54 -05:00
|
|
|
/** @var callable|\Amp\Internal\WhenQueue|null */
|
2016-05-21 09:44:52 -05:00
|
|
|
private $onResolved;
|
2016-12-23 23:48:24 +01:00
|
|
|
|
2016-05-21 09:44:52 -05:00
|
|
|
/**
|
2016-12-23 23:48:24 +01:00
|
|
|
* @inheritdoc
|
2016-05-21 09:44:52 -05:00
|
|
|
*/
|
|
|
|
public function when(callable $onResolved) {
|
|
|
|
if ($this->resolved) {
|
2016-11-14 13:59:21 -06:00
|
|
|
if ($this->result instanceof Promise) {
|
2016-05-21 09:44:52 -05:00
|
|
|
$this->result->when($onResolved);
|
2016-05-23 21:32:41 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$onResolved(null, $this->result);
|
|
|
|
} catch (\Throwable $exception) {
|
2017-03-10 15:32:58 -06:00
|
|
|
Loop::defer(function () use ($exception) {
|
|
|
|
throw $exception;
|
|
|
|
});
|
2016-05-21 09:44:52 -05:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null === $this->onResolved) {
|
|
|
|
$this->onResolved = $onResolved;
|
2016-06-16 23:27:14 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->onResolved instanceof WhenQueue) {
|
2016-05-21 09:44:52 -05:00
|
|
|
$this->onResolved = new WhenQueue($this->onResolved);
|
|
|
|
}
|
2016-06-16 23:27:14 -05:00
|
|
|
|
|
|
|
$this->onResolved->push($onResolved);
|
2016-05-21 09:44:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $value
|
2016-08-11 14:35:58 -05:00
|
|
|
*
|
2016-11-14 13:59:21 -06:00
|
|
|
* @throws \Error Thrown if the promise has already been resolved.
|
2016-05-21 09:44:52 -05:00
|
|
|
*/
|
2016-07-19 12:32:43 -05:00
|
|
|
private function resolve($value = null) {
|
2016-05-21 09:44:52 -05:00
|
|
|
if ($this->resolved) {
|
2016-11-14 13:59:21 -06:00
|
|
|
throw new \Error("Promise has already been resolved");
|
2016-05-21 09:44:52 -05:00
|
|
|
}
|
|
|
|
|
2017-02-20 14:53:58 -06:00
|
|
|
if ($value instanceof ReactPromise) {
|
|
|
|
$value = adapt($value);
|
|
|
|
}
|
|
|
|
|
2016-05-21 09:44:52 -05:00
|
|
|
$this->resolved = true;
|
2016-06-14 21:53:50 -05:00
|
|
|
$this->result = $value;
|
2016-05-21 09:44:52 -05:00
|
|
|
|
2016-06-16 23:27:14 -05:00
|
|
|
if ($this->onResolved === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$onResolved = $this->onResolved;
|
|
|
|
$this->onResolved = null;
|
2016-05-21 09:44:52 -05:00
|
|
|
|
2016-11-14 13:59:21 -06:00
|
|
|
if ($this->result instanceof Promise) {
|
2016-06-16 23:27:14 -05:00
|
|
|
$this->result->when($onResolved);
|
|
|
|
return;
|
|
|
|
}
|
2016-12-23 23:48:24 +01:00
|
|
|
|
2016-06-16 23:27:14 -05:00
|
|
|
try {
|
|
|
|
$onResolved(null, $this->result);
|
|
|
|
} catch (\Throwable $exception) {
|
2017-03-10 15:32:58 -06:00
|
|
|
Loop::defer(function () use ($exception) {
|
|
|
|
throw $exception;
|
|
|
|
});
|
2016-06-14 21:53:50 -05:00
|
|
|
}
|
2016-05-21 09:44:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-11 14:35:58 -05:00
|
|
|
* @param \Throwable $reason Failure reason.
|
2016-05-21 09:44:52 -05:00
|
|
|
*/
|
2016-08-11 14:35:58 -05:00
|
|
|
private function fail(\Throwable $reason) {
|
2016-05-21 09:44:52 -05:00
|
|
|
$this->resolve(new Failure($reason));
|
|
|
|
}
|
|
|
|
}
|