1
0
mirror of https://github.com/danog/amp.git synced 2024-11-30 04:29:08 +01:00
amp/lib/Deferred.php
2016-08-17 22:25:54 -05:00

78 lines
2.1 KiB
PHP

<?php declare(strict_types = 1);
namespace Amp;
use Interop\Async\Awaitable;
// @codeCoverageIgnoreStart
try {
if (@\assert(false)) {
production: // PHP 7 production environment (zend.assertions=0)
final class Deferred implements Awaitable {
use Internal\Placeholder {
resolve as public;
fail as public;
}
/**
* @return \Interop\Async\Awaitable
*/
public function getAwaitable(): Awaitable {
return $this;
}
}
} else {
development: // PHP 7 development (zend.assertions=1) or PHP 5.x
final class Deferred {
/**
* @var \Interop\Async\Awaitable
*/
private $awaitable;
/**
* @var callable
*/
private $resolve;
/**
* @var callable
*/
private $fail;
public function __construct() {
$this->awaitable = new Internal\PrivateAwaitable(function (callable $resolve, callable $fail) {
$this->resolve = $resolve;
$this->fail = $fail;
});
}
/**
* @return \Interop\Async\Awaitable
*/
public function getAwaitable(): Awaitable {
return $this->awaitable;
}
/**
* Fulfill the awaitable with the given value.
*
* @param mixed $value
*/
public function resolve($value = null) {
($this->resolve)($value);
}
/**
* Fails the awaitable the the given reason.
*
* @param \Throwable $reason
*/
public function fail(\Throwable $reason) {
($this->fail)($reason);
}
}
}
} catch (\AssertionError $exception) {
goto development; // zend.assertions=1 and assert.exception=1, use development definition.
} // @codeCoverageIgnoreEnd