1
0
mirror of https://github.com/danog/amp.git synced 2024-12-11 17:09:40 +01:00
amp/lib/Producer.php

48 lines
1.1 KiB
PHP
Raw Normal View History

2017-01-04 02:10:27 +01:00
<?php
namespace Amp;
2020-03-28 14:25:39 +01:00
/**
* @template-covariant TValue
* @template-implements Iterator<TValue>
2020-05-13 17:15:21 +02:00
*
* @deprecated Use {@see AsyncGenerator} instead.
*
* @psalm-suppress DeprecatedInterface
2020-03-28 14:25:39 +01:00
*/
2018-06-18 20:00:01 +02:00
final class Producer implements Iterator
{
2020-03-28 14:25:39 +01:00
/**
* @use Internal\Producer<TValue>
*/
2020-09-27 05:27:47 +02:00
use Internal\Producer;
2017-01-04 02:10:27 +01:00
/**
* @param callable(callable(TValue):Promise):\Generator $producer
2017-01-04 02:10:27 +01:00
*
* @throws \Error Thrown if the callable does not return a Generator.
*/
2018-06-18 20:00:01 +02:00
public function __construct(callable $producer)
{
2020-09-27 05:27:47 +02:00
$result = $producer(\Closure::fromCallable([$this, 'emit']));
2017-01-04 02:10:27 +01:00
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();
2017-01-04 02:10:27 +01:00
});
}
}