2015-05-19 05:57:34 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp;
|
|
|
|
|
|
|
|
/**
|
2015-05-20 00:49:08 +02:00
|
|
|
* Standard Promise interface implementation
|
|
|
|
*
|
2015-05-19 05:57:34 +02:00
|
|
|
* A placeholder value that will be resolved at some point in the future by
|
|
|
|
* the Promisor that created it.
|
|
|
|
*/
|
|
|
|
trait Placeholder {
|
|
|
|
private $isResolved = false;
|
|
|
|
private $watchers = [];
|
|
|
|
private $whens = [];
|
|
|
|
private $error;
|
|
|
|
private $result;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify the $func callback when the promise resolves (whether successful or not)
|
|
|
|
*
|
2015-05-20 00:49:08 +02:00
|
|
|
* @param callable $func An error-first callback to invoke upon promise resolution
|
|
|
|
* @param mixed $data Optional data to pass as a third parameter to $func
|
|
|
|
* @return void
|
2015-05-19 05:57:34 +02:00
|
|
|
*/
|
2015-05-20 00:49:08 +02:00
|
|
|
public function when(callable $func, $data = null) {
|
2015-05-19 05:57:34 +02:00
|
|
|
if ($this->isResolved) {
|
2015-05-20 00:49:08 +02:00
|
|
|
call_user_func($func, $this->error, $this->result, $data);
|
2015-05-19 05:57:34 +02:00
|
|
|
} else {
|
2015-05-20 00:49:08 +02:00
|
|
|
$this->whens[] = [$func, $data];
|
2015-05-19 05:57:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify the $func callback when resolution progress events are emitted
|
|
|
|
*
|
2015-05-20 00:49:08 +02:00
|
|
|
* @param callable $func A callback to invoke when data updates are available
|
|
|
|
* @param mixed $data Optional data to pass as a second parameter to $func
|
|
|
|
* @return void
|
2015-05-19 05:57:34 +02:00
|
|
|
*/
|
2015-05-20 00:49:08 +02:00
|
|
|
public function watch(callable $func, $data = null) {
|
2015-05-19 05:57:34 +02:00
|
|
|
if (!$this->isResolved) {
|
2015-05-20 00:49:08 +02:00
|
|
|
$this->watchers[] = [$func, $data];
|
2015-05-19 05:57:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function update($progress) {
|
|
|
|
if ($this->isResolved) {
|
|
|
|
throw new \LogicException(
|
|
|
|
"Cannot update resolved promise"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function resolve(\Exception $error = null, $result = null) {
|
|
|
|
if ($this->isResolved) {
|
|
|
|
throw new \LogicException(
|
|
|
|
"Promise already resolved"
|
|
|
|
);
|
|
|
|
} elseif ($result === $this) {
|
|
|
|
throw new \LogicException(
|
|
|
|
"A Promise cannot act as its own resolution result"
|
|
|
|
);
|
|
|
|
} elseif ($result instanceof Promise) {
|
|
|
|
$result->when(function($error, $result) {
|
|
|
|
$this->resolve($error, $result);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$this->isResolved = true;
|
|
|
|
$this->error = $error;
|
|
|
|
$this->result = $result;
|
|
|
|
foreach ($this->whens as $when) {
|
2015-05-20 00:49:08 +02:00
|
|
|
call_user_func($when[0], $error, $result, $when[1]);
|
2015-05-19 05:57:34 +02:00
|
|
|
}
|
|
|
|
$this->whens = $this->watchers = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|