2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-08-16 06:46:26 +02:00
|
|
|
|
2016-05-27 01:20:05 +02:00
|
|
|
namespace Amp\Internal;
|
|
|
|
|
2017-03-10 21:58:46 +01:00
|
|
|
use Amp\Deferred;
|
2017-04-26 19:20:30 +02:00
|
|
|
use Amp\Failure;
|
2017-03-10 21:58:46 +01:00
|
|
|
use Amp\Promise;
|
|
|
|
use Amp\Success;
|
2017-02-20 21:53:58 +01:00
|
|
|
use React\Promise\PromiseInterface as ReactPromise;
|
2016-05-27 01:20:05 +02:00
|
|
|
|
2016-06-01 18:37:12 +02:00
|
|
|
/**
|
2017-05-01 07:32:56 +02:00
|
|
|
* Trait used by Iterator implementations. Do not use this trait in your code, instead compose your class from one of
|
|
|
|
* the available classes implementing \Amp\Iterator.
|
2017-01-04 02:10:27 +01:00
|
|
|
* Note that it is the responsibility of the user of this trait to ensure that listeners have a chance to listen first
|
2016-08-14 16:57:17 +02:00
|
|
|
* before emitting values.
|
2016-06-01 18:37:12 +02:00
|
|
|
*
|
|
|
|
* @internal
|
2020-03-28 14:25:39 +01:00
|
|
|
* @template-covariant TValue
|
2016-06-01 18:37:12 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
trait Producer
|
|
|
|
{
|
2020-09-24 18:52:22 +02:00
|
|
|
private ?Promise $complete;
|
2017-04-26 19:20:30 +02:00
|
|
|
|
2020-09-24 18:52:22 +02:00
|
|
|
private array $values = [];
|
2017-04-26 19:20:30 +02:00
|
|
|
|
2020-09-24 18:52:22 +02:00
|
|
|
private array $backPressure = [];
|
2017-04-26 19:20:30 +02:00
|
|
|
|
2020-09-24 18:52:22 +02:00
|
|
|
private int $consumePosition = -1;
|
2019-09-21 06:39:28 +02:00
|
|
|
|
2020-09-24 18:52:22 +02:00
|
|
|
private int $emitPosition = -1;
|
2017-04-26 19:20:30 +02:00
|
|
|
|
2020-09-24 18:52:22 +02:00
|
|
|
private ?Deferred $waiting;
|
2016-05-29 18:35:09 +02:00
|
|
|
|
2020-09-24 18:52:22 +02:00
|
|
|
private ?array $resolutionTrace;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-05-27 01:20:05 +02:00
|
|
|
/**
|
2017-04-26 19:20:30 +02:00
|
|
|
* {@inheritdoc}
|
2020-03-28 14:25:39 +01:00
|
|
|
*
|
|
|
|
* @return Promise<bool>
|
2016-05-27 01:20:05 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function advance(): Promise
|
|
|
|
{
|
2017-04-26 19:20:30 +02:00
|
|
|
if ($this->waiting !== null) {
|
|
|
|
throw new \Error("The prior promise returned must resolve before invoking this method again");
|
|
|
|
}
|
|
|
|
|
2019-09-21 06:39:28 +02:00
|
|
|
unset($this->values[$this->consumePosition]);
|
2017-04-26 19:20:30 +02:00
|
|
|
|
2019-09-21 06:39:28 +02:00
|
|
|
$position = ++$this->consumePosition;
|
2017-04-26 19:20:30 +02:00
|
|
|
|
2019-09-21 06:39:28 +02:00
|
|
|
if (\array_key_exists($position, $this->values)) {
|
|
|
|
\assert(isset($this->backPressure[$position]));
|
|
|
|
$deferred = $this->backPressure[$position];
|
|
|
|
unset($this->backPressure[$position]);
|
|
|
|
$deferred->resolve();
|
2019-09-20 22:41:20 +02:00
|
|
|
|
2017-04-26 19:20:30 +02:00
|
|
|
return new Success(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->complete) {
|
|
|
|
return $this->complete;
|
2016-05-29 18:35:09 +02:00
|
|
|
}
|
|
|
|
|
2017-04-26 19:20:30 +02:00
|
|
|
$this->waiting = new Deferred;
|
2019-09-20 22:41:20 +02:00
|
|
|
|
2017-04-26 19:20:30 +02:00
|
|
|
return $this->waiting->promise();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2020-03-28 14:25:39 +01:00
|
|
|
*
|
2020-03-28 14:32:53 +01:00
|
|
|
* @return TValue
|
2017-04-26 19:20:30 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function getCurrent()
|
|
|
|
{
|
2017-04-26 19:20:30 +02:00
|
|
|
if (empty($this->values) && $this->complete) {
|
2017-04-28 07:51:59 +02:00
|
|
|
throw new \Error("The iterator has completed");
|
2017-04-26 19:20:30 +02:00
|
|
|
}
|
|
|
|
|
2019-09-21 06:39:28 +02:00
|
|
|
if (!\array_key_exists($this->consumePosition, $this->values)) {
|
2017-04-26 19:20:30 +02:00
|
|
|
throw new \Error("Promise returned from advance() must resolve before calling this method");
|
|
|
|
}
|
|
|
|
|
2019-09-21 06:39:28 +02:00
|
|
|
return $this->values[$this->consumePosition];
|
2016-05-27 01:20:05 +02:00
|
|
|
}
|
|
|
|
|
2016-05-29 18:35:09 +02:00
|
|
|
/**
|
2019-09-21 06:39:28 +02:00
|
|
|
* Emits a value from the iterator. The returned promise is resolved once the emitted value has been consumed.
|
2016-05-27 22:44:01 +02:00
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
2020-03-28 12:23:46 +01:00
|
|
|
* @return Promise
|
|
|
|
* @psalm-return Promise<null>
|
2016-05-29 18:35:09 +02:00
|
|
|
*
|
2017-04-28 07:51:59 +02:00
|
|
|
* @throws \Error If the iterator has completed.
|
2016-05-27 01:20:05 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
private function emit($value): Promise
|
|
|
|
{
|
2017-04-26 19:20:30 +02:00
|
|
|
if ($this->complete) {
|
2017-04-28 07:51:59 +02:00
|
|
|
throw new \Error("Iterators cannot emit values after calling complete");
|
2016-05-27 01:20:05 +02:00
|
|
|
}
|
|
|
|
|
2017-02-20 21:53:58 +01:00
|
|
|
if ($value instanceof ReactPromise) {
|
2017-03-28 01:37:55 +02:00
|
|
|
$value = Promise\adapt($value);
|
2017-02-20 21:53:58 +01:00
|
|
|
}
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
if ($value instanceof Promise) {
|
2016-08-16 20:07:38 +02:00
|
|
|
$deferred = new Deferred;
|
2017-03-21 17:23:37 +01:00
|
|
|
$value->onResolve(function ($e, $v) use ($deferred) {
|
2017-04-26 19:20:30 +02:00
|
|
|
if ($this->complete) {
|
2016-08-23 20:17:59 +02:00
|
|
|
$deferred->fail(
|
2017-04-28 07:51:59 +02:00
|
|
|
new \Error("The iterator was completed before the promise result could be emitted")
|
2016-08-23 20:17:59 +02:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-16 20:07:38 +02:00
|
|
|
if ($e) {
|
2016-08-23 20:17:59 +02:00
|
|
|
$this->fail($e);
|
2016-08-16 20:07:38 +02:00
|
|
|
$deferred->fail($e);
|
|
|
|
return;
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-16 20:07:38 +02:00
|
|
|
$deferred->resolve($this->emit($v));
|
2016-08-16 09:28:47 +02:00
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
return $deferred->promise();
|
2016-08-16 09:28:47 +02:00
|
|
|
}
|
|
|
|
|
2019-09-21 06:39:28 +02:00
|
|
|
$position = ++$this->emitPosition;
|
|
|
|
|
|
|
|
$this->values[$position] = $value;
|
2016-08-16 09:28:47 +02:00
|
|
|
|
2017-04-26 19:20:30 +02:00
|
|
|
if ($this->waiting !== null) {
|
|
|
|
$waiting = $this->waiting;
|
|
|
|
$this->waiting = null;
|
|
|
|
$waiting->resolve(true);
|
2019-09-21 06:39:28 +02:00
|
|
|
return new Success; // Consumer was already waiting for a new value, so back-pressure is unnecessary.
|
2016-08-16 09:28:47 +02:00
|
|
|
}
|
|
|
|
|
2019-09-21 06:39:28 +02:00
|
|
|
$this->backPressure[$position] = $pressure = new Deferred;
|
|
|
|
|
2017-04-26 19:20:30 +02:00
|
|
|
return $pressure->promise();
|
2016-05-27 01:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-28 07:47:03 +02:00
|
|
|
* Completes the iterator.
|
2016-05-27 22:44:01 +02:00
|
|
|
*
|
2020-03-28 12:23:46 +01:00
|
|
|
* @return void
|
|
|
|
*
|
2017-04-28 07:51:59 +02:00
|
|
|
* @throws \Error If the iterator has already been completed.
|
2016-05-27 01:20:05 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
private function complete()
|
|
|
|
{
|
2017-04-26 19:20:30 +02:00
|
|
|
if ($this->complete) {
|
2017-04-28 07:47:03 +02:00
|
|
|
$message = "Iterator has already been completed";
|
|
|
|
|
|
|
|
if (isset($this->resolutionTrace)) {
|
|
|
|
$trace = formatStacktrace($this->resolutionTrace);
|
|
|
|
$message .= ". Previous completion 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-04-28 07:47:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new \Error($message);
|
2017-04-26 19:20:30 +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
|
|
|
|
$this->resolutionTrace = $trace;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
})());
|
|
|
|
|
2017-04-26 19:20:30 +02:00
|
|
|
$this->complete = new Success(false);
|
|
|
|
|
|
|
|
if ($this->waiting !== null) {
|
|
|
|
$waiting = $this->waiting;
|
|
|
|
$this->waiting = null;
|
|
|
|
$waiting->resolve($this->complete);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 12:23:46 +01:00
|
|
|
/**
|
|
|
|
* @param \Throwable $exception
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
private function fail(\Throwable $exception)
|
|
|
|
{
|
2017-04-26 19:20:30 +02:00
|
|
|
$this->complete = new Failure($exception);
|
|
|
|
|
|
|
|
if ($this->waiting !== null) {
|
|
|
|
$waiting = $this->waiting;
|
|
|
|
$this->waiting = null;
|
|
|
|
$waiting->resolve($this->complete);
|
|
|
|
}
|
2016-05-27 01:20:05 +02:00
|
|
|
}
|
|
|
|
}
|