2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-08-16 06:46:26 +02:00
|
|
|
|
2016-05-24 05:48:28 +02:00
|
|
|
namespace Amp;
|
2016-05-21 16:44:52 +02:00
|
|
|
|
2017-02-20 21:53:58 +01:00
|
|
|
use React\Promise\PromiseInterface as ReactPromise;
|
|
|
|
|
2016-06-01 19:18:11 +02:00
|
|
|
/**
|
2017-01-04 02:10:27 +01:00
|
|
|
* Creates a successful stream (which is also a promise) using the given value (which can be any value except another
|
2017-04-23 15:29:08 +02:00
|
|
|
* object implementing `Amp\Promise`).
|
2016-06-01 19:18:11 +02:00
|
|
|
*/
|
2017-01-04 02:10:27 +01:00
|
|
|
final class Success implements Stream {
|
2016-08-18 05:25:54 +02:00
|
|
|
/** @var mixed */
|
2016-05-21 16:44:52 +02:00
|
|
|
private $value;
|
|
|
|
|
|
|
|
/**
|
2017-03-12 17:05:52 +01:00
|
|
|
* @param mixed $value Anything other than a Promise object.
|
2016-05-21 16:44:52 +02:00
|
|
|
*
|
2016-11-14 20:59:21 +01:00
|
|
|
* @throws \Error If a promise is given as the value.
|
2016-05-21 16:44:52 +02:00
|
|
|
*/
|
2017-03-10 21:58:46 +01:00
|
|
|
public function __construct($value = null) {
|
2017-02-20 21:53:58 +01:00
|
|
|
if ($value instanceof Promise || $value instanceof ReactPromise) {
|
2016-11-14 20:59:21 +01:00
|
|
|
throw new \Error("Cannot use a promise as success value");
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->value = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2017-03-21 17:23:37 +01:00
|
|
|
public function onResolve(callable $onResolved) {
|
2016-05-21 16:44:52 +02:00
|
|
|
try {
|
2017-03-28 01:37:55 +02:00
|
|
|
$result = $onResolved(null, $this->value);
|
|
|
|
|
|
|
|
if ($result === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result instanceof \Generator) {
|
|
|
|
$result = new Coroutine($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
|
|
|
Promise\rethrow($result);
|
|
|
|
}
|
2016-05-21 16:44:52 +02:00
|
|
|
} catch (\Throwable $exception) {
|
2017-03-10 22:32:58 +01:00
|
|
|
Loop::defer(function () use ($exception) {
|
|
|
|
throw $exception;
|
|
|
|
});
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-22 23:21:47 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2017-03-21 18:24:06 +01:00
|
|
|
public function onEmit(callable $onEmit) {
|
2017-03-10 21:58:46 +01:00
|
|
|
}
|
2016-05-22 20:43:37 +02:00
|
|
|
}
|