2016-12-29 14:09:49 -06:00
|
|
|
<?php
|
2016-08-15 23:46:26 -05:00
|
|
|
|
2016-05-23 22:48:28 -05:00
|
|
|
namespace Amp;
|
2016-05-21 09:44:52 -05:00
|
|
|
|
2017-12-02 19:07:46 -06:00
|
|
|
/**
|
|
|
|
* 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.
|
2020-04-04 16:35:52 +02:00
|
|
|
*
|
|
|
|
* @template TValue
|
2017-12-02 19:07:46 -06:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
final class Deferred
|
|
|
|
{
|
2020-09-26 23:14:17 -05:00
|
|
|
private Internal\Placeholder $resolver;
|
2017-12-20 10:30:43 -06:00
|
|
|
|
2020-09-24 11:52:22 -05:00
|
|
|
private Internal\PrivatePromise $promise;
|
2017-12-02 19:07:46 -06:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
2020-09-26 23:14:17 -05:00
|
|
|
$this->resolver = new Internal\Placeholder;
|
2018-01-13 10:36:01 -06:00
|
|
|
$this->promise = new Internal\PrivatePromise($this->resolver);
|
2017-12-02 19:07:46 -06:00
|
|
|
}
|
2016-05-21 09:44:52 -05:00
|
|
|
|
2017-12-02 19:07:46 -06:00
|
|
|
/**
|
2020-04-04 16:35:52 +02:00
|
|
|
* @return Promise<TValue>
|
2017-12-02 19:07:46 -06:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function promise(): Promise
|
|
|
|
{
|
2017-12-02 19:07:46 -06:00
|
|
|
return $this->promise;
|
|
|
|
}
|
2016-05-21 09:44:52 -05:00
|
|
|
|
2020-07-16 13:50:38 -05:00
|
|
|
/**
|
|
|
|
* @return bool True if the contained promise has been resolved.
|
|
|
|
*/
|
|
|
|
public function isResolved(): bool
|
|
|
|
{
|
2020-07-17 11:19:36 -05:00
|
|
|
/** @psalm-suppress UndefinedInterfaceMethod */
|
2020-07-16 13:50:38 -05:00
|
|
|
return $this->resolver->isResolved();
|
|
|
|
}
|
|
|
|
|
2017-12-02 19:07:46 -06:00
|
|
|
/**
|
|
|
|
* Fulfill the promise with the given value.
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
2020-03-28 12:23:46 +01:00
|
|
|
*
|
2020-04-05 22:37:09 +02:00
|
|
|
* @psalm-param TValue|Promise<TValue> $value
|
2020-04-04 16:35:52 +02:00
|
|
|
*
|
2020-03-28 12:23:46 +01:00
|
|
|
* @return void
|
2017-12-02 19:07:46 -06:00
|
|
|
*/
|
2020-09-24 11:52:22 -05:00
|
|
|
public function resolve(mixed $value = null): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-03-28 12:23:46 +01:00
|
|
|
/** @psalm-suppress UndefinedInterfaceMethod */
|
2017-12-20 10:30:43 -06:00
|
|
|
$this->resolver->resolve($value);
|
2017-12-02 19:07:46 -06:00
|
|
|
}
|
2016-05-21 09:44:52 -05:00
|
|
|
|
2017-12-02 19:07:46 -06:00
|
|
|
/**
|
|
|
|
* Fails the promise the the given reason.
|
|
|
|
*
|
|
|
|
* @param \Throwable $reason
|
2020-03-28 12:23:46 +01:00
|
|
|
*
|
|
|
|
* @return void
|
2017-12-02 19:07:46 -06:00
|
|
|
*/
|
2020-09-24 11:52:22 -05:00
|
|
|
public function fail(\Throwable $reason): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-03-28 12:23:46 +01:00
|
|
|
/** @psalm-suppress UndefinedInterfaceMethod */
|
2017-12-20 10:30:43 -06:00
|
|
|
$this->resolver->fail($reason);
|
2016-05-21 09:44:52 -05:00
|
|
|
}
|
2017-12-02 19:07:46 -06:00
|
|
|
}
|