Skip to content

Commit ab6f71a

Browse files
authored
Update for CoroutineGen!
1 parent 10d57f3 commit ab6f71a

File tree

2 files changed

+49
-37
lines changed

2 files changed

+49
-37
lines changed

src/vennv/vapm/CoroutineGen.php

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,22 @@ final class CoroutineGen implements CoroutineGenInterface
8080
*/
8181
public static function runBlocking(mixed ...$coroutines): void
8282
{
83-
foreach ($coroutines as $coroutine) {
84-
if (is_callable($coroutine)) $coroutine = call_user_func($coroutine);
85-
86-
if ($coroutine instanceof CoroutineScope) {
87-
self::schedule($coroutine);
88-
} else if ($coroutine instanceof Generator) {
89-
self::schedule(new ChildCoroutine($coroutine));
90-
} else {
91-
call_user_func(fn() => $coroutine);
83+
new Async(function () use ($coroutines): void {
84+
foreach ($coroutines as $coroutine) {
85+
if (is_callable($coroutine)) $coroutine = call_user_func($coroutine);
86+
87+
if ($coroutine instanceof CoroutineScope) {
88+
self::schedule($coroutine);
89+
} else if ($coroutine instanceof Generator) {
90+
self::schedule(new ChildCoroutine($coroutine));
91+
} else {
92+
call_user_func(fn() => $coroutine);
93+
}
94+
FiberManager::wait();
9295
}
93-
}
9496

95-
self::run();
97+
self::run();
98+
});
9699
}
97100

98101
/**
@@ -151,19 +154,26 @@ private static function schedule(ChildCoroutine|CoroutineScope $childCoroutine):
151154
*/
152155
private static function run(): void
153156
{
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-
}
161-
162-
if ($coroutine instanceof CoroutineScope) {
163-
$coroutine->run();
164-
if (!$coroutine->isFinished()) self::schedule($coroutine);
157+
new Async(function (): void {
158+
try {
159+
while (self::$taskQueue?->isEmpty() === false) {
160+
$coroutine = self::$taskQueue->dequeue();
161+
162+
if ($coroutine instanceof ChildCoroutine) {
163+
$coroutine->run();
164+
if (!$coroutine->isFinished()) self::schedule($coroutine);
165+
}
166+
167+
if ($coroutine instanceof CoroutineScope) {
168+
Async::await($coroutine->run());
169+
if (!$coroutine->isFinished()) self::schedule($coroutine);
170+
}
171+
FiberManager::wait();
172+
}
173+
} catch (Throwable $e) {
174+
echo $e->getMessage();
165175
}
166-
}
176+
});
167177
}
168178

169179
}

src/vennv/vapm/CoroutineScope.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function launch(mixed ...$callbacks): void;
6464
/**
6565
* This function runs the coroutine.
6666
*/
67-
public function run(): void;
67+
public function run(): Async;
6868

6969
}
7070

@@ -127,25 +127,27 @@ public function launch(mixed ...$callbacks): void
127127
* @throws ReflectionException
128128
* @throws Throwable
129129
*/
130-
public function run(): void
130+
public function run(): Async
131131
{
132-
if (self::$taskQueue?->isEmpty() === false && !self::$cancelled) {
133-
$coroutine = self::$taskQueue->dequeue();
132+
return new Async(function (): void {
133+
if (self::$taskQueue?->isEmpty() === false && !self::$cancelled) {
134+
$coroutine = self::$taskQueue->dequeue();
134135

135-
if ($coroutine instanceof ChildCoroutine) {
136-
$coroutine->run();
136+
if ($coroutine instanceof ChildCoroutine) {
137+
$coroutine->run();
137138

138-
if (!$coroutine->isFinished()) self::schedule($coroutine);
139-
}
139+
if (!$coroutine->isFinished()) self::schedule($coroutine);
140+
}
140141

141-
if ($coroutine instanceof CoroutineScope) {
142-
$coroutine->run();
142+
if ($coroutine instanceof CoroutineScope) {
143+
Async::await($coroutine->run());
143144

144-
if (!$coroutine->isFinished()) self::schedule($coroutine);
145+
if (!$coroutine->isFinished()) self::schedule($coroutine);
146+
}
147+
} else {
148+
self::$finished = true;
145149
}
146-
} else {
147-
self::$finished = true;
148-
}
150+
});
149151
}
150152

151153
private static function schedule(ChildCoroutine|CoroutineScope $childCoroutine): void

0 commit comments

Comments
 (0)