Skip to content

Commit

Permalink
update some methods for short
Browse files Browse the repository at this point in the history
  • Loading branch information
VennDev authored Aug 30, 2024
1 parent 0b4df6e commit 2f6c499
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 14 deletions.
27 changes: 14 additions & 13 deletions src/vennv/vapm/Async.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
122 changes: 122 additions & 0 deletions src/vennv/vapm/ct/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

/**
* Vapm - A library support for PHP about Async, Promise, Coroutine, Thread, GreenThread
* and other non-blocking methods. The library also includes some Javascript packages
* such as Express. The method is based on Fibers & Generator & Processes, requires
* you to have php version from >= 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);
}
93 changes: 93 additions & 0 deletions src/vennv/vapm/io/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/**
* Vapm - A library support for PHP about Async, Promise, Coroutine, Thread, GreenThread
* and other non-blocking methods. The library also includes some Javascript packages
* such as Express. The method is based on Fibers & Generator & Processes, requires
* you to have php version from >= 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);
}
2 changes: 1 addition & 1 deletion src/vennv/vapm/utils/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use ReflectionException;
use ReflectionFunction;
use SplFileInfo;
use Throwable;
use function array_slice;
use function file;
use function implode;
Expand Down Expand Up @@ -321,7 +322,6 @@ public static function isClass(string $class): bool
return true; // This is a class
}
}

return false;
}

Expand Down

0 comments on commit 2f6c499

Please sign in to comment.