1
0
mirror of https://github.com/danog/amp.git synced 2024-12-04 18:38:17 +01:00
amp/lib/Internal/Placeholder.php

180 lines
5.0 KiB
PHP
Raw Normal View History

<?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
use Amp\Coroutine;
2016-05-24 05:48:28 +02:00
use Amp\Failure;
use Amp\Loop;
use Amp\Promise;
2017-02-20 21:53:58 +01:00
use React\Promise\PromiseInterface as ReactPromise;
2016-05-21 16:44:52 +02:00
2016-06-01 19:18:11 +02:00
/**
2016-11-14 20:59:21 +01:00
* Trait used by Promise implementations. Do not use this trait in your code, instead compose your class from one of
* the available classes implementing \Amp\Promise.
2016-06-01 19:18:11 +02:00
*
* @internal
*/
2018-06-18 20:00:01 +02:00
trait Placeholder
{
2016-08-18 05:25:54 +02:00
/** @var bool */
2016-05-21 16:44:52 +02:00
private $resolved = false;
2016-08-18 05:25:54 +02:00
/** @var mixed */
2016-05-21 16:44:52 +02:00
private $result;
2016-12-23 23:48:24 +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
/** @var null|array */
private $resolutionTrace;
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
*/
2018-06-18 20:00:01 +02:00
public function onResolve(callable $onResolved)
{
2016-05-21 16:44:52 +02:00
if ($this->resolved) {
2016-11-14 20:59:21 +01:00
if ($this->result instanceof Promise) {
$this->result->onResolve($onResolved);
2016-05-24 04:32:41 +02:00
return;
}
try {
/** @var mixed $result */
$result = $onResolved(null, $this->result);
if ($result === null) {
return;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
Promise\rethrow($result);
}
2016-05-24 04:32:41 +02:00
} catch (\Throwable $exception) {
2017-06-05 07:21:45 +02:00
Loop::defer(static function () use ($exception) {
throw $exception;
});
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;
}
if (!$this->onResolved instanceof ResolutionQueue) {
2020-03-28 20:27:42 +01:00
/** @psalm-suppress InternalClass */
$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
}
public function __destruct()
{
try {
$this->result = null;
} catch (\Throwable $e) {
Loop::defer(static function () use ($e) {
throw $e;
});
}
}
2016-05-21 16:44:52 +02:00
/**
* @param mixed $value
2016-08-11 21:35:58 +02: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
*/
2018-06-18 20:00:01 +02:00
private function resolve($value = null)
{
2016-05-21 16:44:52 +02:00
if ($this->resolved) {
$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
}
throw new \Error($message);
2016-05-21 16:44:52 +02:00
}
\assert((function () {
$env = \getenv("AMP_DEBUG") ?: "0";
2017-12-02 18:15:06 +01:00
if (($env !== "0" && $env !== "false") || (\defined("AMP_DEBUG") && \AMP_DEBUG)) {
2017-12-02 16:54:28 +01:00
$trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
\array_shift($trace); // remove current closure
$this->resolutionTrace = $trace;
}
return true;
})());
2017-02-20 21:53:58 +01:00
if ($value instanceof ReactPromise) {
$value = Promise\adapt($value);
2017-02-20 21:53:58 +01:00
}
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) {
$this->result->onResolve($onResolved);
2016-06-17 06:27:14 +02:00
return;
}
2016-12-23 23:48:24 +01:00
2016-06-17 06:27:14 +02:00
try {
/** @var mixed $result */
$result = $onResolved(null, $this->result);
$onResolved = null; // allow garbage collection of $onResolved, to catch any exceptions from destructors
if ($result === null) {
return;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
Promise\rethrow($result);
}
2016-06-17 06:27:14 +02:00
} catch (\Throwable $exception) {
2017-06-05 07:21:45 +02:00
Loop::defer(static function () use ($exception) {
throw $exception;
});
2016-06-15 04:53:50 +02:00
}
2016-05-21 16:44:52 +02:00
}
/**
2016-08-11 21:35:58 +02:00
* @param \Throwable $reason Failure reason.
*
* @return void
2016-05-21 16:44:52 +02:00
*/
2018-06-18 20:00:01 +02:00
private function fail(\Throwable $reason)
{
2016-05-21 16:44:52 +02:00
$this->resolve(new Failure($reason));
}
}