diff --git a/composer.json b/composer.json index dc06ffc..d9eec64 100644 --- a/composer.json +++ b/composer.json @@ -1,17 +1,18 @@ { - "name": "mcbeany/libasync", - "description": "Stupid async implementation using await-generator", - "type": "library", - "license": "GPL-3.0-only", - "require": { - "sof3/await-generator": "^3.4" - }, - "require-dev": { - "pocketmine/pocketmine-mp": "^4.5" - }, - "autoload": { - "psr-0": { - "Mcbeany\\libAsync": "src/" - } - } + "name": "mcbeany/libasync", + "version": "0.0.1", + "description": "Stupid async implementation using await-generator", + "type": "library", + "license": "GPL-3.0-only", + "require": { + "sof3/await-generator": "^3.4" + }, + "require-dev": { + "pocketmine/pocketmine-mp": "^4.5" + }, + "autoload": { + "psr-0": { + "Mcbeany\\libAsync": "src/" + } + } } diff --git a/src/Mcbeany/libAsync/libAsync.php b/src/Mcbeany/libAsync/libAsync.php index bdd67fe..3cc91d3 100644 --- a/src/Mcbeany/libAsync/libAsync.php +++ b/src/Mcbeany/libAsync/libAsync.php @@ -12,53 +12,53 @@ use Throwable; final class libAsync { - /** - * Call a function asynchronously. - * - * @template T - * @param Closure(): T @executor - * - * @return Generator - */ - public static function doAsync(Closure $executor): Generator { - $closure = function (Closure $resolve, Closure $reject) use ($executor): void { - $task = new class($executor, $resolve, $reject) extends AsyncTask { - private bool $rejected = false; - public function __construct( - private Closure $executor, - Closure $resolve, - Closure $reject - ) { - $this->storeLocal('resolve', $resolve); - $this->storeLocal('reject', $reject); - } + /** + * Call a function asynchronously. + * + * @template T + * @param Closure(): T $executor + * + * @return Generator + */ + public static function doAsync(Closure $executor): Generator { + $resolve = yield Await::RESOLVE; + $reject = yield Await::REJECT; + $task = new class($executor, $resolve, $reject) extends AsyncTask { + private bool $rejected = false; + public function __construct( + private Closure $executor, + Closure $resolve, + Closure $reject + ) { + $this->storeLocal('resolve', $resolve); + $this->storeLocal('reject', $reject); + } - public function onRun(): void { - try { - $result = ($this->executor)(); - } catch (Throwable $e) { - $this->rejected = true; - $result = $e; - } - $this->setResult($result); - } + public function onRun(): void { + try { + $result = ($this->executor)(); + } catch (Throwable $e) { + $this->rejected = true; + $result = $e; + } + $this->setResult($result); + } - public function onCompletion(): void { - /** @var T|Throwable $result */ - $result = $this->getResult(); - if ($this->rejected && $result instanceof Throwable) { - /** @var Closure(Throwable): void */ - $reject = $this->fetchLocal('reject'); - $reject($result); - return; - } - /** @var Closure(T): void $resolve */ - $resolve = $this->fetchLocal('resolve'); - $resolve($result); - } - }; - Server::getInstance()->getAsyncPool()->submitTask($task); - }; - return yield from Await::promise($closure); - } + public function onCompletion(): void { + /** @var T|Throwable $result */ + $result = $this->getResult(); + if ($this->rejected && $result instanceof Throwable) { + /** @var Closure(Throwable): void */ + $reject = $this->fetchLocal('reject'); + $reject($result); + return; + } + /** @var Closure(T): void $resolve */ + $resolve = $this->fetchLocal('resolve'); + $resolve($result); + } + }; + Server::getInstance()->getAsyncPool()->submitTask($task); + return yield Await::ONCE; + } }