mirror of
https://github.com/danog/amp.git
synced 2024-11-30 04:29:08 +01:00
promise updates are no longer variadic + misc formatting
This commit is contained in:
parent
38b3fbbbbb
commit
1d9d572ee5
@ -31,10 +31,10 @@ class Failure implements Promise {
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* NOTE: because this object represents a resolved Promise it will *always* invoke
|
||||
* the specified $func callback immediately.
|
||||
* the specified $cb callback immediately.
|
||||
*/
|
||||
public function when(callable $func, $data = null) {
|
||||
\call_user_func($func, $this->error, $result = null, $data);
|
||||
public function when(callable $cb, $cbData = null) {
|
||||
\call_user_func($cb, $this->error, $result = null, $cbData);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -44,7 +44,7 @@ class Failure implements Promise {
|
||||
*
|
||||
* Does nothing; a resolved promise has no progress updates
|
||||
*/
|
||||
public function watch(callable $func, $data = null) {
|
||||
public function watch(callable $cb, $cbData = null) {
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
@ -16,32 +16,32 @@ trait Placeholder {
|
||||
private $result;
|
||||
|
||||
/**
|
||||
* Notify the $func callback when the promise resolves (whether successful or not)
|
||||
* Notify the $cb 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
|
||||
* @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
|
||||
* @return self
|
||||
*/
|
||||
public function when(callable $func, $data = null) {
|
||||
public function when(callable $cb, $cbData = null) {
|
||||
if ($this->isResolved) {
|
||||
\call_user_func($func, $this->error, $this->result, $data);
|
||||
\call_user_func($cb, $this->error, $this->result, $cbData);
|
||||
} else {
|
||||
$this->whens[] = [$func, $data];
|
||||
$this->whens[] = [$cb, $cbData];
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify the $func callback when resolution progress events are emitted
|
||||
* Notify the $cb 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
|
||||
* @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
|
||||
* @return self
|
||||
*/
|
||||
public function watch(callable $func, $data = null) {
|
||||
public function watch(callable $cb, $cbData = null) {
|
||||
if (!$this->isResolved) {
|
||||
$this->watchers[] = [$func, $data];
|
||||
$this->watchers[] = [$cb, $cbData];
|
||||
}
|
||||
|
||||
return $this;
|
||||
@ -53,12 +53,9 @@ trait Placeholder {
|
||||
"Cannot update resolved promise"
|
||||
);
|
||||
}
|
||||
|
||||
$baseArgs = \func_get_args();
|
||||
foreach ($this->watchers as $watcher) {
|
||||
$args = $baseArgs;
|
||||
$args[] = $watcher[1];
|
||||
\call_user_func_array($watcher[0], $args);
|
||||
list($cb, $cbData) = $watcher;
|
||||
\call_user_func($cb, $progress, $cbData);
|
||||
}
|
||||
}
|
||||
|
||||
@ -90,7 +87,8 @@ trait Placeholder {
|
||||
$this->error = $error;
|
||||
$this->result = $result;
|
||||
foreach ($this->whens as $when) {
|
||||
\call_user_func($when[0], $error, $result, $when[1]);
|
||||
list($cb, $cbData) = $when;
|
||||
\call_user_func($cb, $error, $result, $cbData);
|
||||
}
|
||||
$this->whens = $this->watchers = [];
|
||||
}
|
||||
|
@ -5,6 +5,8 @@ namespace Amp;
|
||||
/**
|
||||
* A placeholder value that will be resolved at some point in the future by
|
||||
* the Promisor that created it.
|
||||
*
|
||||
* @TODO remove this and use an anonymous class once PHP7 is required
|
||||
*/
|
||||
class PrivatePlaceholder implements Promise {
|
||||
use Placeholder;
|
||||
|
@ -13,14 +13,15 @@ trait PrivatePromisor {
|
||||
private $promise;
|
||||
|
||||
public function __construct() {
|
||||
// @TODO Replace PrivatePlaceholder with an anonymous class once PHP7 is required
|
||||
$placeholder = new PrivatePlaceholder;
|
||||
$resolver = function($error = null, $result = null) {
|
||||
// bound to private PrivatePlaceholder::resolve()
|
||||
// bound to private $placeholder::resolve()
|
||||
$this->resolve($error, $result);
|
||||
};
|
||||
$updater = function($progress) {
|
||||
// bound to private PrivatePlaceholder::update()
|
||||
\call_user_func_array([$this, "update"], \func_get_args());
|
||||
// bound to private $placeholder::update()
|
||||
\call_user_func([$this, "update"], $progress);
|
||||
};
|
||||
$this->resolver = $resolver->bindTo($placeholder, $placeholder);
|
||||
$this->updater = $updater->bindTo($placeholder, $placeholder);
|
||||
@ -43,7 +44,7 @@ trait PrivatePromisor {
|
||||
* @return void
|
||||
*/
|
||||
public function update($progress) {
|
||||
\call_user_func_array($this->updater, func_get_args());
|
||||
\call_user_func($this->updater, $progress);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -7,9 +7,9 @@ namespace Amp;
|
||||
*/
|
||||
interface Promise {
|
||||
/**
|
||||
* Notify the $func callback when the promise resolves (whether successful or not)
|
||||
* Notify the $cb callback when the promise resolves (whether successful or not)
|
||||
*
|
||||
* Implementations MUST invoke the $func callback in error-first style, e.g.:
|
||||
* Implementations MUST invoke the $cb callback in error-first style, e.g.:
|
||||
*
|
||||
* <?php
|
||||
* $promise->when(function(\Exception $error = null, $result = null) {
|
||||
@ -22,25 +22,25 @@ interface Promise {
|
||||
*
|
||||
* Implementations MUST return the current object instance.
|
||||
*
|
||||
* @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
|
||||
* @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
|
||||
* @return self
|
||||
*/
|
||||
public function when(callable $func, $data = null);
|
||||
public function when(callable $cb, $cbData = null);
|
||||
|
||||
/**
|
||||
* Notify the $func callback when resolution progress events are emitted
|
||||
* Notify the $cb callback when resolution progress events are emitted
|
||||
*
|
||||
* Implementations MUST invoke $func callback with a single update parameter, e.g.:
|
||||
* Implementations MUST invoke $cb callback with a single update parameter, e.g.:
|
||||
*
|
||||
* <?php
|
||||
* $promise->watch(function($update) { ... });
|
||||
*
|
||||
* Implementations MUST return the current object instance.
|
||||
*
|
||||
* @param callable $func A callback to invoke when data updates are available
|
||||
* @param mixed $data Optional data to pass as an additional parameter to $func
|
||||
* @param callable $cb A callback to invoke when data updates are available
|
||||
* @param mixed $cbData Optional data to pass as an additional parameter to $cb
|
||||
* @return self
|
||||
*/
|
||||
public function watch(callable $func, $data = null);
|
||||
public function watch(callable $cb, $cbData = null);
|
||||
}
|
||||
|
@ -19,10 +19,10 @@ class Success implements Promise {
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* NOTE: because this object represents a resolved Promise it will *always* invoke
|
||||
* the specified $func callback immediately.
|
||||
* the specified $cb callback immediately.
|
||||
*/
|
||||
public function when(callable $func, $data = null) {
|
||||
\call_user_func($func, $error = null, $this->result, $data);
|
||||
public function when(callable $cb, $cbData = null) {
|
||||
\call_user_func($cb, $error = null, $this->result, $cbData);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -32,7 +32,7 @@ class Success implements Promise {
|
||||
*
|
||||
* Does nothing; a resolved promise has no progress updates
|
||||
*/
|
||||
public function watch(callable $func, $data = null) {
|
||||
public function watch(callable $cb, $cbData = null) {
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
@ -503,6 +503,11 @@ function promises(array $values) {
|
||||
* $update = (yield $promisedUpdate);
|
||||
* }
|
||||
*
|
||||
* NOTE: Promise::watch() updates allow variadic parameters. Only the first
|
||||
* argument passed using Promisor::update() is resolved when yielding stream
|
||||
* elements. Update producers are encouraged to limit update data to a single
|
||||
* parameter.
|
||||
*
|
||||
* @param \Amp\Promise $promise A promise that receives watch() updates
|
||||
* @return \Generator A generator yielding a promise for each watch() update
|
||||
*/
|
||||
|
@ -117,24 +117,24 @@ abstract class PromisorTest extends \PHPUnit_Framework_TestCase {
|
||||
$this->assertSame(6, $updatable);
|
||||
}
|
||||
|
||||
public function testUpdateVariadicArgs() {
|
||||
public function testUpdateArgs() {
|
||||
$updates = new \StdClass;
|
||||
$updates->arr = [];
|
||||
|
||||
$promisor = $this->getPromisor();
|
||||
$promise = $promisor->promise();
|
||||
$promise->watch(function($arg1, $arg2, $arg3) use ($updates) {
|
||||
$updates->arr[] = func_get_args();
|
||||
$promise->watch(function($progress, $cbData) use ($updates) {
|
||||
$updates->arr[] = \func_get_args();
|
||||
}, "cb_data");
|
||||
|
||||
$promisor->update(1, "foo");
|
||||
$promisor->update(2, "bar");
|
||||
$promisor->update(3, "baz");
|
||||
$promisor->update(1);
|
||||
$promisor->update(2);
|
||||
$promisor->update(3);
|
||||
|
||||
$expected = [
|
||||
[1, "foo", "cb_data"],
|
||||
[2, "bar", "cb_data"],
|
||||
[3, "baz", "cb_data"],
|
||||
[1, "cb_data"],
|
||||
[2, "cb_data"],
|
||||
[3, "cb_data"],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, $updates->arr);
|
||||
|
Loading…
Reference in New Issue
Block a user