2016-08-17 22:25:54 -05:00
|
|
|
<?php declare(strict_types = 1);
|
2016-08-15 23:46:26 -05:00
|
|
|
|
2016-05-26 18:20:05 -05:00
|
|
|
namespace Amp\Internal;
|
|
|
|
|
2016-08-17 21:11:03 -05:00
|
|
|
use Amp\{ Deferred, Observable, Success };
|
2016-08-14 16:57:17 +02:00
|
|
|
use Interop\Async\{ Awaitable, Loop };
|
2016-05-26 18:20:05 -05:00
|
|
|
|
2016-06-01 11:37:12 -05:00
|
|
|
/**
|
|
|
|
* Trait used by Observable implementations. Do not use this trait in your code, instead compose your class from one of
|
|
|
|
* the available classes implementing \Amp\Observable.
|
2016-08-14 16:57:17 +02:00
|
|
|
* Note that it is the responsibility of the user of this trait to ensure that subscribers have a chance to subscribe first
|
|
|
|
* before emitting values.
|
2016-06-01 11:37:12 -05:00
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
2016-05-26 18:20:05 -05:00
|
|
|
trait Producer {
|
2016-05-29 11:35:09 -05:00
|
|
|
use Placeholder {
|
|
|
|
resolve as complete;
|
|
|
|
}
|
|
|
|
|
2016-08-17 22:25:54 -05:00
|
|
|
/** @var callable[] */
|
2016-05-29 11:35:09 -05:00
|
|
|
private $subscribers = [];
|
2016-08-16 13:30:32 -05:00
|
|
|
|
2016-05-26 18:20:05 -05:00
|
|
|
/**
|
2016-05-29 11:35:09 -05:00
|
|
|
* @param callable $onNext
|
2016-05-26 18:20:05 -05:00
|
|
|
*/
|
2016-08-17 21:11:03 -05:00
|
|
|
public function subscribe(callable $onNext) {
|
2016-07-19 13:30:28 -05:00
|
|
|
if ($this->resolved) {
|
2016-08-17 21:11:03 -05:00
|
|
|
return;
|
2016-05-29 11:35:09 -05:00
|
|
|
}
|
|
|
|
|
2016-08-17 21:11:03 -05:00
|
|
|
$this->subscribers[] = $onNext;
|
2016-05-26 18:20:05 -05:00
|
|
|
}
|
|
|
|
|
2016-05-29 11:35:09 -05:00
|
|
|
/**
|
|
|
|
* Emits a value from the observable. The returned awaitable is resolved with the emitted value once all subscribers
|
|
|
|
* have been invoked.
|
2016-05-27 15:44:01 -05:00
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
|
|
|
* @return \Interop\Async\Awaitable
|
2016-05-29 11:35:09 -05:00
|
|
|
*
|
2016-08-11 14:35:58 -05:00
|
|
|
* @throws \Error If the observable has resolved.
|
2016-05-26 18:20:05 -05:00
|
|
|
*/
|
2016-08-11 14:35:58 -05:00
|
|
|
private function emit($value): Awaitable {
|
2016-05-29 11:35:09 -05:00
|
|
|
if ($this->resolved) {
|
2016-08-11 14:35:58 -05:00
|
|
|
throw new \Error("The observable has been resolved; cannot emit more values");
|
2016-05-26 18:20:05 -05:00
|
|
|
}
|
|
|
|
|
2016-08-16 02:28:47 -05:00
|
|
|
if ($value instanceof Awaitable) {
|
2016-08-16 13:07:38 -05:00
|
|
|
$deferred = new Deferred;
|
|
|
|
$value->when(function ($e, $v) use ($deferred) {
|
2016-08-23 13:17:59 -05:00
|
|
|
if ($this->resolved) {
|
|
|
|
$deferred->fail(
|
|
|
|
new \Error("The observable was resolved before the awaitable result could be emitted")
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-16 13:07:38 -05:00
|
|
|
if ($e) {
|
2016-08-23 13:17:59 -05:00
|
|
|
$this->fail($e);
|
2016-08-16 13:07:38 -05:00
|
|
|
$deferred->fail($e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$deferred->resolve($this->emit($v));
|
2016-08-16 02:28:47 -05:00
|
|
|
});
|
2016-08-16 13:07:38 -05:00
|
|
|
|
|
|
|
return $deferred->getAwaitable();
|
2016-08-16 02:28:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$awaitables = [];
|
|
|
|
|
|
|
|
foreach ($this->subscribers as $onNext) {
|
|
|
|
try {
|
|
|
|
$result = $onNext($value);
|
|
|
|
if ($result instanceof Awaitable) {
|
|
|
|
$awaitables[] = $result;
|
|
|
|
}
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
Loop::defer(static function () use ($e) {
|
|
|
|
throw $e;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$awaitables) {
|
|
|
|
return new Success($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$deferred = new Deferred;
|
|
|
|
$count = \count($awaitables);
|
2016-08-16 13:07:38 -05:00
|
|
|
$f = static function ($e) use ($deferred, $value, &$count) {
|
2016-08-16 02:28:47 -05:00
|
|
|
if ($e) {
|
|
|
|
Loop::defer(static function () use ($e) {
|
|
|
|
throw $e;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (!--$count) {
|
|
|
|
$deferred->resolve($value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
foreach ($awaitables as $awaitable) {
|
|
|
|
$awaitable->when($f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $deferred->getAwaitable();
|
2016-05-26 18:20:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-29 11:35:09 -05:00
|
|
|
* Resolves the observable with the given value.
|
2016-05-27 15:44:01 -05:00
|
|
|
*
|
2016-05-29 11:35:09 -05:00
|
|
|
* @param mixed $value
|
2016-05-27 15:44:01 -05:00
|
|
|
*
|
2016-08-11 14:35:58 -05:00
|
|
|
* @throws \Error If the observable has already been resolved.
|
2016-05-26 18:20:05 -05:00
|
|
|
*/
|
2016-07-19 12:32:43 -05:00
|
|
|
private function resolve($value = null) {
|
2016-07-19 13:19:44 -05:00
|
|
|
$this->complete($value);
|
2016-07-19 12:36:09 -05:00
|
|
|
$this->subscribers = [];
|
2016-05-26 18:20:05 -05:00
|
|
|
}
|
|
|
|
}
|