1
0
mirror of https://github.com/danog/amp.git synced 2024-11-30 04:29:08 +01:00

Make promise updates variadic (ugly)

This commit is contained in:
Daniel Lowrey 2015-05-31 19:33:55 -04:00
parent 48d8aaf0e5
commit 954eaabcf5
5 changed files with 41 additions and 8 deletions

View File

@ -50,8 +50,11 @@ trait Placeholder {
);
}
$baseArgs = func_get_args();
foreach ($this->watchers as $watcher) {
call_user_func($watcher[0], $progress, $watcher[1]);
$args = $baseArgs;
$args[] = $watcher[1];
\call_user_func_array($watcher[0], $args);
}
}

View File

@ -20,7 +20,7 @@ trait PrivatePromisor {
};
$updater = function($progress) {
// bound to private PrivatePlaceholder::update()
$this->update($progress);
\call_user_func_array([$this, "update"], func_get_args());
};
$this->resolver = $resolver->bindTo($placeholder, $placeholder);
$this->updater = $updater->bindTo($placeholder, $placeholder);
@ -39,11 +39,11 @@ trait PrivatePromisor {
/**
* Update watchers of progress resolving the promised value
*
* @param mixed $progress
* @param mixed $progress1, $progress2, ... $progressN
* @return void
*/
public function update($progress) {
call_user_func($this->updater, $progress);
\call_user_func_array($this->updater, func_get_args());
}
/**

View File

@ -32,7 +32,7 @@ namespace Amp;
interface Promisor {
/**
* Promise deferred fulfillment via a temporary placeholder value
*
*
* @return \Amp\Promise
*/
public function promise();
@ -40,7 +40,11 @@ interface Promisor {
/**
* Update watchers of progress resolving the promised value
*
* @param mixed $progress
* Implementations must support variadic argument passing to update
* even though 5.5-compatibility prevents us from specifying it as
* part of the API.
*
* @param mixed $progress1, $progress2, ... $progressN
* @return void
*/
public function update($progress);

View File

@ -20,7 +20,7 @@ trait PublicPromisor {
/**
* Update watchers of progress resolving the promised value
*
* @param mixed $progress
* @param mixed $progress1, $progress2, ... $progressN
* @return void
*/
public function update($progress) {
@ -30,8 +30,11 @@ trait PublicPromisor {
);
}
$baseArgs = func_get_args();
foreach ($this->watchers as $watcher) {
call_user_func($watcher[0], $progress, $watcher[1]);
$args = $baseArgs;
$args[] = $watcher[1];
\call_user_func_array($watcher[0], $args);
}
}

View File

@ -116,4 +116,27 @@ abstract class PromisorTest extends \PHPUnit_Framework_TestCase {
$this->assertSame(6, $updatable);
}
public function testUpdateVariadicArgs() {
$updates = new \StdClass;
$updates->arr = [];
$promisor = $this->getPromisor();
$promise = $promisor->promise();
$promise->watch(function($arg1, $arg2, $arg3) use ($updates) {
$updates->arr[] = func_get_args();
}, "cb_data");
$promisor->update(1, "foo");
$promisor->update(2, "bar");
$promisor->update(3, "baz");
$expected = [
[1, "foo", "cb_data"],
[2, "bar", "cb_data"],
[3, "baz", "cb_data"],
];
$this->assertSame($expected, $updates->arr);
}
}