2015-05-19 05:57:34 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp;
|
|
|
|
|
|
|
|
trait PublicPromisor {
|
|
|
|
use Placeholder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Promise future fulfillment via a temporary placeholder value
|
|
|
|
*
|
|
|
|
* This implementation acts as both Promisor and Promise so we simply return the
|
|
|
|
* current instance.
|
|
|
|
*
|
|
|
|
* @return \Amp\Promise
|
|
|
|
*/
|
|
|
|
public function promise() {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update watchers of progress resolving the promised value
|
|
|
|
*
|
|
|
|
* @param mixed $progress
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function update($progress) {
|
|
|
|
if ($this->isResolved) {
|
|
|
|
throw new \LogicException(
|
|
|
|
'Cannot update resolved promise'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->watchers as $watcher) {
|
2015-05-20 00:49:08 +02:00
|
|
|
call_user_func($watcher[0], $progress, $watcher[1]);
|
2015-05-19 05:57:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve the promised value as a success
|
|
|
|
*
|
|
|
|
* @param mixed $result
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function succeed($result = null) {
|
2015-05-20 00:49:08 +02:00
|
|
|
$this->resolve($error = null, $result);
|
2015-05-19 05:57:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve the promised value as a failure
|
|
|
|
*
|
2015-05-20 00:49:08 +02:00
|
|
|
* @param \Exception $error
|
2015-05-19 05:57:34 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function fail(\Exception $error) {
|
2015-05-20 00:49:08 +02:00
|
|
|
$this->resolve($error, $result = null);
|
2015-05-19 05:57:34 +02:00
|
|
|
}
|
|
|
|
}
|