mirror of
https://github.com/danog/amp.git
synced 2024-11-27 12:35:02 +01:00
39 lines
1015 B
PHP
39 lines
1015 B
PHP
<?php
|
|
|
|
namespace Amp;
|
|
|
|
use AsyncInterop\Loop;
|
|
|
|
final class Producer implements Stream {
|
|
use CallableMaker, Internal\Producer;
|
|
|
|
/**
|
|
* @param callable(callable(mixed $value): Promise $emit): \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");
|
|
}
|
|
|
|
Loop::defer(function () use ($result) {
|
|
$coroutine = new Coroutine($result);
|
|
$coroutine->when(function ($exception, $value) {
|
|
if ($this->resolved) {
|
|
return;
|
|
}
|
|
|
|
if ($exception) {
|
|
$this->fail($exception);
|
|
return;
|
|
}
|
|
|
|
$this->resolve($value);
|
|
});
|
|
});
|
|
}
|
|
}
|