diff options
Diffstat (limited to 'vendor/sabre/event/lib/coroutine.php')
-rw-r--r-- | vendor/sabre/event/lib/coroutine.php | 52 |
1 files changed, 23 insertions, 29 deletions
diff --git a/vendor/sabre/event/lib/coroutine.php b/vendor/sabre/event/lib/coroutine.php index 750e8ab52..a6a2baf41 100644 --- a/vendor/sabre/event/lib/coroutine.php +++ b/vendor/sabre/event/lib/coroutine.php @@ -1,4 +1,6 @@ -<?php declare (strict_types=1); +<?php + +declare(strict_types=1); namespace Sabre\Event; @@ -41,12 +43,13 @@ use Throwable; * }); * * @return \Sabre\Event\Promise + * * @copyright Copyright (C) fruux GmbH (https://fruux.com/) * @author Evert Pot (http://evertpot.com/) * @license http://sabre.io/license/ Modified BSD License */ -function coroutine(callable $gen) : Promise { - +function coroutine(callable $gen): Promise +{ $generator = $gen(); if (!$generator instanceof Generator) { throw new \InvalidArgumentException('You must pass a generator function'); @@ -59,22 +62,20 @@ function coroutine(callable $gen) : Promise { * So tempted to use the mythical y-combinator here, but it's not needed in * PHP. */ - $advanceGenerator = function() use (&$advanceGenerator, $generator, $promise, &$lastYieldResult) { - + $advanceGenerator = function () use (&$advanceGenerator, $generator, $promise) { while ($generator->valid()) { - $yieldedValue = $generator->current(); if ($yieldedValue instanceof Promise) { $yieldedValue->then( - function($value) use ($generator, &$advanceGenerator, &$lastYieldResult) { + function ($value) use ($generator, &$advanceGenerator) { $generator->send($value); $advanceGenerator(); }, - function(Throwable $reason) use ($generator, $advanceGenerator) { + function (Throwable $reason) use ($generator, $advanceGenerator) { $generator->throw($reason); $advanceGenerator(); } - )->otherwise(function(Throwable $reason) use ($promise) { + )->otherwise(function (Throwable $reason) use ($promise) { // This error handler would be called, if something in the // generator throws an exception, and it's not caught // locally. @@ -87,31 +88,25 @@ function coroutine(callable $gen) : Promise { // If the value was not a promise, we'll just let it pass through. $generator->send($yieldedValue); } - } // If the generator is at the end, and we didn't run into an exception, // We're grabbing the "return" value and fulfilling our top-level // promise with its value. - if (!$generator->valid() && $promise->state === Promise::PENDING) { - $returnValue = $generator->getReturn(); - - // The return value is a promise. - if ($returnValue instanceof Promise) { - $returnValue->then(function($value) use ($promise) { - $promise->fulfill($value); - }, function(Throwable $reason) { - $promise->reject($reason); - }); - } else { - - $promise->fulfill($returnValue); - - } - - + if (!$generator->valid() && Promise::PENDING === $promise->state) { + $returnValue = $generator->getReturn(); + + // The return value is a promise. + if ($returnValue instanceof Promise) { + $returnValue->then(function ($value) use ($promise) { + $promise->fulfill($value); + }, function (Throwable $reason) use ($promise) { + $promise->reject($reason); + }); + } else { + $promise->fulfill($returnValue); + } } - }; try { @@ -121,5 +116,4 @@ function coroutine(callable $gen) : Promise { } return $promise; - } |