2016-08-17 22:25:54 -05:00
|
|
|
<?php declare(strict_types = 1);
|
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-11-14 13:59:21 -06:00
|
|
|
use Interop\Async\Promise;
|
2016-05-21 09:44:52 -05:00
|
|
|
|
|
|
|
/**
|
2016-11-14 13:59:21 -06:00
|
|
|
* An promise that cannot be externally resolved. Used by Deferred in development mode.
|
2016-06-01 12:18:11 -05:00
|
|
|
*
|
|
|
|
* @internal
|
2016-05-21 09:44:52 -05:00
|
|
|
*/
|
2016-11-14 13:59:21 -06:00
|
|
|
final class PrivatePromise implements Promise {
|
2016-05-22 13:24:39 -05:00
|
|
|
use Placeholder;
|
2016-05-21 09:44:52 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param callable(callable $resolve, callable $reject): void $resolver
|
|
|
|
*/
|
|
|
|
public function __construct(callable $resolver) {
|
|
|
|
/**
|
2016-11-14 13:59:21 -06:00
|
|
|
* Resolves the promise with the given promise or value.
|
2016-05-21 09:44:52 -05:00
|
|
|
*
|
2016-05-22 13:24:39 -05:00
|
|
|
* @param mixed $value
|
2016-05-21 09:44:52 -05:00
|
|
|
*/
|
|
|
|
$resolve = function ($value = null) {
|
|
|
|
$this->resolve($value);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-11-14 13:59:21 -06:00
|
|
|
* Fails the promise with the given exception.
|
2016-05-21 09:44:52 -05:00
|
|
|
*
|
2016-08-11 14:35:58 -05:00
|
|
|
* @param \Throwable $reason
|
2016-05-21 09:44:52 -05:00
|
|
|
*/
|
2016-08-11 14:35:58 -05:00
|
|
|
$fail = function (\Throwable $reason) {
|
2016-05-21 09:44:52 -05:00
|
|
|
$this->fail($reason);
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
$resolver($resolve, $fail);
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
$this->fail($exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|