1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 21:31:18 +01:00
amp/lib/Success.php
Aaron Piotrowski 5651240615 Update to promise spec v0.3
Dropped strict-types due to spec requiring weak types in callbacks.
2016-12-29 16:29:27 -06:00

45 lines
1021 B
PHP

<?php
namespace Amp;
use Interop\Async\{ Promise, Promise\ErrorHandler };
/**
* Creates a successful observable using the given value (which can be any value except another object implementing
* \Interop\Async\Promise).
*/
final class Success implements Observable {
/** @var mixed */
private $value;
/**
* @param mixed $value Anything other than an Promise object.
*
* @throws \Error If a promise is given as the value.
*/
public function __construct($value = null)
{
if ($value instanceof Promise) {
throw new \Error("Cannot use a promise as success value");
}
$this->value = $value;
}
/**
* {@inheritdoc}
*/
public function when(callable $onResolved) {
try {
$onResolved(null, $this->value);
} catch (\Throwable $exception) {
ErrorHandler::notify($exception);
}
}
/**
* {@inheritdoc}
*/
public function subscribe(callable $onNext) {}
}