1616class DataLoader
1717{
1818 /**
19- * @var BatchLoadFn
19+ * @var callable
2020 */
2121 private $ batchLoadFn ;
2222
@@ -35,26 +35,15 @@ class DataLoader
3535 */
3636 private $ queue = [];
3737
38- /**
39- * @var null|\React\EventLoop\LoopInterface
40- */
41- private $ eventLoop ;
42-
43- /**
44- * @var Promise
45- */
46- private $ resolvedPromise ;
47-
4838 /**
4939 * @var self[]
5040 */
5141 private static $ instances = [];
5242
53- public function __construct (BatchLoadFn $ batchLoadFn , Option $ options = null )
43+ public function __construct (callable $ batchLoadFn , Option $ options = null )
5444 {
5545 $ this ->batchLoadFn = $ batchLoadFn ;
5646 $ this ->options = $ options ?: new Option ();
57- $ this ->eventLoop = class_exists ('React \\EventLoop \\Factory ' ) ? \React \EventLoop \Factory::create () : null ;
5847 $ this ->promiseCache = $ this ->options ->getCacheMap ();
5948 self ::$ instances [] = $ this ;
6049 }
@@ -97,12 +86,7 @@ function ($resolve, $reject) use (&$promise, $key, $shouldBatch) {
9786 // A single dispatch should be scheduled per queue at the time when the
9887 // queue changes from "empty" to "full".
9988 if (count ($ this ->queue ) === 1 ) {
100- if ($ shouldBatch ) {
101- // If batching, schedule a task to dispatch the queue.
102- $ this ->enqueuePostPromiseJob (function () {
103- $ this ->dispatchQueue ();
104- });
105- } else {
89+ if (!$ shouldBatch ) {
10690 // Otherwise dispatch the (queue of one) immediately.
10791 $ this ->dispatchQueue ();
10892 }
@@ -124,11 +108,11 @@ function (callable $resolve, callable $reject) {
124108 /**
125109 * Loads multiple keys, promising an array of values:
126110 *
127- * [ $a, $b] = $myLoader->loadMany(['a', 'b']);
111+ * list( $a, $b) = $myLoader->loadMany(['a', 'b']);
128112 *
129113 * This is equivalent to the more verbose:
130114 *
131- * [ $a, $b] = \React\Promise\all([
115+ * list( $a, $b) = \React\Promise\all([
132116 * $myLoader->load('a'),
133117 * $myLoader->load('b')
134118 * ]);
@@ -139,7 +123,7 @@ function (callable $resolve, callable $reject) {
139123 public function loadMany ($ keys )
140124 {
141125 if (!is_array ($ keys ) && !$ keys instanceof \Traversable) {
142- throw new \InvalidArgumentException (sprintf ('The %s function must be called with Array<key> but got: %s. ' , __METHOD__ , gettype ($ keys )));
126+ throw new \InvalidArgumentException (sprintf ('The "%s" method must be called with Array<key> but got: %s. ' , __METHOD__ , gettype ($ keys )));
143127 }
144128 return \React \Promise \all (array_map (
145129 function ($ key ) {
@@ -207,7 +191,9 @@ public function __destruct()
207191 if ($ this ->needProcess ()) {
208192 foreach ($ this ->queue as $ data ) {
209193 try {
210- $ data ['promise ' ]->cancel ();
194+ /** @var Promise $promise */
195+ $ promise = $ data ['promise ' ];
196+ $ promise ->cancel ();
211197 } catch (\Exception $ e ) {
212198 // no need to do nothing if cancel failed
213199 }
@@ -243,51 +229,45 @@ public static function await($promise = null, $unwrap = true)
243229 {
244230 self ::awaitInstances ();
245231
246- if (null !== $ promise ) {
247- $ resolvedValue = null ;
248- $ exception = null ;
232+ if (null === $ promise ) {
233+ return null ;
234+ }
235+ $ resolvedValue = null ;
236+ $ exception = null ;
249237
250- if (!is_callable ([$ promise , 'then ' ])) {
251- throw new \InvalidArgumentException (' Promise must have a "then" method. ' );
252- }
238+ if (!is_callable ([$ promise , 'then ' ])) {
239+ throw new \InvalidArgumentException (sprintf ( ' The "%s" method must be called with a Promise ( "then" method). ' , __METHOD__ ) );
240+ }
253241
254- $ promise ->then (function ($ values ) use (&$ resolvedValue ) {
255- $ resolvedValue = $ values ;
256- }, function ($ reason ) use (&$ exception ) {
257- $ exception = $ reason ;
258- });
259- if ($ exception instanceof \Exception) {
260- if (!$ unwrap ) {
261- return $ exception ;
262- }
263- throw $ exception ;
242+ $ promise ->then (function ($ values ) use (&$ resolvedValue ) {
243+ $ resolvedValue = $ values ;
244+ }, function ($ reason ) use (&$ exception ) {
245+ $ exception = $ reason ;
246+ });
247+ if ($ exception instanceof \Exception) {
248+ if (!$ unwrap ) {
249+ return $ exception ;
264250 }
265-
266- return $ resolvedValue ;
251+ throw $ exception ;
267252 }
253+
254+ return $ resolvedValue ;
268255 }
269256
270257 private static function awaitInstances ()
271258 {
272259 $ dataLoaders = self ::$ instances ;
273- if (empty ($ dataLoaders )) {
274- return ;
275- }
260+ if (!empty ($ dataLoaders )) {
261+ $ wait = true ;
276262
277- $ wait = true ;
278-
279- while ($ wait ) {
280- foreach ($ dataLoaders as $ dataLoader ) {
281- try {
263+ while ($ wait ) {
264+ foreach ($ dataLoaders as $ dataLoader ) {
282265 if (!$ dataLoader || !$ dataLoader ->needProcess ()) {
283266 $ wait = false ;
284267 continue ;
285268 }
286269 $ wait = true ;
287270 $ dataLoader ->process ();
288- } catch (\Exception $ e ) {
289- $ wait = false ;
290- continue ;
291271 }
292272 }
293273 }
@@ -305,27 +285,11 @@ private function checkKey($key, $method)
305285 {
306286 if (null === $ key ) {
307287 throw new \InvalidArgumentException (
308- sprintf ('The %s function must be called with a value, but got: %s. ' , $ method , gettype ($ key ))
288+ sprintf ('The "%s" method must be called with a value, but got: %s. ' , $ method , gettype ($ key ))
309289 );
310290 }
311291 }
312292
313- /**
314- * @param $fn
315- */
316- private function enqueuePostPromiseJob ($ fn )
317- {
318- if (!$ this ->resolvedPromise ) {
319- $ this ->resolvedPromise = \React \Promise \resolve ();
320- }
321-
322- if ($ this ->eventLoop ) {
323- $ this ->resolvedPromise ->then (function () use ($ fn ) {
324- $ this ->eventLoop ->nextTick ($ fn );
325- });
326- }
327- }
328-
329293 /**
330294 * Given the current state of a Loader instance, perform a batch load
331295 * from its current queue.
0 commit comments