1
0
mirror of https://github.com/danog/amp.git synced 2024-11-27 04:24:42 +01:00
amp/lib/Internal/LazyAwaitable.php
2016-05-21 23:47:50 -05:00

51 lines
1.1 KiB
PHP

<?php
namespace Amp\Awaitable\Internal;
use Amp\Awaitable;
class LazyAwaitable implements \Interop\Async\Awaitable {
/**
* @var callable|null
*/
private $provider;
/**
* @var \Interop\Async\Awaitable
*/
private $awaitable;
/**
* @param callable $provider
*/
public function __construct(callable $provider) {
$this->provider = $provider;
}
/**
* @return \Interop\Async\Awaitable
*/
protected function getAwaitable() {
if (null === $this->awaitable) {
$provider = $this->provider;
$this->provider = null;
try {
$this->awaitable = Awaitable\resolve($provider());
} catch (\Throwable $exception) {
$this->awaitable = Awaitable\fail($exception);
} catch (\Exception $exception) {
$this->awaitable = Awaitable\fail($exception);
}
}
return $this->awaitable;
}
/**
* {@inheritdoc}
*/
public function when(callable $onResolved) {
$this->getAwaitable()->when($onResolved);
}
}