1
0
mirror of https://github.com/danog/amp.git synced 2024-12-03 09:57:51 +01:00
amp/lib/Producer.php
Aaron Piotrowski 96007f11aa
Add Streams
2020-09-24 12:53:27 -05:00

48 lines
1.1 KiB
PHP

<?php
namespace Amp;
/**
* @template-covariant TValue
* @template-implements Iterator<TValue>
*
* @deprecated Use {@see AsyncGenerator} instead.
*
* @psalm-suppress DeprecatedInterface
*/
final class Producer implements Iterator
{
/**
* @use Internal\Producer<TValue>
*/
use CallableMaker, Internal\Producer;
/**
* @param callable(callable(TValue):Promise):\Generator $producer
*
* @throws \Error Thrown if the callable does not return a Generator.
*/
public function __construct(callable $producer)
{
$result = $producer($this->callableFromInstanceMethod("emit"));
if (!$result instanceof \Generator) {
throw new \Error("The callable did not return a Generator");
}
$coroutine = new Coroutine($result);
$coroutine->onResolve(function ($exception) {
if ($this->complete) {
return;
}
if ($exception) {
$this->fail($exception);
return;
}
$this->complete();
});
}
}