Shortened method names of PromiseAdapter

This commit is contained in:
vladar 2016-12-04 15:49:34 +07:00
parent 26db4c76c6
commit a165c3aaab
7 changed files with 61 additions and 49 deletions

View File

@ -5,8 +5,14 @@ use GraphQL\Executor\Promise\Adapter\SyncPromise;
class Deferred
{
/**
* @var \SplQueue
*/
private static $queue;
/**
* @var callable
*/
private $callback;
/**
@ -21,8 +27,8 @@ class Deferred
public static function runQueue()
{
$q = self::getQueue();
while (!$q->isEmpty()) {
$q = self::$queue;
while ($q && !$q->isEmpty()) {
/** @var self $dfd */
$dfd = $q->dequeue();
$dfd->run();

View File

@ -225,7 +225,7 @@ class Executor
// field and its descendants will be omitted, and sibling fields will still
// be executed. An execution which encounters errors will still result in a
// resolved Promise.
$result = $this->promises->createPromise(function (callable $resolve) {
$result = $this->promises->create(function (callable $resolve) {
return $resolve($this->executeOperation($this->exeContext->operation, $this->exeContext->rootValue));
});
return $result
@ -313,7 +313,7 @@ class Executor
*/
private function executeFieldsSerially(ObjectType $parentType, $sourceValue, $path, $fields)
{
$prevPromise = $this->promises->createResolvedPromise([]);
$prevPromise = $this->promises->createFulfilled([]);
$process = function ($results, $responseName, $path, $parentType, $sourceValue, $fieldNodes) {
$fieldPath = $path;
@ -398,7 +398,7 @@ class Executor
$keys = array_keys($assoc);
$valuesAndPromises = array_values($assoc);
$promise = $this->promises->createPromiseAll($valuesAndPromises);
$promise = $this->promises->all($valuesAndPromises);
return $promise->then(function($values) use ($keys) {
$resolvedResults = [];
@ -729,7 +729,7 @@ class Executor
if ($completed instanceof Promise) {
return $completed->then(null, function ($error) use ($exeContext) {
$exeContext->addError($error);
return $this->promises->createResolvedPromise(null);
return $this->promises->createFulfilled(null);
});
}
return $completed;
@ -772,7 +772,7 @@ class Executor
);
if ($completed instanceof Promise) {
return $completed->then(null, function ($error) use ($fieldNodes, $path) {
return $this->promises->createRejectedPromise(Error::createLocatedError($error, $fieldNodes, $path));
return $this->promises->createRejected(Error::createLocatedError($error, $fieldNodes, $path));
});
}
return $completed;
@ -820,7 +820,7 @@ class Executor
)
{
if ($this->promises->isThenable($result)) {
$result = $this->promises->convert($result);
$result = $this->promises->convertThenable($result);
Utils::invariant($result instanceof Promise);
}
@ -1023,7 +1023,7 @@ class Executor
}
$completedItems[] = $completedItem;
}
return $containsPromise ? $this->promises->createPromiseAll($completedItems) : $completedItems;
return $containsPromise ? $this->promises->all($completedItems) : $completedItems;
}
/**

View File

@ -20,9 +20,9 @@ class ReactPromiseAdapter implements PromiseAdapter
/**
* @inheritdoc
*/
public function convert($promise)
public function convertThenable($thenable)
{
return new Promise($promise, $this);
return new Promise($thenable, $this);
}
/**
@ -38,7 +38,7 @@ class ReactPromiseAdapter implements PromiseAdapter
/**
* @inheritdoc
*/
public function createPromise(callable $resolver)
public function create(callable $resolver)
{
$promise = new ReactPromise($resolver);
return new Promise($promise, $this);
@ -47,7 +47,7 @@ class ReactPromiseAdapter implements PromiseAdapter
/**
* @inheritdoc
*/
public function createResolvedPromise($value = null)
public function createFulfilled($value = null)
{
$promise = \React\Promise\resolve($value);
return new Promise($promise, $this);
@ -56,7 +56,7 @@ class ReactPromiseAdapter implements PromiseAdapter
/**
* @inheritdoc
*/
public function createRejectedPromise(\Exception $reason)
public function createRejected(\Exception $reason)
{
$promise = \React\Promise\reject($reason);
return new Promise($promise, $this);
@ -65,7 +65,7 @@ class ReactPromiseAdapter implements PromiseAdapter
/**
* @inheritdoc
*/
public function createPromiseAll(array $promisesOrValues)
public function all(array $promisesOrValues)
{
// TODO: rework with generators when PHP minimum required version is changed to 5.5+
$promisesOrValues = Utils::map($promisesOrValues, function ($item) {

View File

@ -29,8 +29,8 @@ class SyncPromise
public static function runQueue()
{
$q = self::getQueue();
while (!$q->isEmpty()) {
$q = self::$queue;
while ($q && !$q->isEmpty()) {
$task = $q->dequeue();
$task();
}
@ -38,8 +38,8 @@ class SyncPromise
public static function runNext()
{
$q = self::getQueue();
if (!$q->isEmpty()) {
$q = self::$queue;
if ($q && !$q->isEmpty()) {
$task = $q->dequeue();
$task();
}

View File

@ -28,12 +28,12 @@ class SyncPromiseAdapter implements PromiseAdapter
/**
* @inheritdoc
*/
public function convert($value)
public function convertThenable($thenable)
{
if (!$value instanceof Deferred) {
throw new InvariantViolation('Expected instance of GraphQL\Deferred, got ' . Utils::printSafe($value));
if (!$thenable instanceof Deferred) {
throw new InvariantViolation('Expected instance of GraphQL\Deferred, got ' . Utils::printSafe($thenable));
}
return new Promise($value->promise, $this);
return new Promise($thenable->promise, $this);
}
/**
@ -49,7 +49,7 @@ class SyncPromiseAdapter implements PromiseAdapter
/**
* @inheritdoc
*/
public function createPromise(callable $resolver)
public function create(callable $resolver)
{
$promise = new SyncPromise();
@ -68,7 +68,7 @@ class SyncPromiseAdapter implements PromiseAdapter
/**
* @inheritdoc
*/
public function createResolvedPromise($value = null)
public function createFulfilled($value = null)
{
$promise = new SyncPromise();
return new Promise($promise->resolve($value), $this);
@ -77,7 +77,7 @@ class SyncPromiseAdapter implements PromiseAdapter
/**
* @inheritdoc
*/
public function createRejectedPromise(\Exception $reason)
public function createRejected(\Exception $reason)
{
$promise = new SyncPromise();
return new Promise($promise->reject($reason), $this);
@ -86,7 +86,7 @@ class SyncPromiseAdapter implements PromiseAdapter
/**
* @inheritdoc
*/
public function createPromiseAll(array $promisesOrValues)
public function all(array $promisesOrValues)
{
$all = new SyncPromise();
@ -117,6 +117,12 @@ class SyncPromiseAdapter implements PromiseAdapter
return new Promise($all, $this);
}
/**
* Synchronously wait when promise completes
*
* @param Promise $promise
* @return mixed
*/
public function wait(Promise $promise)
{
$dfdQueue = Deferred::getQueue();

View File

@ -6,18 +6,18 @@ interface PromiseAdapter
/**
* Return true if value is promise of underlying system
*
* @param mixed $value
* @param mixed $value
* @return bool
*/
public function isThenable($value);
/**
* Converts promise of underlying system into Promise instance
* Converts thenable of underlying system into Promise instance
*
* @param $adaptedPromise
* @param object $thenable
* @return Promise
*/
public function convert($adaptedPromise);
public function convertThenable($thenable);
/**
* Accepts our Promise wrapper, extracts adopted promise out of it and executes actual `then` logic described
@ -38,7 +38,7 @@ interface PromiseAdapter
* @return Promise
*/
public function createPromise(callable $resolver);
public function create(callable $resolver);
/**
* Creates a fulfilled Promise for a value if the value is not a promise.
@ -47,7 +47,7 @@ interface PromiseAdapter
*
* @return Promise
*/
public function createResolvedPromise($value = null);
public function createFulfilled($value = null);
/**
* Creates a rejected promise for a reason if the reason is not a promise. If
@ -57,7 +57,7 @@ interface PromiseAdapter
*
* @return Promise
*/
public function createRejectedPromise(\Exception $reason);
public function createRejected(\Exception $reason);
/**
* Given an array of promises (or values), returns a promise that is fulfilled when all the
@ -67,5 +67,5 @@ interface PromiseAdapter
*
* @return Promise
*/
public function createPromiseAll(array $promisesOrValues);
public function all(array $promisesOrValues);
}

View File

@ -35,13 +35,13 @@ class SyncPromiseAdapterTest extends \PHPUnit_Framework_TestCase
public function testConvert()
{
$dfd = new Deferred(function() {});
$result = $this->promises->convert($dfd);
$result = $this->promises->convertThenable($dfd);
$this->assertInstanceOf('GraphQL\Executor\Promise\Promise', $result);
$this->assertInstanceOf('GraphQL\Executor\Promise\Adapter\SyncPromise', $result->adoptedPromise);
try {
$this->promises->convert('');
$this->promises->convertThenable('');
$this->fail('Expected exception no thrown');
} catch (InvariantViolation $e) {
$this->assertEquals('Expected instance of GraphQL\Deferred, got (empty string)', $e->getMessage());
@ -51,7 +51,7 @@ class SyncPromiseAdapterTest extends \PHPUnit_Framework_TestCase
public function testThen()
{
$dfd = new Deferred(function() {});
$promise = $this->promises->convert($dfd);
$promise = $this->promises->convertThenable($dfd);
$result = $this->promises->then($promise);
@ -61,12 +61,12 @@ class SyncPromiseAdapterTest extends \PHPUnit_Framework_TestCase
public function testCreatePromise()
{
$promise = $this->promises->createPromise(function($resolve, $reject) {});
$promise = $this->promises->create(function($resolve, $reject) {});
$this->assertInstanceOf('GraphQL\Executor\Promise\Promise', $promise);
$this->assertInstanceOf('GraphQL\Executor\Promise\Adapter\SyncPromise', $promise->adoptedPromise);
$promise = $this->promises->createPromise(function($resolve, $reject) {
$promise = $this->promises->create(function($resolve, $reject) {
$resolve('A');
});
@ -75,22 +75,22 @@ class SyncPromiseAdapterTest extends \PHPUnit_Framework_TestCase
public function testCreateFulfilledPromise()
{
$promise = $this->promises->createResolvedPromise('test');
$promise = $this->promises->createFulfilled('test');
$this->assertValidPromise($promise, null, 'test', SyncPromise::FULFILLED);
}
public function testCreateRejectedPromise()
{
$promise = $this->promises->createRejectedPromise(new \Exception('test reason'));
$promise = $this->promises->createRejected(new \Exception('test reason'));
$this->assertValidPromise($promise, 'test reason', null, SyncPromise::REJECTED);
}
public function testCreatePromiseAll()
{
$promise = $this->promises->createPromiseAll([]);
$promise = $this->promises->all([]);
$this->assertValidPromise($promise, null, [], SyncPromise::FULFILLED);
$promise = $this->promises->createPromiseAll(['1']);
$promise = $this->promises->all(['1']);
$this->assertValidPromise($promise, null, ['1'], SyncPromise::FULFILLED);
$promise1 = new SyncPromise();
@ -110,7 +110,7 @@ class SyncPromiseAdapterTest extends \PHPUnit_Framework_TestCase
[]
];
$promise = $this->promises->createPromiseAll($data);
$promise = $this->promises->all($data);
$this->assertValidPromise($promise, null, null, SyncPromise::PENDING);
$promise1->resolve('value1');
@ -132,15 +132,15 @@ class SyncPromiseAdapterTest extends \PHPUnit_Framework_TestCase
return 2;
});
$p1 = $this->promises->convert($deferred1);
$p2 = $this->promises->convert($deferred2);
$p1 = $this->promises->convertThenable($deferred1);
$p2 = $this->promises->convertThenable($deferred2);
$p3 = $p2->then(function() use (&$called) {
$dfd = new Deferred(function() use (&$called) {
$called[] = 3;
return 3;
});
return $this->promises->convert($dfd);
return $this->promises->convertThenable($dfd);
});
$p4 = $p3->then(function() use (&$called) {
@ -150,7 +150,7 @@ class SyncPromiseAdapterTest extends \PHPUnit_Framework_TestCase
});
});
$all = $this->promises->createPromiseAll([0, $p1, $p2, $p3, $p4]);
$all = $this->promises->all([0, $p1, $p2, $p3, $p4]);
$result = $this->promises->wait($p2);
$this->assertEquals(2, $result);