2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-08-16 06:46:26 +02:00
|
|
|
|
2016-05-27 01:20:05 +02:00
|
|
|
namespace Amp\Internal;
|
|
|
|
|
2017-03-28 01:37:55 +02:00
|
|
|
use Amp\Coroutine;
|
2017-03-10 21:58:46 +01:00
|
|
|
use Amp\Deferred;
|
2017-03-10 22:32:58 +01:00
|
|
|
use Amp\Loop;
|
2017-03-10 21:58:46 +01:00
|
|
|
use Amp\Promise;
|
|
|
|
use Amp\Success;
|
2017-02-20 21:53:58 +01:00
|
|
|
use React\Promise\PromiseInterface as ReactPromise;
|
2016-05-27 01:20:05 +02:00
|
|
|
|
2016-06-01 18:37:12 +02:00
|
|
|
/**
|
2017-01-04 02:10:27 +01:00
|
|
|
* Trait used by Stream implementations. Do not use this trait in your code, instead compose your class from one of
|
|
|
|
* the available classes implementing \Amp\Stream.
|
|
|
|
* Note that it is the responsibility of the user of this trait to ensure that listeners have a chance to listen first
|
2016-08-14 16:57:17 +02:00
|
|
|
* before emitting values.
|
2016-06-01 18:37:12 +02:00
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
2016-05-27 01:20:05 +02:00
|
|
|
trait Producer {
|
2016-05-29 18:35:09 +02:00
|
|
|
use Placeholder {
|
|
|
|
resolve as complete;
|
|
|
|
}
|
|
|
|
|
2016-08-18 05:25:54 +02:00
|
|
|
/** @var callable[] */
|
2017-01-04 02:10:27 +01:00
|
|
|
private $listeners = [];
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-05-27 01:20:05 +02:00
|
|
|
/**
|
2017-03-21 18:24:06 +01:00
|
|
|
* @param callable $onEmit
|
2016-05-27 01:20:05 +02:00
|
|
|
*/
|
2017-03-21 18:24:06 +01:00
|
|
|
public function onEmit(callable $onEmit) {
|
2016-07-19 20:30:28 +02:00
|
|
|
if ($this->resolved) {
|
2016-08-18 04:11:03 +02:00
|
|
|
return;
|
2016-05-29 18:35:09 +02:00
|
|
|
}
|
|
|
|
|
2017-03-21 18:24:06 +01:00
|
|
|
$this->listeners[] = $onEmit;
|
2016-05-27 01:20:05 +02:00
|
|
|
}
|
|
|
|
|
2016-05-29 18:35:09 +02:00
|
|
|
/**
|
2017-01-04 02:10:27 +01:00
|
|
|
* Emits a value from the stream. The returned promise is resolved with the emitted value once all listeners
|
2016-05-29 18:35:09 +02:00
|
|
|
* have been invoked.
|
2016-05-27 22:44:01 +02:00
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
2017-03-10 21:58:46 +01:00
|
|
|
* @return \Amp\Promise
|
2016-05-29 18:35:09 +02:00
|
|
|
*
|
2017-01-04 02:10:27 +01:00
|
|
|
* @throws \Error If the stream has resolved.
|
2016-05-27 01:20:05 +02:00
|
|
|
*/
|
2016-11-14 20:59:21 +01:00
|
|
|
private function emit($value): Promise {
|
2016-05-29 18:35:09 +02:00
|
|
|
if ($this->resolved) {
|
2017-01-07 19:25:04 +01:00
|
|
|
throw new \Error("Streams cannot emit values after calling resolve");
|
2016-05-27 01:20:05 +02:00
|
|
|
}
|
|
|
|
|
2017-02-20 21:53:58 +01:00
|
|
|
if ($value instanceof ReactPromise) {
|
2017-03-28 01:37:55 +02:00
|
|
|
$value = Promise\adapt($value);
|
2017-02-20 21:53:58 +01:00
|
|
|
}
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
if ($value instanceof Promise) {
|
2016-08-16 20:07:38 +02:00
|
|
|
$deferred = new Deferred;
|
2017-03-21 17:23:37 +01:00
|
|
|
$value->onResolve(function ($e, $v) use ($deferred) {
|
2016-08-23 20:17:59 +02:00
|
|
|
if ($this->resolved) {
|
|
|
|
$deferred->fail(
|
2017-01-04 02:10:27 +01:00
|
|
|
new \Error("The stream was resolved before the promise result could be emitted")
|
2016-08-23 20:17:59 +02:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-16 20:07:38 +02:00
|
|
|
if ($e) {
|
2016-08-23 20:17:59 +02:00
|
|
|
$this->fail($e);
|
2016-08-16 20:07:38 +02:00
|
|
|
$deferred->fail($e);
|
|
|
|
return;
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-16 20:07:38 +02:00
|
|
|
$deferred->resolve($this->emit($v));
|
2016-08-16 09:28:47 +02:00
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
return $deferred->promise();
|
2016-08-16 09:28:47 +02:00
|
|
|
}
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$promises = [];
|
2016-08-16 09:28:47 +02:00
|
|
|
|
2017-03-21 18:24:06 +01:00
|
|
|
foreach ($this->listeners as $onEmit) {
|
2016-08-16 09:28:47 +02:00
|
|
|
try {
|
2017-03-21 18:24:06 +01:00
|
|
|
$result = $onEmit($value);
|
2017-03-28 01:37:55 +02:00
|
|
|
if ($result === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($result instanceof \Generator) {
|
|
|
|
$result = new Coroutine($result);
|
|
|
|
} elseif ($result instanceof ReactPromise) {
|
|
|
|
$result = Promise\adapt($result);
|
2017-02-20 21:53:58 +01:00
|
|
|
}
|
2016-11-14 20:59:21 +01:00
|
|
|
if ($result instanceof Promise) {
|
|
|
|
$promises[] = $result;
|
2016-08-16 09:28:47 +02:00
|
|
|
}
|
|
|
|
} catch (\Throwable $e) {
|
2017-03-10 22:32:58 +01:00
|
|
|
Loop::defer(function () use ($e) {
|
|
|
|
throw $e;
|
|
|
|
});
|
2016-08-16 09:28:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
if (!$promises) {
|
2016-08-16 09:28:47 +02:00
|
|
|
return new Success($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$deferred = new Deferred;
|
2016-11-14 20:59:21 +01:00
|
|
|
$count = \count($promises);
|
2016-08-16 20:07:38 +02:00
|
|
|
$f = static function ($e) use ($deferred, $value, &$count) {
|
2016-08-16 09:28:47 +02:00
|
|
|
if ($e) {
|
2017-03-10 22:32:58 +01:00
|
|
|
Loop::defer(function () use ($e) {
|
|
|
|
throw $e;
|
|
|
|
});
|
2016-08-16 09:28:47 +02:00
|
|
|
}
|
|
|
|
if (!--$count) {
|
|
|
|
$deferred->resolve($value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
foreach ($promises as $promise) {
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve($f);
|
2016-08-16 09:28:47 +02:00
|
|
|
}
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
return $deferred->promise();
|
2016-05-27 01:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-01-04 02:10:27 +01:00
|
|
|
* Resolves the stream with the given value.
|
2016-05-27 22:44:01 +02:00
|
|
|
*
|
2016-05-29 18:35:09 +02:00
|
|
|
* @param mixed $value
|
2016-05-27 22:44:01 +02:00
|
|
|
*
|
2017-01-04 02:10:27 +01:00
|
|
|
* @throws \Error If the stream has already been resolved.
|
2016-05-27 01:20:05 +02:00
|
|
|
*/
|
2016-07-19 19:32:43 +02:00
|
|
|
private function resolve($value = null) {
|
2016-07-19 20:19:44 +02:00
|
|
|
$this->complete($value);
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->listeners = [];
|
2016-05-27 01:20:05 +02:00
|
|
|
}
|
|
|
|
}
|