update to PHP-8.1
[m6w6/seekat] / lib / API / Consumer.php
index 3af614caa9355335c70278f36395f2f5449f8d17..8db2b4d614c86993c0e887a14cbe3eda039a1fdc 100644 (file)
@@ -8,64 +8,37 @@ use seekat\API;
 use seekat\Exception\{function exception};
 
 final class Consumer {
-       /**
-        * Loop
-        * @var callable
-        */
-       private $loop;
-
        /**
         * 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;
-
-       /**
-        * @var \Closure
         */
-       private $resolve;
+       private bool $cancelled = false;
 
        /**
-        * @var \Closure
+        * Promise
         */
-       private $reject;
+       private object $promise;
 
-       /**
-        * @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();
        }
 
        /**
@@ -74,7 +47,7 @@ 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) {
@@ -85,15 +58,16 @@ final class Consumer {
 
                if ($this->cancelled) {
                        ($this->reject)("Cancelled");
-               } else {
+               } 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);
                }
+               ($this->resolve)($this->result);
 
-               return $this->context->promise();
+               return $this->promise;
        }
 
        /**
@@ -102,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);
                }