1
0
mirror of https://github.com/danog/amp.git synced 2024-11-27 04:24:42 +01:00
amp/lib/Success.php

45 lines
1.0 KiB
PHP
Raw Normal View History

<?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
use AsyncInterop\{ Promise, Promise\ErrorHandler };
2016-05-21 16:44:52 +02:00
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
* object implementing \AsyncInterop\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;
/**
2016-11-14 20:59:21 +01:00
* @param mixed $value Anything other than an 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
*/
2016-05-29 17:40:00 +02:00
public function __construct($value = null)
2016-05-21 16:44:52 +02:00
{
2016-11-14 20:59:21 +01:00
if ($value instanceof Promise) {
throw new \Error("Cannot use a promise as success value");
2016-05-21 16:44:52 +02:00
}
$this->value = $value;
}
/**
* {@inheritdoc}
*/
public function when(callable $onResolved) {
try {
$onResolved(null, $this->value);
} catch (\Throwable $exception) {
ErrorHandler::notify($exception);
2016-05-21 16:44:52 +02:00
}
}
/**
* {@inheritdoc}
*/
2017-01-04 02:10:27 +01:00
public function listen(callable $onNext) {}
}