2016-05-24 18:47:14 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-16 06:46:26 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-05-24 18:47:14 +02:00
|
|
|
namespace Amp;
|
|
|
|
|
2016-08-14 16:57:17 +02:00
|
|
|
use Interop\Async\Loop;
|
|
|
|
|
2016-05-27 01:20:05 +02:00
|
|
|
final class Emitter implements Observable {
|
2016-06-01 18:19:19 +02:00
|
|
|
use Internal\Producer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param callable(callable $emit): \Generator $emitter
|
|
|
|
*/
|
|
|
|
public function __construct(callable $emitter) {
|
|
|
|
$this->init();
|
2016-08-14 16:57:17 +02:00
|
|
|
|
|
|
|
// defer first emit until next tick in order to give *all* subscribers a chance to subscribe first
|
|
|
|
$pending = true;
|
|
|
|
Loop::defer(static function() use (&$pending) {
|
|
|
|
if ($pending instanceof Deferred) {
|
|
|
|
$pending->resolve();
|
|
|
|
}
|
|
|
|
$pending = false;
|
|
|
|
});
|
|
|
|
$emit = function ($value) use (&$pending) {
|
|
|
|
if ($pending) {
|
|
|
|
if ($pending === true) {
|
|
|
|
$pending = new Deferred;
|
|
|
|
}
|
|
|
|
$pending->when(function() use ($value) {
|
|
|
|
$this->emit($value);
|
|
|
|
});
|
|
|
|
return $pending->getAwaitable();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->emit($value);
|
|
|
|
};
|
|
|
|
|
2016-06-01 18:19:19 +02:00
|
|
|
$result = $emitter($emit);
|
|
|
|
|
|
|
|
if (!$result instanceof \Generator) {
|
2016-08-11 21:35:58 +02:00
|
|
|
throw new \Error("The callable did not return a Generator");
|
2016-06-01 18:19:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($result);
|
|
|
|
$coroutine->when(function ($exception, $value) {
|
|
|
|
if ($exception) {
|
|
|
|
$this->fail($exception);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->resolve($value);
|
|
|
|
});
|
2016-05-24 18:47:14 +02:00
|
|
|
}
|
|
|
|
}
|