drop async-interop
[m6w6/seekat] / lib / API / Consumer.php
index 5e72ef2a0aa91f9e362e44c0dcf52c9af474dcfe..926eb736e756f066d61d9b7db1b497dfa0b18a4e 100644 (file)
@@ -4,20 +4,18 @@ namespace seekat\API;
 
 use Generator;
 use http\Client;
-use React\Promise\{
-       Deferred,
-       ExtendedPromiseInterface,
-       PromiseInterface,
-       function all
+use seekat\API;
+use seekat\Exception\{
+       InvalidArgumentException, UnexpectedValueException, function exception
 };
 
-class Consumer extends Deferred
+final class Consumer
 {
        /**
-        * The HTTP client
-        * @var Client
+        * Loop
+        * @var callable
         */
-       private $client;
+       private $loop;
 
        /**
         * The return value of the generator
@@ -32,24 +30,54 @@ class Consumer extends Deferred
        private $cancelled = false;
 
        /**
-        * Create a new generator invoker
-        * @param Client $client
+        * @var Future
         */
-       function __construct(Client $client) {
-               $this->client = $client;
+       private $future;
 
-               parent::__construct(function($resolve, $reject) {
-                       return $this->cancel($resolve, $reject);
+       /**
+        * @var mixed
+        */
+       private $context;
+
+       /**
+        * @var \Closure
+        */
+       private $resolve;
+
+       /**
+        * @var \Closure
+        */
+       private $reject;
+
+       /**
+        * @var \Closure
+        */
+       private $reduce;
+
+       /**
+        * Create a new generator consumer
+        * @param Future $future
+        * @param callable $loop
+        */
+       function __construct(Future $future, callable $loop) {
+               $this->loop = $loop;
+
+               $this->future = $future;
+               $this->context = $future->createContext(function() {
+                       $this->cancelled = true;
                });
+               $this->resolve = API\Future\resolver($future, $this->context);
+               $this->reject = API\Future\rejecter($future, $this->context);
+               $this->reduce = API\Future\reducer($future, $this->context);
        }
 
        /**
         * Iterate over $gen, a \Generator yielding promises
         *
         * @param Generator $gen
-        * @return ExtendedPromiseInterface
+        * @return mixed promise
         */
-       function __invoke(Generator $gen) : ExtendedPromiseInterface {
+       function __invoke(Generator $gen) {
                $this->cancelled = false;
 
                foreach ($gen as $promise) {
@@ -59,50 +87,43 @@ class Consumer extends Deferred
                        $this->give($promise, $gen);
                }
 
+               #($this->loop)();
+
                if (!$this->cancelled) {
-                       $this->resolve($this->result = $gen->getReturn());
+                       $this->result = $gen->getReturn();
+               }
+               if (isset($this->result)) {
+                       ($this->resolve)($this->result);
+               } else {
+                       ($this->reject)("Cancelled");
                }
 
-               return $this->promise();
+               return $this->context->promise();
        }
 
        /**
         * Promise handler
         *
-        * @param array|PromiseInterface $promise
+        * @param mixed $promise
         * @param Generator $gen
         */
        private function give($promise, Generator $gen) {
-               if ($promise instanceof PromiseInterface) {
-                       $promise->then(function($result) use($gen) {
-                               if (($promise = $gen->send($result))) {
-                                       $this->give($promise, $gen);
-                               }
-                       });
-               } else {
-                       all($promise)->then(function($results) use($gen) {
-                               if (($promise = $gen->send($results))) {
-                                       $this->give($promise, $gen);
-                               }
-                       });
+               if ($promise instanceof \Traversable) {
+                       $promise = iterator_to_array($promise);
+               }
+               if (is_array($promise)) {
+                       $promise = ($this->reduce)($promise);
                }
-               $this->client->send();
-       }
 
-       /**
-        * Cancellation callback
-        *
-        * @param callable $resolve
-        * @param callable $reject
-        */
-       private function cancel(callable $resolve, callable $reject) {
-               $this->cancelled = true;
+               $this->future->handlePromise($promise, function($result) use($gen) {
+                       if (($promise = $gen->send($result))) {
+                               $this->give($promise, $gen);
+                       }
+               }, function($error) use($gen) {
+                       $gen->throw(exception($error));
+               });
 
-               /* did we finish in the meantime? */
-               if ($this->result) {
-                       $resolve($this->result);
-               } else {
-                       $reject("Cancelled");
-               }
+               /* FIXME: external loop */
+               ($this->loop)();
        }
 }