2016-05-21 16:44:52 +02:00
|
|
|
<?php
|
|
|
|
|
2016-05-22 20:24:39 +02:00
|
|
|
namespace Amp\Awaitable\Internal;
|
2016-05-21 16:44:52 +02:00
|
|
|
|
|
|
|
use Interop\Async\Awaitable;
|
|
|
|
|
|
|
|
/**
|
2016-05-22 20:24:39 +02:00
|
|
|
* An awaitable that cannot be externally resolved. Used by Deferred in development mode.
|
2016-05-21 16:44:52 +02:00
|
|
|
*/
|
2016-05-22 20:24:39 +02:00
|
|
|
final class PrivateAwaitable implements Awaitable {
|
|
|
|
use Placeholder;
|
2016-05-21 16:44:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param callable(callable $resolve, callable $reject): void $resolver
|
|
|
|
*/
|
|
|
|
public function __construct(callable $resolver) {
|
|
|
|
/**
|
2016-05-22 20:24:39 +02:00
|
|
|
* Resolves the awaitable with the given awaitable or value.
|
2016-05-21 16:44:52 +02:00
|
|
|
*
|
2016-05-22 20:24:39 +02:00
|
|
|
* @param mixed $value
|
2016-05-21 16:44:52 +02:00
|
|
|
*/
|
|
|
|
$resolve = function ($value = null) {
|
|
|
|
$this->resolve($value);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-05-22 20:24:39 +02:00
|
|
|
* Fails the awaitable with the given exception.
|
2016-05-21 16:44:52 +02:00
|
|
|
*
|
|
|
|
* @param \Exception $reason
|
|
|
|
*/
|
|
|
|
$fail = function ($reason) {
|
|
|
|
$this->fail($reason);
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
$resolver($resolve, $fail);
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
$this->fail($exception);
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
$this->fail($exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|