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

60 lines
1.2 KiB
PHP
Raw Normal View History

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
/**
* 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>
* @template-implements \IteratorAggregate<int, TValue>
2020-05-13 17:15:21 +02: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
{
$this->source = $source;
2020-05-13 17:15:21 +02:00
}
public function __destruct()
{
$this->source->destroy();
2020-05-13 17:15:21 +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
{
return $this->source->continue();
2020-05-13 17:15:21 +02:00
}
/**
* @inheritDoc
*/
2020-09-24 18:52:22 +02:00
public function dispose(): void
{
$this->source->dispose();
}
/**
* @inheritDoc
*
* @psalm-return \Traversable<int, TValue>
*/
public function getIterator(): \Traversable
{
while (null !== $value = $this->source->continue()) {
yield $value;
}
}
2020-05-13 17:15:21 +02:00
}