1
0
mirror of https://github.com/danog/amp.git synced 2024-11-27 12:35:02 +01:00
amp/lib/Deferred.php

78 lines
2.1 KiB
PHP
Raw Normal View History

2016-08-18 05:25:54 +02:00
<?php declare(strict_types = 1);
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 Interop\Async\Awaitable;
2016-07-12 18:20:06 +02:00
// @codeCoverageIgnoreStart
2016-05-21 16:44:52 +02:00
try {
2016-08-18 05:25:54 +02:00
if (@\assert(false)) {
2016-05-21 16:44:52 +02:00
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
*/
2016-08-11 21:35:58 +02:00
public function getAwaitable(): Awaitable {
2016-05-21 16:44:52 +02:00
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() {
2016-05-22 20:24:39 +02:00
$this->awaitable = new Internal\PrivateAwaitable(function (callable $resolve, callable $fail) {
2016-05-21 16:44:52 +02:00
$this->resolve = $resolve;
$this->fail = $fail;
});
}
/**
* @return \Interop\Async\Awaitable
*/
2016-08-11 21:35:58 +02:00
public function getAwaitable(): Awaitable {
2016-05-21 16:44:52 +02:00
return $this->awaitable;
}
/**
* Fulfill the awaitable with the given value.
*
* @param mixed $value
*/
public function resolve($value = null) {
2016-08-11 21:35:58 +02:00
($this->resolve)($value);
2016-05-21 16:44:52 +02:00
}
/**
* Fails the awaitable the the given reason.
*
2016-08-11 21:35:58 +02:00
* @param \Throwable $reason
2016-05-21 16:44:52 +02:00
*/
2016-08-11 21:35:58 +02:00
public function fail(\Throwable $reason) {
($this->fail)($reason);
2016-05-21 16:44:52 +02:00
}
}
}
} catch (\AssertionError $exception) {
goto development; // zend.assertions=1 and assert.exception=1, use development definition.
2016-07-12 18:20:06 +02:00
} // @codeCoverageIgnoreEnd