1
0
mirror of https://github.com/danog/amp.git synced 2024-11-27 12:35:02 +01:00
amp/lib/PrivateFuture.php
2015-03-19 11:26:56 -04:00

65 lines
1.8 KiB
PHP

<?php
namespace Amp;
/**
* A PrivateFuture creates a read-only Promise that may *only* be fulfilled by holders of the
* actual PrivateFuture instance. This provides an additional layer of API protection over
* the standard Future Promisor implementation whose Promise can be resolved by any code
* holding a reference to the Future instance.
*/
class PrivateFuture implements Promisor {
private $resolver;
private $updater;
private $promise;
public function __construct() {
$this->promise = new Unresolved;
$this->resolver = function(\Exception $error = null, $result = null) {
// bound to private Unresolved::resolve() at call-time
$this->resolve($error, $result);
};
$this->updater = function(...$progress) {
// bound to private Unresolved::update() at call-time
$this->update(...$progress);
};
}
/**
* Promise future fulfillment via a temporary placeholder value
*/
public function promise(): Promise {
return $this->promise;
}
/**
* Update subscribers of progress resolving the promised value
*
* @param mixed $progress
* @return void
*/
public function update(...$progress) {
$this->updater->call($this->promise, ...$progress);
}
/**
* Resolve the promised value as a success
*
* @param mixed $result
* @return void
*/
public function succeed($result = null) {
$this->resolver->call($this->promise, $error = null, $result);
}
/**
* Resolve the promised value as a failure
*
* @param \Exception $error
* @return void
*/
public function fail(\Exception $error) {
$this->resolver->call($this->promise, $error, $result = null);
}
}