@@ -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 * ]);
@@ -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 }
@@ -310,22 +296,6 @@ private function checkKey($key, $method)
310296 }
311297 }
312298
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-
329299 /**
330300 * Given the current state of a Loader instance, perform a batch load
331301 * from its current queue.
0 commit comments