2020-05-13 17:15:21 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Internal;
|
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
use Amp\Pipeline;
|
2020-05-13 17:15:21 +02:00
|
|
|
|
|
|
|
/**
|
2020-08-27 19:45:48 +02:00
|
|
|
* Wraps an EmitSource instance that has public methods to emit, complete, and fail into an object that only allows
|
|
|
|
* access to the public API methods and automatically calls EmitSource::destroy() when the object is destroyed.
|
2020-05-13 17:15:21 +02:00
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* @template-covariant TValue
|
2020-08-23 16:18:28 +02:00
|
|
|
* @template-implements Pipeline<TValue>
|
2020-11-10 20:28:43 +01:00
|
|
|
* @template-implements \IteratorAggregate<int, TValue>
|
2020-05-13 17:15:21 +02:00
|
|
|
*/
|
2020-11-10 19:05:47 +01:00
|
|
|
final class AutoDisposingPipeline implements Pipeline, \IteratorAggregate
|
2020-05-13 17:15:21 +02:00
|
|
|
{
|
2020-05-28 19:59:55 +02:00
|
|
|
/** @var EmitSource<TValue, null> */
|
2020-09-24 18:52:22 +02:00
|
|
|
private EmitSource $source;
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-05-28 19:59:55 +02:00
|
|
|
public function __construct(EmitSource $source)
|
2020-05-13 17:15:21 +02:00
|
|
|
{
|
2020-05-19 17:49:40 +02:00
|
|
|
$this->source = $source;
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
{
|
2020-08-27 19:45:48 +02:00
|
|
|
$this->source->destroy();
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-14 21:14:54 +02:00
|
|
|
* @inheritDoc
|
2020-05-13 17:15:21 +02:00
|
|
|
*/
|
2020-09-25 05:14:58 +02:00
|
|
|
public function continue(): mixed
|
2020-05-13 17:15:21 +02:00
|
|
|
{
|
2020-05-19 17:49:40 +02:00
|
|
|
return $this->source->continue();
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
2020-05-13 21:59:31 +02:00
|
|
|
|
|
|
|
/**
|
2020-05-14 21:14:54 +02:00
|
|
|
* @inheritDoc
|
2020-05-13 21:59:31 +02:00
|
|
|
*/
|
2020-09-24 18:52:22 +02:00
|
|
|
public function dispose(): void
|
2020-05-13 21:59:31 +02:00
|
|
|
{
|
2020-05-19 17:49:40 +02:00
|
|
|
$this->source->dispose();
|
2020-05-13 21:59:31 +02:00
|
|
|
}
|
2020-11-10 19:05:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
2020-11-10 20:28:43 +01:00
|
|
|
*
|
2020-11-11 04:56:00 +01:00
|
|
|
* @psalm-return \Traversable<int, TValue>
|
2020-11-10 19:05:47 +01:00
|
|
|
*/
|
2020-11-11 04:56:00 +01:00
|
|
|
public function getIterator(): \Traversable
|
2020-11-10 19:05:47 +01:00
|
|
|
{
|
2020-11-10 20:28:43 +01:00
|
|
|
while (null !== $value = $this->source->continue()) {
|
2020-11-10 19:05:47 +01:00
|
|
|
yield $value;
|
|
|
|
}
|
|
|
|
}
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|