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

84 lines
2.4 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
2016-07-12 18:20:06 +02:00
// @codeCoverageIgnoreStart
2016-05-21 16:44:52 +02:00
try {
if (!@\assert(false)) {
development: // PHP 7 development (zend.assertions=1)
/**
* Deferred is a container for a promise that is resolved using the resolve() and fail() methods of this object.
* The contained promise may be accessed using the promise() method. This object should not be part of a public
* API, but used internally to create and resolve a promise.
*/
2016-05-21 16:44:52 +02:00
final class Deferred {
/**
* @var \Amp\Promise
2016-05-21 16:44:52 +02:00
*/
2016-11-14 20:59:21 +01:00
private $promise;
2016-05-21 16:44:52 +02:00
/**
* @var callable
*/
private $resolve;
/**
* @var callable
*/
private $fail;
public function __construct() {
2016-11-14 20:59:21 +01:00
$this->promise = new Internal\PrivatePromise(function (callable $resolve, callable $fail) {
2016-05-21 16:44:52 +02:00
$this->resolve = $resolve;
$this->fail = $fail;
});
}
/**
* @return \Amp\Promise
2016-05-21 16:44:52 +02:00
*/
2016-11-14 20:59:21 +01:00
public function promise(): Promise {
return $this->promise;
2016-05-21 16:44:52 +02:00
}
/**
2016-11-14 20:59:21 +01:00
* Fulfill the promise with the given value.
2016-05-21 16:44:52 +02:00
*
* @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
}
/**
2016-11-14 20:59:21 +01:00
* Fails the promise the the given reason.
2016-05-21 16:44:52 +02:00
*
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
}
}
} else {
production: // PHP 7 production environment (zend.assertions=0)
/**
* An optimized version of Deferred for production environments that is itself the promise.
*/
final class Deferred implements Promise {
use Internal\Placeholder {
resolve as public;
fail as public;
}
/**
* @return \Amp\Promise
*/
public function promise(): Promise {
return $this;
}
}
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