mirror of
https://github.com/danog/amp.git
synced 2024-11-27 04:24:42 +01:00
82 lines
2.4 KiB
PHP
82 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Amp;
|
|
|
|
/**
|
|
* Standard Promise interface implementation
|
|
*
|
|
* 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)
|
|
*
|
|
* @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
|
|
*/
|
|
public function when(callable $func, $data = null) {
|
|
if ($this->isResolved) {
|
|
call_user_func($func, $this->error, $this->result, $data);
|
|
} else {
|
|
$this->whens[] = [$func, $data];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Notify the $func callback when resolution progress events are emitted
|
|
*
|
|
* @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
|
|
*/
|
|
public function watch(callable $func, $data = null) {
|
|
if (!$this->isResolved) {
|
|
$this->watchers[] = [$func, $data];
|
|
}
|
|
}
|
|
|
|
private function update($progress) {
|
|
if ($this->isResolved) {
|
|
throw new \LogicException(
|
|
"Cannot update resolved promise"
|
|
);
|
|
}
|
|
|
|
foreach ($this->watchers as $watcher) {
|
|
call_user_func($watcher[0], $progress, $watcher[1]);
|
|
}
|
|
}
|
|
|
|
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) {
|
|
call_user_func($when[0], $error, $result, $when[1]);
|
|
}
|
|
$this->whens = $this->watchers = [];
|
|
}
|
|
}
|
|
}
|