2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-08-16 06:46:26 +02:00
|
|
|
|
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;
|
2017-03-10 22:32:58 +01:00
|
|
|
use Amp\Loop;
|
2017-03-10 21:58:46 +01:00
|
|
|
use Amp\Promise;
|
2016-05-21 16:44:52 +02:00
|
|
|
|
2016-06-01 19:18:11 +02:00
|
|
|
/**
|
2020-09-27 06:14:17 +02:00
|
|
|
* Class used by Promise implementations. Do not use this trait in your code, instead compose your class from one of
|
|
|
|
* the available classes implementing {@see Promise}
|
2016-06-01 19:18:11 +02:00
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-09-27 06:14:17 +02:00
|
|
|
final class Placeholder
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-24 18:52:22 +02:00
|
|
|
private bool $resolved = false;
|
2016-05-21 16:44:52 +02:00
|
|
|
|
2020-09-24 18:52:22 +02:00
|
|
|
private mixed $result;
|
2016-12-23 23:48:24 +01:00
|
|
|
|
2020-03-28 12:23:46 +01:00
|
|
|
/** @var ResolutionQueue|null|callable(\Throwable|null, mixed): (Promise|\React\Promise\PromiseInterface|\Generator<mixed,
|
|
|
|
* Promise|\React\Promise\PromiseInterface|array<array-key, Promise|\React\Promise\PromiseInterface>, mixed,
|
|
|
|
* mixed>|null)|callable(\Throwable|null, mixed): void */
|
2016-05-21 16:44:52 +02:00
|
|
|
private $onResolved;
|
2016-12-23 23:48:24 +01:00
|
|
|
|
2020-09-25 05:17:13 +02:00
|
|
|
private ?array $resolutionTrace = null;
|
2017-03-29 17:25:44 +02:00
|
|
|
|
2016-05-21 16:44:52 +02:00
|
|
|
/**
|
2016-12-23 23:48:24 +01:00
|
|
|
* @inheritdoc
|
2016-05-21 16:44:52 +02:00
|
|
|
*/
|
2020-09-24 18:52:22 +02:00
|
|
|
public function onResolve(callable $onResolved): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-05-21 16:44:52 +02:00
|
|
|
if ($this->resolved) {
|
2016-11-14 20:59:21 +01:00
|
|
|
if ($this->result instanceof Promise) {
|
2017-03-21 17:23:37 +01:00
|
|
|
$this->result->onResolve($onResolved);
|
2016-05-24 04:32:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-01 18:19:17 +01:00
|
|
|
Loop::defer(fn() => $onResolved(null, $this->result));
|
2016-05-21 16:44:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null === $this->onResolved) {
|
|
|
|
$this->onResolved = $onResolved;
|
2016-06-17 06:27:14 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
if (!$this->onResolved instanceof ResolutionQueue) {
|
2020-03-28 20:27:42 +01:00
|
|
|
/** @psalm-suppress InternalClass */
|
2017-03-21 17:23:37 +01:00
|
|
|
$this->onResolved = new ResolutionQueue($this->onResolved);
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
2016-06-17 06:27:14 +02:00
|
|
|
|
2020-03-28 20:27:42 +01:00
|
|
|
/** @psalm-suppress InternalMethod */
|
2016-06-17 06:27:14 +02:00
|
|
|
$this->onResolved->push($onResolved);
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
|
|
|
|
2020-03-28 12:23:46 +01:00
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->result = null;
|
2020-11-30 05:36:55 +01:00
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
Loop::defer(static fn() => throw $exception);
|
2020-03-28 12:23:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-27 06:14:17 +02:00
|
|
|
public function isResolved(): bool
|
2020-07-16 20:50:38 +02:00
|
|
|
{
|
|
|
|
return $this->resolved;
|
|
|
|
}
|
|
|
|
|
2016-05-21 16:44:52 +02:00
|
|
|
/**
|
|
|
|
* @param mixed $value
|
2016-08-11 21:35:58 +02:00
|
|
|
*
|
2020-03-28 12:23:46 +01:00
|
|
|
* @return void
|
|
|
|
*
|
2016-11-14 20:59:21 +01:00
|
|
|
* @throws \Error Thrown if the promise has already been resolved.
|
2016-05-21 16:44:52 +02:00
|
|
|
*/
|
2020-09-27 06:14:17 +02:00
|
|
|
public function resolve(mixed $value = null): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-05-21 16:44:52 +02:00
|
|
|
if ($this->resolved) {
|
2017-03-29 17:25:44 +02:00
|
|
|
$message = "Promise has already been resolved";
|
|
|
|
|
|
|
|
if (isset($this->resolutionTrace)) {
|
|
|
|
$trace = formatStacktrace($this->resolutionTrace);
|
|
|
|
$message .= ". Previous resolution trace:\n\n{$trace}\n\n";
|
|
|
|
} else {
|
2017-12-02 16:54:28 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
$message .= ", define environment variable AMP_DEBUG or const AMP_DEBUG = true and enable assertions "
|
|
|
|
. "for a stacktrace of the previous resolution.";
|
|
|
|
// @codeCoverageIgnoreEnd
|
2017-03-29 17:25:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new \Error($message);
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 07:47:03 +02:00
|
|
|
\assert((function () {
|
2020-07-11 17:13:51 +02:00
|
|
|
if (isDebugEnabled()) {
|
2017-12-02 16:54:28 +01:00
|
|
|
$trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
|
2017-04-28 07:47:03 +02:00
|
|
|
\array_shift($trace); // remove current closure
|
2017-03-29 17:25:44 +02:00
|
|
|
$this->resolutionTrace = $trace;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
})());
|
|
|
|
|
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-17 06:27:14 +02:00
|
|
|
if ($this->onResolved === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$onResolved = $this->onResolved;
|
|
|
|
$this->onResolved = null;
|
2016-05-21 16:44:52 +02:00
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
if ($this->result instanceof Promise) {
|
2017-03-21 17:23:37 +01:00
|
|
|
$this->result->onResolve($onResolved);
|
2016-06-17 06:27:14 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-12-23 23:48:24 +01:00
|
|
|
|
2020-11-01 18:19:17 +01:00
|
|
|
Loop::defer(fn() => $onResolved(null, $this->result));
|
2021-02-12 06:02:31 +01:00
|
|
|
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-11 21:35:58 +02:00
|
|
|
* @param \Throwable $reason Failure reason.
|
2020-03-28 12:23:46 +01:00
|
|
|
*
|
|
|
|
* @return void
|
2016-05-21 16:44:52 +02:00
|
|
|
*/
|
2020-09-27 06:14:17 +02:00
|
|
|
public function fail(\Throwable $reason): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-05-21 16:44:52 +02:00
|
|
|
$this->resolve(new Failure($reason));
|
|
|
|
}
|
|
|
|
}
|