2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-08-16 06:46:26 +02:00
|
|
|
|
2016-05-24 18:47:14 +02:00
|
|
|
namespace Amp;
|
|
|
|
|
2017-12-03 02:07:46 +01:00
|
|
|
/**
|
|
|
|
* Emitter is a container for an iterator that can emit values using the emit() method and completed using the
|
|
|
|
* complete() and fail() methods of this object. The contained iterator may be accessed using the iterate()
|
|
|
|
* method. This object should not be part of a public API, but used internally to create and emit values to an
|
|
|
|
* iterator.
|
2020-03-28 12:23:46 +01:00
|
|
|
*
|
|
|
|
* @template TValue
|
2020-05-13 17:15:21 +02:00
|
|
|
*
|
2020-08-23 16:18:28 +02:00
|
|
|
* @deprecated Use {@see PipelineSource} and {@see Pipeline} instead of {@see Emitter} and {@see Iterator}.
|
2017-12-03 02:07:46 +01:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
final class Emitter
|
|
|
|
{
|
2020-09-27 06:14:17 +02:00
|
|
|
private Internal\Producer $emitter;
|
2017-12-20 17:30:43 +01:00
|
|
|
|
2020-09-24 18:52:22 +02:00
|
|
|
private Internal\PrivateIterator $iterator;
|
2017-12-03 02:07:46 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
2020-09-27 06:14:17 +02:00
|
|
|
$this->emitter = new Internal\Producer;
|
2017-12-20 17:30:43 +01:00
|
|
|
|
2018-01-13 17:36:01 +01:00
|
|
|
$this->iterator = new Internal\PrivateIterator($this->emitter);
|
2017-12-03 02:07:46 +01:00
|
|
|
}
|
2017-01-04 02:10:27 +01:00
|
|
|
|
2017-12-03 02:07:46 +01:00
|
|
|
/**
|
2020-03-28 12:23:46 +01:00
|
|
|
* @return Iterator
|
|
|
|
* @psalm-return Iterator<TValue>
|
2017-12-03 02:07:46 +01:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function iterate(): Iterator
|
|
|
|
{
|
2017-12-03 02:07:46 +01:00
|
|
|
return $this->iterator;
|
|
|
|
}
|
2017-01-04 02:10:27 +01:00
|
|
|
|
2017-12-03 02:07:46 +01:00
|
|
|
/**
|
|
|
|
* Emits a value to the iterator.
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
2020-03-28 12:23:46 +01:00
|
|
|
* @psalm-param TValue $value
|
|
|
|
*
|
|
|
|
* @return Promise
|
|
|
|
* @psalm-return Promise<null>
|
|
|
|
* @psalm-suppress MixedInferredReturnType
|
|
|
|
* @psalm-suppress MixedReturnStatement
|
2017-12-03 02:07:46 +01:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function emit($value): Promise
|
|
|
|
{
|
2020-03-28 12:23:46 +01:00
|
|
|
/** @psalm-suppress UndefinedInterfaceMethod */
|
2017-12-20 17:30:43 +01:00
|
|
|
return $this->emitter->emit($value);
|
2017-12-03 02:07:46 +01:00
|
|
|
}
|
2017-01-04 02:10:27 +01:00
|
|
|
|
2017-12-03 02:07:46 +01:00
|
|
|
/**
|
|
|
|
* Completes the iterator.
|
2020-03-28 12:23:46 +01:00
|
|
|
*
|
|
|
|
* @return void
|
2017-12-03 02:07:46 +01:00
|
|
|
*/
|
2020-09-25 05:17:13 +02:00
|
|
|
public function complete(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-03-28 12:23:46 +01:00
|
|
|
/** @psalm-suppress UndefinedInterfaceMethod */
|
2017-12-20 17:30:43 +01:00
|
|
|
$this->emitter->complete();
|
2017-12-03 02:07:46 +01:00
|
|
|
}
|
2016-06-01 18:19:19 +02:00
|
|
|
|
2017-12-03 02:07:46 +01:00
|
|
|
/**
|
|
|
|
* Fails the iterator with the given reason.
|
|
|
|
*
|
|
|
|
* @param \Throwable $reason
|
2020-03-28 12:23:46 +01:00
|
|
|
*
|
|
|
|
* @return void
|
2017-12-03 02:07:46 +01:00
|
|
|
*/
|
2020-09-25 05:17:13 +02:00
|
|
|
public function fail(\Throwable $reason): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-03-28 12:23:46 +01:00
|
|
|
/** @psalm-suppress UndefinedInterfaceMethod */
|
2017-12-20 17:30:43 +01:00
|
|
|
$this->emitter->fail($reason);
|
2016-05-24 18:47:14 +02:00
|
|
|
}
|
2017-12-03 02:07:46 +01:00
|
|
|
}
|