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;
|
|
|
|
|
|
|
|
/**
|
2015-07-27 16:07:26 +02:00
|
|
|
* Notify the $cb callback when the promise resolves (whether successful or not)
|
2015-05-19 05:57:34 +02:00
|
|
|
*
|
2015-07-27 16:07:26 +02:00
|
|
|
* @param callable $cb An error-first callback to invoke upon promise resolution
|
|
|
|
* @param mixed $cbData Optional data to pass as a third parameter to $cb
|
2015-06-12 12:48:11 +02:00
|
|
|
* @return self
|
2015-05-19 05:57:34 +02:00
|
|
|
*/
|
2015-07-27 16:07:26 +02:00
|
|
|
public function when(callable $cb, $cbData = null) {
|
2015-05-19 05:57:34 +02:00
|
|
|
if ($this->isResolved) {
|
2015-07-27 16:07:26 +02:00
|
|
|
\call_user_func($cb, $this->error, $this->result, $cbData);
|
2015-05-19 05:57:34 +02:00
|
|
|
} else {
|
2015-07-27 16:07:26 +02:00
|
|
|
$this->whens[] = [$cb, $cbData];
|
2015-05-19 05:57:34 +02:00
|
|
|
}
|
2015-06-12 12:48:11 +02:00
|
|
|
|
|
|
|
return $this;
|
2015-05-19 05:57:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-27 16:07:26 +02:00
|
|
|
* Notify the $cb callback when resolution progress events are emitted
|
2015-05-19 05:57:34 +02:00
|
|
|
*
|
2015-07-27 16:07:26 +02:00
|
|
|
* @param callable $cb A callback to invoke when data updates are available
|
|
|
|
* @param mixed $cbData Optional data to pass as a second parameter to $cb
|
2015-06-12 12:48:11 +02:00
|
|
|
* @return self
|
2015-05-19 05:57:34 +02:00
|
|
|
*/
|
2015-07-27 16:07:26 +02:00
|
|
|
public function watch(callable $cb, $cbData = null) {
|
2015-05-19 05:57:34 +02:00
|
|
|
if (!$this->isResolved) {
|
2015-07-27 16:07:26 +02:00
|
|
|
$this->watchers[] = [$cb, $cbData];
|
2015-05-19 05:57:34 +02:00
|
|
|
}
|
2015-06-12 12:48:11 +02:00
|
|
|
|
|
|
|
return $this;
|
2015-05-19 05:57:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function update($progress) {
|
|
|
|
if ($this->isResolved) {
|
|
|
|
throw new \LogicException(
|
|
|
|
"Cannot update resolved promise"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
foreach ($this->watchers as $watcher) {
|
2015-07-27 16:07:26 +02:00
|
|
|
list($cb, $cbData) = $watcher;
|
|
|
|
\call_user_func($cb, $progress, $cbData);
|
2015-05-19 05:57:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
private function resolve($error = null, $result = null) {
|
2015-05-19 05:57:34 +02:00
|
|
|
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);
|
|
|
|
});
|
2015-07-21 18:25:34 +02:00
|
|
|
} elseif (isset($error) && !($error instanceof \Throwable || $error instanceof \Exception)) {
|
2015-06-08 21:11:17 +02:00
|
|
|
throw new \InvalidArgumentException(
|
2015-07-21 18:25:34 +02:00
|
|
|
"Throwable Exception instance required to fail a promise"
|
2015-06-08 21:11:17 +02:00
|
|
|
);
|
2015-05-19 05:57:34 +02:00
|
|
|
} else {
|
|
|
|
$this->isResolved = true;
|
|
|
|
$this->error = $error;
|
|
|
|
$this->result = $result;
|
|
|
|
foreach ($this->whens as $when) {
|
2015-07-27 16:07:26 +02:00
|
|
|
list($cb, $cbData) = $when;
|
|
|
|
\call_user_func($cb, $error, $result, $cbData);
|
2015-05-19 05:57:34 +02:00
|
|
|
}
|
|
|
|
$this->whens = $this->watchers = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|