1
0
mirror of https://github.com/danog/amp.git synced 2024-12-13 18:07:30 +01:00
amp/lib/Emitter.php

87 lines
2.1 KiB
PHP
Raw Normal View History

<?php
2016-08-16 06:46:26 +02:00
2016-05-24 18:47:14 +02:00
namespace Amp;
/**
* 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.
*
* @template TValue
2020-05-13 17:15:21 +02:00
*
* @deprecated Use {@see StreamSource} and {@see Stream} instead of {@see Emitter} and {@see Iterator}.
*/
2018-06-18 20:00:01 +02:00
final class Emitter
{
/** @var Iterator<TValue> Has public emit, complete, and fail methods. */
2017-12-20 17:30:43 +01:00
private $emitter;
/** @var Iterator<TValue> Hides producer methods. */
private $iterator;
2018-06-18 20:00:01 +02:00
public function __construct()
{
$this->emitter = new class implements Iterator {
use Internal\Producer {
emit as public;
complete as public;
fail as public;
2017-01-04 02:10:27 +01:00
}
};
2017-12-20 17:30:43 +01:00
$this->iterator = new Internal\PrivateIterator($this->emitter);
}
2017-01-04 02:10:27 +01:00
/**
* @return Iterator
* @psalm-return Iterator<TValue>
*/
2018-06-18 20:00:01 +02:00
public function iterate(): Iterator
{
return $this->iterator;
}
2017-01-04 02:10:27 +01:00
/**
* Emits a value to the iterator.
*
* @param mixed $value
*
* @psalm-param TValue $value
*
* @return Promise
* @psalm-return Promise<null>
* @psalm-suppress MixedInferredReturnType
* @psalm-suppress MixedReturnStatement
*/
2018-06-18 20:00:01 +02:00
public function emit($value): Promise
{
/** @psalm-suppress UndefinedInterfaceMethod */
2017-12-20 17:30:43 +01:00
return $this->emitter->emit($value);
}
2017-01-04 02:10:27 +01:00
/**
* Completes the iterator.
*
* @return void
*/
2018-06-18 20:00:01 +02:00
public function complete()
{
/** @psalm-suppress UndefinedInterfaceMethod */
2017-12-20 17:30:43 +01:00
$this->emitter->complete();
}
/**
* Fails the iterator with the given reason.
*
* @param \Throwable $reason
*
* @return void
*/
2018-06-18 20:00:01 +02:00
public function fail(\Throwable $reason)
{
/** @psalm-suppress UndefinedInterfaceMethod */
2017-12-20 17:30:43 +01:00
$this->emitter->fail($reason);
2016-05-24 18:47:14 +02:00
}
}