2016-12-29 14:09:49 -06:00
|
|
|
<?php
|
2016-08-15 23:46:26 -05:00
|
|
|
|
2016-05-23 22:48:28 -05:00
|
|
|
namespace Amp;
|
2016-05-21 09:44:52 -05:00
|
|
|
|
2016-12-29 14:09:49 -06:00
|
|
|
use Interop\Async\{ Promise, Promise\ErrorHandler };
|
2016-05-21 09:44:52 -05:00
|
|
|
|
2016-06-01 12:18:11 -05:00
|
|
|
/**
|
2016-08-22 16:21:47 -05:00
|
|
|
* Creates a successful observable using the given value (which can be any value except another object implementing
|
2016-11-14 13:59:21 -06:00
|
|
|
* \Interop\Async\Promise).
|
2016-06-01 12:18:11 -05:00
|
|
|
*/
|
2016-08-22 16:21:47 -05:00
|
|
|
final class Success implements Observable {
|
2016-08-17 22:25:54 -05:00
|
|
|
/** @var mixed */
|
2016-05-21 09:44:52 -05:00
|
|
|
private $value;
|
|
|
|
|
|
|
|
/**
|
2016-11-14 13:59:21 -06:00
|
|
|
* @param mixed $value Anything other than an Promise object.
|
2016-05-21 09:44:52 -05:00
|
|
|
*
|
2016-11-14 13:59:21 -06:00
|
|
|
* @throws \Error If a promise is given as the value.
|
2016-05-21 09:44:52 -05:00
|
|
|
*/
|
2016-05-29 10:40:00 -05:00
|
|
|
public function __construct($value = null)
|
2016-05-21 09:44:52 -05:00
|
|
|
{
|
2016-11-14 13:59:21 -06:00
|
|
|
if ($value instanceof Promise) {
|
|
|
|
throw new \Error("Cannot use a promise as success value");
|
2016-05-21 09:44:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->value = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function when(callable $onResolved) {
|
|
|
|
try {
|
|
|
|
$onResolved(null, $this->value);
|
|
|
|
} catch (\Throwable $exception) {
|
2016-12-29 14:09:49 -06:00
|
|
|
ErrorHandler::notify($exception);
|
2016-05-21 09:44:52 -05:00
|
|
|
}
|
|
|
|
}
|
2016-08-22 16:21:47 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function subscribe(callable $onNext) {}
|
2016-05-22 13:43:37 -05:00
|
|
|
}
|