diff options
Diffstat (limited to 'vendor/sabre/event/lib/coroutine.php')
-rw-r--r-- | vendor/sabre/event/lib/coroutine.php | 52 |
1 files changed, 29 insertions, 23 deletions
diff --git a/vendor/sabre/event/lib/coroutine.php b/vendor/sabre/event/lib/coroutine.php index a6a2baf41..750e8ab52 100644 --- a/vendor/sabre/event/lib/coroutine.php +++ b/vendor/sabre/event/lib/coroutine.php @@ -1,6 +1,4 @@ -<?php - -declare(strict_types=1); +<?php declare (strict_types=1); namespace Sabre\Event; @@ -43,13 +41,12 @@ 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'); @@ -62,20 +59,22 @@ 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) { + $advanceGenerator = function() use (&$advanceGenerator, $generator, $promise, &$lastYieldResult) { + while ($generator->valid()) { + $yieldedValue = $generator->current(); if ($yieldedValue instanceof Promise) { $yieldedValue->then( - function ($value) use ($generator, &$advanceGenerator) { + function($value) use ($generator, &$advanceGenerator, &$lastYieldResult) { $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. @@ -88,25 +87,31 @@ 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::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); - } + 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); + + } + + } + }; try { @@ -116,4 +121,5 @@ function coroutine(callable $gen): Promise } return $promise; + } |