From 2f6c499140ba1155757596f5194545ef4fea9a8e Mon Sep 17 00:00:00 2001 From: VennV <111500380+VennDev@users.noreply.github.com> Date: Sat, 31 Aug 2024 02:11:44 +0700 Subject: [PATCH] update some methods for short --- src/vennv/vapm/Async.php | 27 +++---- src/vennv/vapm/ct/functions.php | 122 ++++++++++++++++++++++++++++++++ src/vennv/vapm/io/functions.php | 93 ++++++++++++++++++++++++ src/vennv/vapm/utils/Utils.php | 2 +- 4 files changed, 230 insertions(+), 14 deletions(-) create mode 100644 src/vennv/vapm/ct/functions.php create mode 100644 src/vennv/vapm/io/functions.php diff --git a/src/vennv/vapm/Async.php b/src/vennv/vapm/Async.php index f81a08dd..335aa87e 100644 --- a/src/vennv/vapm/Async.php +++ b/src/vennv/vapm/Async.php @@ -64,24 +64,25 @@ public function getId(): int */ public static function await(mixed $await): mixed { - if (!Utils::isClass(Async::class)) throw new RuntimeException(Error::ASYNC_AWAIT_MUST_CALL_IN_ASYNC_FUNCTION); - - $result = $await; - - if (is_callable($await)) $await = new Async($await); + if (!$await instanceof Promise && !$await instanceof Async) { + if (is_callable($await)) { + $await = new Async($await); + } else { + if (!Utils::isClass(Async::class)) { + throw new RuntimeException(Error::ASYNC_AWAIT_MUST_CALL_IN_ASYNC_FUNCTION); + } + return $await; + } + } - if ($await instanceof Promise || $await instanceof Async) { + do { $return = EventLoop::getReturn($await->getId()); - - while ($return === null) { - $return = EventLoop::getReturn($await->getId()); + if ($return === null) { FiberManager::wait(); } + } while ($return === null); - if ($return instanceof Promise) $result = $return->getResult(); - } - - return $result; + return $return->getResult(); } } \ No newline at end of file diff --git a/src/vennv/vapm/ct/functions.php b/src/vennv/vapm/ct/functions.php new file mode 100644 index 00000000..21edc87b --- /dev/null +++ b/src/vennv/vapm/ct/functions.php @@ -0,0 +1,122 @@ += 8.1 + * + * Copyright (C) 2023 VennDev + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +declare(strict_types=1); + +namespace vennv\vapm\ct; + +use Generator; +use Closure; +use vennv\vapm\CoroutineGen; +use vennv\vapm\Deferred; +use vennv\vapm\Channel; +use vennv\vapm\AwaitGroup; +use vennv\vapm\Mutex; + +/** + * This file is used to create a coroutine with non-blocking mode + */ + +/** + * @param callable ...$callbacks + * @return void + * + * This function is used to create a coroutine with non-blocking mode + */ +function c(callable ...$callbacks): void +{ + CoroutineGen::runNonBlocking(...$callbacks); +} + +/** + * @param callable ...$callbacks + * @return void + * + * This function is used to create a coroutine with blocking mode + */ +function cBlock(callable ...$callbacks): void +{ + CoroutineGen::runBlocking(...$callbacks); +} + +/** + * @param int $milliseconds + * @return Generator + * + * This function is used to delay the execution of a coroutine + */ +function cDelay(int $milliseconds): Generator +{ + return CoroutineGen::delay($milliseconds); +} + +/** + * @param callable $callback + * @param int $times + * @return Closure + * + * This function is used to repeat the execution of a coroutine + */ +function cRepeat(callable $callback, int $times): Closure +{ + return CoroutineGen::repeat($callback, $times); +} + +/** + * @return Channel + * + * This function is used to create a channel + */ +function channel(): Channel +{ + return new Channel(); +} + +/** + * @return AwaitGroup + * + * This function is used to create a await group + */ +function awaitGroup(): AwaitGroup +{ + return new AwaitGroup(); +} + +/** + * @return Mutex + * + * This function is used to create a mutex + */ +function mutex(): Mutex +{ + return new Mutex(); +} + +/** + * @param callable $callback + * @return Deferred + * + * This function is used to create a deferred + */ +function deferred(callable $callback): Deferred +{ + return new Deferred($callback); +} \ No newline at end of file diff --git a/src/vennv/vapm/io/functions.php b/src/vennv/vapm/io/functions.php new file mode 100644 index 00000000..a64b8b97 --- /dev/null +++ b/src/vennv/vapm/io/functions.php @@ -0,0 +1,93 @@ += 8.1 + * + * Copyright (C) 2023 VennDev + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +declare(strict_types=1); + +namespace vennv\vapm\io; + +use vennv\vapm\Async; +use vennv\vapm\Promise; +use vennv\vapm\System; + +/** + * This functions is used to handle the IO operations + */ + +/** + * @param callable $callback + * @return Async + * + * This function is used to run asynchronous callbacks + */ +function async(callable $callback): Async +{ + return new Async($callback); +} + +/** + * @param mixed $await + * @return mixed + * + * This function is used to wait for a callback to be executed + */ +function await(mixed $await): mixed +{ + return Async::await($await); +} + +/** + * @param int $milliseconds + * @return Promise + * + * This function is used to delay the execution of a callback + */ +function delay(int $milliseconds): Promise +{ + return new Promise(function($resolve) use ($milliseconds) { + System::setTimeout(function() use ($resolve) { + $resolve(); + }, $milliseconds); + }); +} + +/** + * @param callable $callback + * @param int $milliseconds + * @return void + * + * This function is used to set a timeout for a callback + */ +function setTimeout(callable $callback, int $milliseconds): void +{ + System::setTimeout($callback, $milliseconds); +} + +/** + * @param callable $callback + * @param int $milliseconds + * @return void + * + * This function is used to set an interval for a callback + */ +function setInterval(callable $callback, int $milliseconds): void +{ + System::setInterval($callback, $milliseconds); +} \ No newline at end of file diff --git a/src/vennv/vapm/utils/Utils.php b/src/vennv/vapm/utils/Utils.php index 9053fc4a..b8f17b7d 100644 --- a/src/vennv/vapm/utils/Utils.php +++ b/src/vennv/vapm/utils/Utils.php @@ -31,6 +31,7 @@ use ReflectionException; use ReflectionFunction; use SplFileInfo; +use Throwable; use function array_slice; use function file; use function implode; @@ -321,7 +322,6 @@ public static function isClass(string $class): bool return true; // This is a class } } - return false; }