Skip to content

Commit dfc641b

Browse files
authored
Update!
1 parent f358b7f commit dfc641b

36 files changed

+1362
-874
lines changed

src/vennv/vapm/AssumptionFailedError.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@
1919
* GNU General Public License for more details.
2020
*/
2121

22-
declare(strict_types = 1);
22+
declare(strict_types=1);
2323

2424
namespace vennv\vapm;
2525

2626
use TypeError;
2727

28-
final class AssumptionFailedError extends TypeError {
28+
final class AssumptionFailedError extends TypeError
29+
{
2930

3031
public function __construct(
3132
protected string $errorMessage,
3233
protected int $errorCode = 0
33-
) {
34-
parent::__construct(
35-
$this->errorMessage,
36-
$this->errorCode
37-
);
34+
)
35+
{
36+
parent::__construct($this->errorMessage, $this->errorCode);
3837
}
3938

40-
public function __toString() : string {
39+
public function __toString(): string
40+
{
4141
return __CLASS__ . ": [$this->errorCode]: $this->errorMessage\n";
4242
}
4343

src/vennv/vapm/Async.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,49 +19,52 @@
1919
* GNU General Public License for more details.
2020
*/
2121

22-
declare(strict_types = 1);
22+
declare(strict_types=1);
2323

2424
namespace vennv\vapm;
2525

2626
use Throwable;
2727
use function is_callable;
2828

29-
interface AsyncInterface {
29+
interface AsyncInterface
30+
{
3031

31-
public function getId() : int;
32+
public function getId(): int;
3233

3334
/**
3435
* @throws Throwable
3536
*/
36-
public static function await(mixed $await) : mixed;
37+
public static function await(mixed $await): mixed;
3738

3839
}
3940

40-
final class Async implements AsyncInterface {
41+
final class Async implements AsyncInterface
42+
{
4143

4244
private Promise $promise;
4345

4446
/**
4547
* @throws Throwable
4648
*/
47-
public function __construct(callable $callback) {
49+
public function __construct(callable $callback)
50+
{
4851
$promise = new Promise($callback, true);
4952
$this->promise = $promise;
5053
}
5154

52-
public function getId() : int {
55+
public function getId(): int
56+
{
5357
return $this->promise->getId();
5458
}
5559

5660
/**
5761
* @throws Throwable
5862
*/
59-
public static function await(mixed $await) : mixed {
63+
public static function await(mixed $await): mixed
64+
{
6065
$result = $await;
6166

62-
if (is_callable($await)) {
63-
$await = new Async($await);
64-
}
67+
if (is_callable($await)) $await = new Async($await);
6568

6669
if ($await instanceof Promise || $await instanceof Async) {
6770
$return = EventLoop::getReturn($await->getId());
@@ -71,9 +74,7 @@ public static function await(mixed $await) : mixed {
7174
FiberManager::wait();
7275
}
7376

74-
if ($return instanceof Promise) {
75-
$result = $return->getResult();
76-
}
77+
if ($return instanceof Promise) $result = $return->getResult();
7778
}
7879

7980
return $result;

src/vennv/vapm/ChildCoroutine.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,55 @@
1919
* GNU General Public License for more details.
2020
*/
2121

22-
declare(strict_types = 1);
22+
declare(strict_types=1);
2323

2424
namespace vennv\vapm;
2525

2626
use Generator;
2727
use Exception;
2828

29-
interface ChildCoroutineInterface {
29+
interface ChildCoroutineInterface
30+
{
3031

31-
public function setException(Exception $exception) : void;
32+
public function setException(Exception $exception): void;
3233

33-
public function run() : void;
34+
public function run(): void;
3435

35-
public function isFinished() : bool;
36+
public function isFinished(): bool;
3637

37-
public function getReturn() : mixed;
38+
public function getReturn(): mixed;
3839

3940
}
4041

41-
final class ChildCoroutine implements ChildCoroutineInterface {
42+
final class ChildCoroutine implements ChildCoroutineInterface
43+
{
4244

4345
protected Generator $coroutine;
4446

4547
protected Exception $exception;
4648

47-
public function __construct(Generator $coroutine) {
49+
public function __construct(Generator $coroutine)
50+
{
4851
$this->coroutine = $coroutine;
4952
}
5053

51-
public function setException(Exception $exception) : void {
54+
public function setException(Exception $exception): void
55+
{
5256
$this->exception = $exception;
5357
}
5458

55-
public function run() : void {
59+
public function run(): void
60+
{
5661
$this->coroutine->send($this->coroutine->current());
5762
}
5863

59-
public function isFinished() : bool {
64+
public function isFinished(): bool
65+
{
6066
return !$this->coroutine->valid();
6167
}
6268

63-
public function getReturn() : mixed {
69+
public function getReturn(): mixed
70+
{
6471
return $this->coroutine->getReturn();
6572
}
6673

src/vennv/vapm/CoroutineGen.php

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* GNU General Public License for more details.
2020
*/
2121

22-
declare(strict_types = 1);
22+
declare(strict_types=1);
2323

2424
namespace vennv\vapm;
2525

@@ -30,15 +30,16 @@
3030
use Throwable;
3131
use function call_user_func;
3232

33-
interface CoroutineGenInterface {
33+
interface CoroutineGenInterface
34+
{
3435

3536
/**
3637
* @param mixed ...$coroutines
3738
* @return void
3839
*
3940
* This is a blocking function that runs all the coroutines passed to it.
4041
*/
41-
public static function runBlocking(mixed ...$coroutines) : void;
42+
public static function runBlocking(mixed ...$coroutines): void;
4243

4344
/**
4445
* @param callable $callback
@@ -47,40 +48,40 @@ public static function runBlocking(mixed ...$coroutines) : void;
4748
*
4849
* This is a generator that runs a callback function a specified amount of times.
4950
*/
50-
public static function repeat(callable $callback, int $times) : Closure;
51+
public static function repeat(callable $callback, int $times): Closure;
5152

5253
/**
5354
* @param int $milliseconds
5455
* @return Generator
5556
*
5657
* This is a generator that yields for a specified amount of milliseconds.
5758
*/
58-
public static function delay(int $milliseconds) : Generator;
59+
public static function delay(int $milliseconds): Generator;
5960

6061
/**
6162
* @param mixed ...$callback
6263
* @return CoroutineScope
6364
*
6465
* This is a generator that runs a callback function.
6566
*/
66-
public static function launch(mixed ...$callback) : CoroutineScope;
67+
public static function launch(mixed ...$callback): CoroutineScope;
6768

6869
}
6970

70-
final class CoroutineGen implements CoroutineGenInterface {
71+
final class CoroutineGen implements CoroutineGenInterface
72+
{
7173

7274
protected static ?SplQueue $taskQueue = null;
7375

7476
/**
75-
* @throws Throwable
7677
* @param mixed ...$coroutines
7778
* @return void
79+
* @throws Throwable
7880
*/
79-
public static function runBlocking(mixed ...$coroutines) : void {
81+
public static function runBlocking(mixed ...$coroutines): void
82+
{
8083
foreach ($coroutines as $coroutine) {
81-
if (is_callable($coroutine)) {
82-
$coroutine = call_user_func($coroutine);
83-
}
84+
if (is_callable($coroutine)) $coroutine = call_user_func($coroutine);
8485

8586
if ($coroutine instanceof CoroutineScope) {
8687
self::schedule($coroutine);
@@ -98,85 +99,69 @@ public static function runBlocking(mixed ...$coroutines) : void {
9899
* @param mixed ...$coroutines
99100
* @return Closure
100101
*/
101-
private static function processCoroutine(mixed ...$coroutines) : Closure {
102-
return function () use ($coroutines) : void {
102+
private static function processCoroutine(mixed ...$coroutines): Closure
103+
{
104+
return function () use ($coroutines): void {
103105
foreach ($coroutines as $coroutine) {
104106
if ($coroutine instanceof CoroutineScope) {
105107
self::schedule($coroutine);
106108
} else if (is_callable($coroutine)) {
107109
$coroutine = call_user_func($coroutine);
108110
}
109111

110-
if (!$coroutine instanceof Generator) {
111-
call_user_func(fn() => $coroutine);
112-
} else {
113-
self::schedule(new ChildCoroutine($coroutine));
114-
}
112+
!$coroutine instanceof Generator ? call_user_func(fn() => $coroutine) : self::schedule(new ChildCoroutine($coroutine));
115113
}
116114

117115
self::run();
118116
};
119117
}
120118

121-
public static function repeat(callable $callback, int $times) : Closure {
122-
for ($i = 0; $i <= $times; $i++) {
123-
if (call_user_func($callback) instanceof Generator) {
124-
$callback = self::processCoroutine($callback);
125-
}
126-
}
127-
119+
public static function repeat(callable $callback, int $times): Closure
120+
{
121+
for ($i = 0; $i <= $times; $i++) if (call_user_func($callback) instanceof Generator) $callback = self::processCoroutine($callback);
128122
return fn() => null;
129123
}
130124

131-
public static function delay(int $milliseconds) : Generator {
132-
for ($i = 0; $i < GeneratorManager::calculateSeconds($milliseconds); $i++) {
133-
yield;
134-
}
125+
public static function delay(int $milliseconds): Generator
126+
{
127+
for ($i = 0; $i < GeneratorManager::calculateSeconds($milliseconds); $i++) yield;
135128
}
136129

137130
/**
138131
* @throws ReflectionException
139132
* @throws Throwable
140133
*/
141-
public static function launch(mixed ...$callback) : CoroutineScope {
134+
public static function launch(mixed ...$callback): CoroutineScope
135+
{
142136
$coroutine = new CoroutineScope();
143137
$coroutine->launch(...$callback);
144138

145139
return $coroutine;
146140
}
147141

148-
private static function schedule(ChildCoroutine|CoroutineScope $childCoroutine) : void {
149-
if (self::$taskQueue === null) {
150-
self::$taskQueue = new SplQueue();
151-
}
152-
142+
private static function schedule(ChildCoroutine|CoroutineScope $childCoroutine): void
143+
{
144+
if (self::$taskQueue === null) self::$taskQueue = new SplQueue();
153145
self::$taskQueue->enqueue($childCoroutine);
154146
}
155147

156148
/**
157149
* @throws ReflectionException
158150
* @throws Throwable
159151
*/
160-
private static function run() : void {
161-
if (self::$taskQueue !== null) {
162-
while (!self::$taskQueue->isEmpty()) {
163-
$coroutine = self::$taskQueue->dequeue();
164-
165-
if ($coroutine instanceof ChildCoroutine) {
166-
$coroutine->run();
167-
168-
if (!$coroutine->isFinished()) {
169-
self::schedule($coroutine);
170-
}
171-
}
172-
173-
if ($coroutine instanceof CoroutineScope) {
174-
$coroutine->run();
152+
private static function run(): void
153+
{
154+
while (self::$taskQueue?->isEmpty() === false) {
155+
$coroutine = self::$taskQueue->dequeue();
156+
157+
if ($coroutine instanceof ChildCoroutine) {
158+
$coroutine->run();
159+
if (!$coroutine->isFinished()) self::schedule($coroutine);
160+
}
175161

176-
if (!$coroutine->isFinished()) {
177-
self::schedule($coroutine);
178-
}
179-
}
162+
if ($coroutine instanceof CoroutineScope) {
163+
$coroutine->run();
164+
if (!$coroutine->isFinished()) self::schedule($coroutine);
180165
}
181166
}
182167
}

0 commit comments

Comments
 (0)