Skip to content

Commit 580269e

Browse files
authored
Update CoroutineGen.php
1 parent 8f25b73 commit 580269e

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/vennv/vapm/CoroutineGen.php

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,19 @@ final class CoroutineGen implements CoroutineGenInterface
8080
*/
8181
public static function runBlocking(mixed ...$coroutines): void
8282
{
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();
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);
9592
}
93+
}
9694

97-
self::run();
98-
});
95+
self::run();
9996
}
10097

10198
/**
@@ -127,7 +124,7 @@ public static function repeat(callable $callback, int $times): Closure
127124

128125
public static function delay(int $milliseconds): Generator
129126
{
130-
for ($i = 0; $i < GeneratorManager::calculateSeconds($milliseconds); $i++) yield;
127+
for ($i = 0; $i < GeneratorManager::calculateSeconds((int)($milliseconds/5)); $i++) yield;
131128
}
132129

133130
/**
@@ -156,24 +153,30 @@ private static function run(): void
156153
{
157154
new Async(function (): void {
158155
try {
156+
$fnWait = function (&$i) {
157+
FiberManager::wait();
158+
$i = 0;
159+
};
160+
$i = 0;
159161
while (self::$taskQueue?->isEmpty() === false) {
160162
$coroutine = self::$taskQueue->dequeue();
161163

162164
if ($coroutine instanceof ChildCoroutine) {
163165
$coroutine->run();
164166
if (!$coroutine->isFinished()) self::schedule($coroutine);
167+
if ($i >= 5) $fnWait($i);
165168
}
166169

167170
if ($coroutine instanceof CoroutineScope) {
168171
Async::await($coroutine->run());
169172
if (!$coroutine->isFinished()) self::schedule($coroutine);
170173
}
171-
FiberManager::wait();
174+
$i++;
172175
}
173176
} catch (Throwable $e) {
174177
echo $e->getMessage();
175178
}
176179
});
177180
}
178181

179-
}
182+
}

0 commit comments

Comments
 (0)