1
0
mirror of https://github.com/danog/amp.git synced 2024-12-11 08:59:46 +01:00
amp/lib/PipelineSource.php

114 lines
2.9 KiB
PHP
Raw Normal View History

2020-05-13 17:15:21 +02:00
<?php
namespace Amp;
/**
2020-08-23 16:18:28 +02:00
* PipelineSource is a container for a Pipeline that can emit values using the emit() method and completed using the
* complete() and fail() methods. The contained Pipeline may be accessed using the pipeline() method. This object should
* not be returned as part of a public API, but used internally to create and emit values to a Pipeline.
2020-05-13 17:15:21 +02:00
*
* @template TValue
*/
2020-08-23 16:18:28 +02:00
final class PipelineSource
2020-05-13 17:15:21 +02:00
{
2020-05-28 19:59:55 +02:00
/** @var Internal\EmitSource<TValue, null> Has public emit, complete, and fail methods. */
2020-09-25 05:14:58 +02:00
private Internal\EmitSource $source;
2020-05-13 17:15:21 +02:00
public function __construct()
{
2020-05-28 19:59:55 +02:00
$this->source = new Internal\EmitSource;
2020-05-13 17:15:21 +02:00
}
/**
2020-08-23 16:18:28 +02:00
* Returns a Pipeline that can be given to an API consumer. This method may be called only once!
2020-05-13 17:15:21 +02:00
*
2020-08-23 16:18:28 +02:00
* @return Pipeline
2020-05-13 17:15:21 +02:00
*
2020-08-23 16:18:28 +02:00
* @psalm-return Pipeline<TValue>
*
* @throws \Error If this method is called more than once.
2020-05-13 17:15:21 +02:00
*/
2020-08-23 16:18:28 +02:00
public function pipe(): Pipeline
2020-05-13 17:15:21 +02:00
{
2020-08-23 16:18:28 +02:00
return $this->source->pipe();
2020-05-13 17:15:21 +02:00
}
/**
* Emits a value to the pipeline, returning a promise that is resolved once the emitted value is consumed.
* Use {@see yield()} to wait until the value is consumed or use {@see await()} on the promise returned
* to wait at a later time.
2020-05-13 17:15:21 +02:00
*
* @param mixed $value
*
* @psalm-param TValue $value
*
2020-05-28 19:59:55 +02:00
* @return Promise<null> Resolves with null when the emitted value has been consumed or fails with
* {@see DisposedException} if the pipeline has been disposed.
2020-05-13 17:15:21 +02:00
*/
public function emit(mixed $value): Promise
2020-05-13 17:15:21 +02:00
{
2020-05-28 19:59:55 +02:00
return $this->source->emit($value);
2020-05-13 17:15:21 +02:00
}
/**
* Emits a value to the pipeline and does not return until the emitted value is consumed.
* Use {@see emit()} to emit a value without waiting for the value to be consumed.
*
* @param mixed $value
*
* @throws DisposedException Thrown if the pipeline is disposed.
*/
public function yield(mixed $value): void
{
await($this->source->emit($value));
}
/**
2020-08-23 16:18:28 +02:00
* @return bool True if the pipeline has been completed or failed.
*/
public function isComplete(): bool
{
return $this->source->isComplete();
}
2020-07-17 18:22:13 +02:00
/**
2020-08-23 16:18:28 +02:00
* @return bool True if the pipeline has been disposed.
2020-07-17 18:22:13 +02:00
*/
public function isDisposed(): bool
{
return $this->source->isDisposed();
}
2020-07-29 17:29:57 +02:00
/**
* @param callable():void $onDisposal
*
* @return void
*/
2020-09-24 18:52:22 +02:00
public function onDisposal(callable $onDisposal): void
2020-07-29 17:29:57 +02:00
{
$this->source->onDisposal($onDisposal);
}
2020-05-13 17:15:21 +02:00
/**
2020-08-23 16:18:28 +02:00
* Completes the pipeline.
2020-05-13 17:15:21 +02:00
*
* @return void
*/
2020-09-24 18:52:22 +02:00
public function complete(): void
2020-05-13 17:15:21 +02:00
{
$this->source->complete();
2020-05-13 17:15:21 +02:00
}
/**
2020-08-23 16:18:28 +02:00
* Fails the pipeline with the given reason.
2020-05-13 17:15:21 +02:00
*
* @param \Throwable $reason
*
* @return void
*/
2020-09-24 18:52:22 +02:00
public function fail(\Throwable $reason): void
2020-05-13 17:15:21 +02:00
{
$this->source->fail($reason);
2020-05-13 17:15:21 +02:00
}
}