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

37 lines
885 B
PHP
Raw Normal View History

2017-01-04 02:10:27 +01:00
<?php
namespace Amp;
2018-06-18 20:00:01 +02:00
final class Producer implements Iterator
{
2017-01-04 02:10:27 +01:00
use CallableMaker, Internal\Producer;
/**
* @param callable(callable(mixed $value): Promise $emit): \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)
{
2017-01-04 02:10:27 +01:00
$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();
2017-01-04 02:10:27 +01:00
});
}
}