1
0
mirror of https://github.com/danog/amp.git synced 2024-12-02 17:37:50 +01:00
amp/lib/Deferred.php
Aaron Piotrowski 0eceb48fad
Refactor internal traits as classes
Trait tests should test Deferred and Emitter instead, will update with other tests.
2020-09-26 23:14:17 -05:00

69 lines
1.6 KiB
PHP

<?php
namespace Amp;
/**
* 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.
*
* @template TValue
*/
final class Deferred
{
private Internal\Placeholder $resolver;
private Internal\PrivatePromise $promise;
public function __construct()
{
$this->resolver = new Internal\Placeholder;
$this->promise = new Internal\PrivatePromise($this->resolver);
}
/**
* @return Promise<TValue>
*/
public function promise(): Promise
{
return $this->promise;
}
/**
* @return bool True if the contained promise has been resolved.
*/
public function isResolved(): bool
{
/** @psalm-suppress UndefinedInterfaceMethod */
return $this->resolver->isResolved();
}
/**
* Fulfill the promise with the given value.
*
* @param mixed $value
*
* @psalm-param TValue|Promise<TValue> $value
*
* @return void
*/
public function resolve(mixed $value = null): void
{
/** @psalm-suppress UndefinedInterfaceMethod */
$this->resolver->resolve($value);
}
/**
* Fails the promise the the given reason.
*
* @param \Throwable $reason
*
* @return void
*/
public function fail(\Throwable $reason): void
{
/** @psalm-suppress UndefinedInterfaceMethod */
$this->resolver->fail($reason);
}
}