update to PHP-8.1
[m6w6/seekat] / lib / API / Consumer.php
index 926eb736e756f066d61d9b7db1b497dfa0b18a4e..8db2b4d614c86993c0e887a14cbe3eda039a1fdc 100644 (file)
@@ -2,73 +2,43 @@
 
 namespace seekat\API;
 
+use Exception;
 use Generator;
-use http\Client;
 use seekat\API;
-use seekat\Exception\{
-       InvalidArgumentException, UnexpectedValueException, function exception
-};
-
-final class Consumer
-{
-       /**
-        * Loop
-        * @var callable
-        */
-       private $loop;
+use seekat\Exception\{function exception};
 
+final class Consumer {
        /**
         * The return value of the generator
         * @var mixed
         */
-       private $result;
+       private mixed $result = null;
 
        /**
         * Cancellation flag
-        * @var bool
-        */
-       private $cancelled = false;
-
-       /**
-        * @var Future
-        */
-       private $future;
-
-       /**
-        * @var mixed
         */
-       private $context;
+       private bool $cancelled = false;
 
        /**
-        * @var \Closure
+        * Promise
         */
-       private $resolve;
+       private object $promise;
 
-       /**
-        * @var \Closure
-        */
-       private $reject;
-
-       /**
-        * @var \Closure
-        */
-       private $reduce;
+       private \Closure $resolve;
+       private \Closure $reject;
+       private \Closure $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() {
+       function __construct(private readonly Future $future, private readonly \Closure $loop) {
+               $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);
+               $this->promise = $future->getPromise($context);
+               $this->resolve = $future->resolver($context);
+               $this->reject = $future->rejecter($context);
+               $this->reduce = $future->reducer();
        }
 
        /**
@@ -77,9 +47,8 @@ final class Consumer
         * @param Generator $gen
         * @return mixed promise
         */
-       function __invoke(Generator $gen) {
+       function __invoke(Generator $gen) : mixed {
                $this->cancelled = false;
-
                foreach ($gen as $promise) {
                        if ($this->cancelled) {
                                break;
@@ -87,18 +56,18 @@ final class Consumer
                        $this->give($promise, $gen);
                }
 
-               #($this->loop)();
-
-               if (!$this->cancelled) {
-                       $this->result = $gen->getReturn();
-               }
-               if (isset($this->result)) {
-                       ($this->resolve)($this->result);
-               } else {
+               if ($this->cancelled) {
                        ($this->reject)("Cancelled");
+               } else if (!$gen->valid()) {
+                       try {
+                               $this->result = $gen->getReturn();
+                       } catch (Exception $e) {
+                               assert($e->getMessage() === "Cannot get return value of a generator that hasn't returned");
+                       }
                }
+               ($this->resolve)($this->result);
 
-               return $this->context->promise();
+               return $this->promise;
        }
 
        /**
@@ -107,7 +76,7 @@ final class Consumer
         * @param mixed $promise
         * @param Generator $gen
         */
-       private function give($promise, Generator $gen) {
+       private function give(mixed $promise, Generator $gen) : void {
                if ($promise instanceof \Traversable) {
                        $promise = iterator_to_array($promise);
                }