1
0
mirror of https://github.com/danog/amp.git synced 2024-12-02 17:37:50 +01:00
amp/lib/Producer.php
Niklas Keller 603ce25299 Fix invalid types in doc comments
We're on the way to psalm error level 4, but there are still some remaining issues.
2020-03-28 14:32:53 +01:00

44 lines
1004 B
PHP

<?php
namespace Amp;
/**
* @template-covariant TValue
* @template-implements Iterator<TValue>
*/
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();
});
}
}