1
0
mirror of https://github.com/danog/amp.git synced 2024-12-04 02:17:54 +01:00
amp/lib/Internal/LazyAwaitable.php
2016-05-22 13:42:38 -05:00

57 lines
1.3 KiB
PHP

<?php
namespace Amp\Awaitable\Internal;
use Amp\Awaitable\Failure;
use Amp\Awaitable\Success;
use Interop\Async\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 = $provider();
if ($this->awaitable instanceof Awaitable) {
$this->awaitable = new Success($this->awaitable);
}
} catch (\Throwable $exception) {
$this->awaitable = new Failure($exception);
} catch (\Exception $exception) {
$this->awaitable = new Failure($exception);
}
}
return $this->awaitable;
}
/**
* {@inheritdoc}
*/
public function when(callable $onResolved) {
$this->getAwaitable()->when($onResolved);
}
}