-
Notifications
You must be signed in to change notification settings - Fork 20
/
WebonyxGraphQLSyncPromiseAdapter.php
166 lines (146 loc) · 4.46 KB
/
WebonyxGraphQLSyncPromiseAdapter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/*
* This file is part of the DataLoaderPhp package.
*
* (c) Overblog <http://github.com/overblog/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Overblog\PromiseAdapter\Adapter;
use GraphQL\Deferred;
use GraphQL\Executor\Promise\Adapter\SyncPromise;
use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter;
use GraphQL\Executor\Promise\Promise;
use Overblog\PromiseAdapter\PromiseAdapterInterface;
/**
* @implements PromiseAdapterInterface<Promise>
*/
class WebonyxGraphQLSyncPromiseAdapter implements PromiseAdapterInterface
{
/** @var callable[] */
private $cancellers = [];
/**
* @var SyncPromiseAdapter
*/
private $webonyxPromiseAdapter;
public function __construct(SyncPromiseAdapter $webonyxPromiseAdapter = null)
{
$webonyxPromiseAdapter = $webonyxPromiseAdapter?:new SyncPromiseAdapter();
$this->setWebonyxPromiseAdapter($webonyxPromiseAdapter);
}
/**
* @return SyncPromiseAdapter
*/
public function getWebonyxPromiseAdapter()
{
return $this->webonyxPromiseAdapter;
}
/**
* @param SyncPromiseAdapter $webonyxPromiseAdapter
*/
public function setWebonyxPromiseAdapter(SyncPromiseAdapter $webonyxPromiseAdapter)
{
$this->webonyxPromiseAdapter = $webonyxPromiseAdapter;
}
/**
* {@inheritdoc}
*/
public function create(&$resolve = null, &$reject = null, callable $canceller = null)
{
$promise = $this->webonyxPromiseAdapter->create(function ($res, $rej) use (&$resolve, &$reject) {
$resolve = $res;
$reject = $rej;
});
$this->cancellers[spl_object_hash($promise)] = $canceller;
return $promise;
}
/**
* {@inheritdoc}
*/
public function createFulfilled($promiseOrValue = null)
{
return $this->getWebonyxPromiseAdapter()->createFulfilled($promiseOrValue);
}
/**
* {@inheritdoc}
*/
public function createRejected($reason)
{
return $this->getWebonyxPromiseAdapter()->createRejected($reason);
}
/**
* {@inheritdoc}
*/
public function createAll($promisesOrValues)
{
return $this->getWebonyxPromiseAdapter()->all($promisesOrValues);
}
/**
* {@inheritdoc}
*/
public function isPromise($value, $strict = false)
{
if ($value instanceof Promise) {
$value = $value->adoptedPromise;
}
$isStrictPromise = $value instanceof SyncPromise;
if ($strict) {
return $isStrictPromise;
}
return $isStrictPromise || is_callable([$value, 'then']);
}
/**
* {@inheritdoc}
*/
public function await($promise = null, $unwrap = false)
{
if (null === $promise) {
Deferred::runQueue();
SyncPromise::runQueue();
$this->cancellers = [];
return null;
}
$promiseAdapter = $this->getWebonyxPromiseAdapter();
$resolvedValue = null;
$exception = null;
if (!$this->isPromise($promise)) {
throw new \InvalidArgumentException(sprintf('The "%s" method must be called with a Promise ("then" method).', __METHOD__));
}
try {
$resolvedValue = $promiseAdapter->wait($promise);
} catch (\Exception $reason) {
$exception = $reason;
}
if ($exception instanceof \Exception) {
if (!$unwrap) {
return $exception;
}
throw $exception;
}
$hash = spl_object_hash($promise);
unset($this->cancellers[$hash]);
return $resolvedValue;
}
/**
* {@inheritdoc}
*/
public function cancel($promise)
{
$hash = spl_object_hash($promise);
if (!$this->isPromise($promise) || !isset($this->cancellers[$hash])) {
throw new \InvalidArgumentException(sprintf('The "%s" method must be called with a compatible Promise.', __METHOD__));
}
$canceller = $this->cancellers[$hash];
unset($this->cancellers[$hash]);
$adoptedPromise = $promise;
if ($promise instanceof Promise) {
$adoptedPromise = $promise->adoptedPromise;
}
try {
$canceller([$adoptedPromise, 'resolve'], [$adoptedPromise, 'reject']);
} catch (\Exception $reason) {
$adoptedPromise->reject($reason);
}
}
}