2016-05-21 16:44:52 +02:00
|
|
|
<?php
|
|
|
|
|
2016-05-24 05:48:28 +02:00
|
|
|
namespace Amp\Internal;
|
2016-05-21 16:44:52 +02:00
|
|
|
|
2016-05-24 05:48:28 +02:00
|
|
|
use Amp\Failure;
|
2016-05-21 16:44:52 +02:00
|
|
|
use Interop\Async\Awaitable;
|
|
|
|
use Interop\Async\Loop;
|
|
|
|
|
2016-06-01 19:18:11 +02:00
|
|
|
/**
|
|
|
|
* Trait used by Awaitable implementations. Do not use this trait in your code, instead compose your class from one of
|
|
|
|
* the available classes implementing \Interop\Async\Awaitable.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
2016-05-21 16:44:52 +02:00
|
|
|
trait Placeholder {
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $resolved = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
private $result;
|
|
|
|
|
|
|
|
/**
|
2016-05-24 05:48:28 +02:00
|
|
|
* @var callable|\Amp\Internal\WhenQueue|null
|
2016-05-21 16:44:52 +02:00
|
|
|
*/
|
|
|
|
private $onResolved;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function when(callable $onResolved) {
|
|
|
|
if ($this->resolved) {
|
|
|
|
if ($this->result instanceof Awaitable) {
|
|
|
|
$this->result->when($onResolved);
|
2016-05-24 04:32:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$onResolved(null, $this->result);
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
Loop::defer(static function () use ($exception) {
|
|
|
|
throw $exception;
|
|
|
|
});
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
Loop::defer(static function () use ($exception) {
|
|
|
|
throw $exception;
|
|
|
|
});
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null === $this->onResolved) {
|
|
|
|
$this->onResolved = $onResolved;
|
|
|
|
} elseif (!$this->onResolved instanceof WhenQueue) {
|
|
|
|
$this->onResolved = new WhenQueue($this->onResolved);
|
|
|
|
$this->onResolved->push($onResolved);
|
|
|
|
} else {
|
|
|
|
$this->onResolved->push($onResolved);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
protected function resolve($value = null) {
|
|
|
|
if ($this->resolved) {
|
2016-05-24 04:32:41 +02:00
|
|
|
throw new \LogicException("Awaitable has already been resolved");
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->resolved = true;
|
2016-06-15 04:53:50 +02:00
|
|
|
$this->result = $value;
|
2016-05-21 16:44:52 +02:00
|
|
|
|
2016-06-15 04:53:50 +02:00
|
|
|
if ($this->onResolved !== null) {
|
|
|
|
if ($this->result instanceof Awaitable) {
|
2016-05-21 16:44:52 +02:00
|
|
|
$this->result->when($this->onResolved);
|
2016-06-15 04:53:50 +02:00
|
|
|
} else {
|
2016-05-24 04:32:41 +02:00
|
|
|
try {
|
|
|
|
$onResolved = $this->onResolved;
|
|
|
|
$onResolved(null, $this->result);
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
Loop::defer(static function () use ($exception) {
|
|
|
|
throw $exception;
|
|
|
|
});
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
Loop::defer(static function () use ($exception) {
|
|
|
|
throw $exception;
|
|
|
|
});
|
|
|
|
}
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
|
|
|
|
2016-06-15 04:53:50 +02:00
|
|
|
$this->onResolved = null;
|
|
|
|
}
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Throwable|\Exception $reason
|
|
|
|
*/
|
|
|
|
protected function fail($reason) {
|
|
|
|
$this->resolve(new Failure($reason));
|
|
|
|
}
|
|
|
|
}
|