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
|
|
|
|
*
|
2015-06-01 01:33:55 +02:00
|
|
|
* @param mixed $progress1, $progress2, ... $progressN
|
2015-05-19 05:57:34 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function update($progress) {
|
|
|
|
if ($this->isResolved) {
|
|
|
|
throw new \LogicException(
|
2015-07-21 18:25:34 +02:00
|
|
|
"Cannot update resolved promise"
|
2015-05-19 05:57:34 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-06-01 01:33:55 +02:00
|
|
|
$baseArgs = func_get_args();
|
2015-05-19 05:57:34 +02:00
|
|
|
foreach ($this->watchers as $watcher) {
|
2015-06-01 01:33:55 +02:00
|
|
|
$args = $baseArgs;
|
|
|
|
$args[] = $watcher[1];
|
|
|
|
\call_user_func_array($watcher[0], $args);
|
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-06-08 21:11:17 +02:00
|
|
|
* The error parameter used to fail a promisor must always be an exception
|
|
|
|
* instance. However, we cannot typehint this parameter in environments
|
2015-07-07 16:15:57 +02:00
|
|
|
* where PHP5.x compatibility is required because PHP7 Throwable
|
2015-06-08 21:11:17 +02:00
|
|
|
* instances will break the typehint.
|
|
|
|
*
|
2015-07-07 16:15:57 +02:00
|
|
|
* @param mixed $error An Exception or Throwable in PHP7 environments
|
2015-05-19 05:57:34 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
2015-06-08 21:11:17 +02:00
|
|
|
public function fail($error) {
|
2015-05-20 00:49:08 +02:00
|
|
|
$this->resolve($error, $result = null);
|
2015-05-19 05:57:34 +02:00
|
|
|
}
|
|
|
|
}
|