Skip to content

Commit

Permalink
Merge branch 'some-optimizations'
Browse files Browse the repository at this point in the history
  • Loading branch information
mcg-web committed Nov 15, 2016
2 parents aa8e79c + 3d93a30 commit 036b68f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 6 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "mcg-web/promise-factory",
"description": "This library tries to create a simple promise factory standard while waiting for a psr.",
"type": "library",
"keywords": ["promise", "factory", "guzzle", "react"],
"autoload": {
"psr-4": {
"McGWeb\\PromiseFactory\\": "src/"
Expand Down
21 changes: 18 additions & 3 deletions src/Factory/GuzzleHttpPromiseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class GuzzleHttpPromiseFactory implements PromiseFactoryInterface
*/
public static function create(&$resolve = null, &$reject = null, callable $canceller = null)
{
$promise = new Promise(null, $canceller);
$queue = \GuzzleHttp\Promise\queue();
$promise = new Promise([$queue, 'run'], $canceller);

$reject = [$promise, 'reject'];
$resolve = [$promise, 'resolve'];
Expand All @@ -39,7 +40,7 @@ public static function create(&$resolve = null, &$reject = null, callable $cance
*
* @return FulfilledPromise a full filed Promise
*/
public static function createResolve($promiseOrValue)
public static function createResolve($promiseOrValue = null)
{
$promise = \GuzzleHttp\Promise\promise_for($promiseOrValue);

Expand All @@ -65,7 +66,7 @@ public static function createReject($promiseOrValue)
*/
public static function createAll($promisesOrValues)
{
$promise = \GuzzleHttp\Promise\all($promisesOrValues);
$promise = empty($promisesOrValues) ? static::createResolve($promisesOrValues) : \GuzzleHttp\Promise\all($promisesOrValues);

return $promise;
}
Expand Down Expand Up @@ -96,6 +97,7 @@ public static function await($promise = null, $unwrap = false)
if (!static::isPromise($promise)) {
throw new \InvalidArgumentException(sprintf('The "%s" method must be called with a Promise ("then" method).', __METHOD__));
}

/** @var Promise $promise */
$promise->then(function ($values) use (&$resolvedValue) {
$resolvedValue = $values;
Expand All @@ -116,4 +118,17 @@ public static function await($promise = null, $unwrap = false)

return $resolvedValue;
}

/**
* Cancel a promise
*
* @param PromiseInterface $promise
*/
public static function cancel($promise)
{
if (!static::isPromise($promise, true)) {
throw new \InvalidArgumentException(sprintf('The "%s" method must be called with a compatible Promise.', __METHOD__));
}
$promise->cancel();
}
}
16 changes: 15 additions & 1 deletion src/Factory/ReactPromiseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace McGWeb\PromiseFactory\Factory;

use McGWeb\PromiseFactory\PromiseFactoryInterface;
use React\Promise\CancellablePromiseInterface;
use React\Promise\Deferred;
use React\Promise\FulfilledPromise;
use React\Promise\Promise;
Expand Down Expand Up @@ -40,7 +41,7 @@ public static function create(&$resolve = null, &$reject = null, callable $cance
*
* @return FulfilledPromise a full filed Promise
*/
public static function createResolve($promiseOrValue)
public static function createResolve($promiseOrValue = null)
{
return \React\Promise\resolve($promiseOrValue);
}
Expand Down Expand Up @@ -107,4 +108,17 @@ public static function await($promise = null, $unwrap = false)

return $resolvedValue;
}

/**
* Cancel a promise
*
* @param CancellablePromiseInterface $promise
*/
public static function cancel($promise)
{
if (!$promise instanceof CancellablePromiseInterface) {
throw new \InvalidArgumentException(sprintf('The "%s" method must be called with a compatible Promise.', __METHOD__));
}
$promise->cancel();
}
}
10 changes: 8 additions & 2 deletions src/PromiseFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static function create(&$resolve = null, &$reject = null, callable $cance
*
* @return mixed a full filed Promise
*/
public static function createResolve($promiseOrValue);
public static function createResolve($promiseOrValue = null);

/**
* Creates a rejected promise for a reason if the reason is not a promise. If
Expand All @@ -53,7 +53,6 @@ public static function createReject($promiseOrValue);
*/
public static function createAll($promisesOrValues);


/**
* Check if value is a promise
*
Expand All @@ -72,4 +71,11 @@ public static function isPromise($value, $strict = false);
* @return mixed
*/
public static function await($promise = null, $unwrap = false);

/**
* Cancel a promise
*
* @param $promise
*/
public static function cancel($promise);
}
33 changes: 33 additions & 0 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,39 @@ public function testAwaitWithInvalidPromise(PromiseFactoryInterface $factory)
$factory->await(new \stdClass(), true);
}

/**
* @dataProvider factoryDataProvider
* @param PromiseFactoryInterface $factory
*
* @expectedException \Exception
* @expectedExceptionMessage Cancel promise!
*/
public function testCancel(PromiseFactoryInterface $factory)
{
$promise = $factory->create($resolve, $reject, function () {
throw new \Exception('Cancel promise!');
});

$factory->cancel($promise);
$factory->await($promise, true);
}

/**
* @dataProvider factoryDataProvider
* @param PromiseFactoryInterface $factory
*
* @expectedException \Exception
* @expectedExceptionMessage ::cancel" method must be called with a compatible Promise.
*/
public function testCancelInvalidPromise(PromiseFactoryInterface $factory)
{
$factory->create($resolve, $reject, function () {
throw new \Exception('Cancel will never be called!');
});

$factory->cancel(new \stdClass());
}

public function factoryDataProvider()
{
return [
Expand Down

0 comments on commit 036b68f

Please sign in to comment.