1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Executor\Promise\Adapter; |
13
|
|
|
|
14
|
|
|
use GraphQL\Executor\ExecutionResult; |
15
|
|
|
use GraphQL\Executor\Promise\Adapter\ReactPromiseAdapter as BaseReactPromiseAdapter; |
16
|
|
|
use GraphQL\Executor\Promise\Promise; |
17
|
|
|
use Overblog\GraphQLBundle\Executor\Promise\PromiseAdapterInterface; |
18
|
|
|
|
19
|
|
|
class ReactPromiseAdapter extends BaseReactPromiseAdapter implements PromiseAdapterInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
17 |
|
public function isThenable($value) |
25
|
|
|
{ |
26
|
17 |
|
return parent::isThenable($value instanceof Promise ? $value->adoptedPromise : $value); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
10 |
|
public function convertThenable($thenable) |
33
|
|
|
{ |
34
|
10 |
|
if ($thenable instanceof Promise) { |
35
|
|
|
return $thenable; |
36
|
|
|
} |
37
|
|
|
|
38
|
10 |
|
return parent::convertThenable($thenable); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Synchronously wait when promise completes. |
43
|
|
|
* |
44
|
|
|
* @param Promise $promise |
45
|
|
|
* @param callable $onProgress |
46
|
|
|
* |
47
|
|
|
* @return ExecutionResult |
48
|
|
|
* |
49
|
|
|
* @throws \Exception |
50
|
|
|
*/ |
51
|
17 |
|
public function wait(Promise $promise, callable $onProgress = null) |
52
|
|
|
{ |
53
|
17 |
|
if (!$this->isThenable($promise)) { |
54
|
1 |
|
throw new \InvalidArgumentException(sprintf('The "%s" method must be call with compatible a Promise.', __METHOD__)); |
55
|
|
|
} |
56
|
16 |
|
$wait = true; |
57
|
16 |
|
$resolvedValue = null; |
58
|
16 |
|
$exception = null; |
59
|
|
|
/** @var \React\Promise\PromiseInterface $reactPromise */ |
60
|
16 |
|
$reactPromise = $promise->adoptedPromise; |
61
|
|
|
|
62
|
15 |
|
$reactPromise->then(function ($values) use (&$resolvedValue, &$wait) { |
63
|
15 |
|
$resolvedValue = $values; |
64
|
15 |
|
$wait = false; |
65
|
|
|
}, function ($reason) use (&$exception, &$wait) { |
66
|
1 |
|
$exception = $reason; |
67
|
1 |
|
$wait = false; |
68
|
16 |
|
}); |
69
|
|
|
|
70
|
|
|
// wait until promise resolution |
71
|
|
|
while ($wait) { |
72
|
|
|
if (null !== $onProgress) { |
73
|
|
|
$onProgress(); |
74
|
|
|
} |
75
|
|
|
// less CPU intensive without sacrificing the performance |
76
|
|
|
usleep(5); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** @var \Exception|null $exception */ |
80
|
|
|
if (null !== $exception) { |
81
|
|
|
throw $exception; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $resolvedValue; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|