Skip to content

Commit 07d1092

Browse files
committed
add go loop
1 parent e8f3f5c commit 07d1092

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/functions.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* Time: 15:21
77
*/
88

9+
use rabbit\helper\ExceptionHelper;
10+
911
defined('BREAKS') or define('BREAKS', PHP_SAPI === 'cli' ? PHP_EOL : '</br>');
1012

1113
if (!function_exists('getDI')) {
@@ -81,3 +83,22 @@ function hasDef(string $key): bool
8183
return \rabbit\core\ObjectFactory::hasDef($key);
8284
}
8385
}
86+
87+
if (!function_exists('goloop')) {
88+
/**
89+
* @param string $key
90+
* @return bool
91+
*/
92+
function goloop(\Closure $function): int
93+
{
94+
return go(function () use ($function) {
95+
while (true) {
96+
try {
97+
$function();
98+
} catch (\Throwable $throwable) {
99+
print_r(ExceptionHelper::convertExceptionToArray($throwable));
100+
}
101+
}
102+
});
103+
}
104+
}

src/helper/CoroHelper.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public static function addDefer(callable $function): void
8585
* @param \Closure $function
8686
* @param \Closure|null $defer
8787
*/
88-
public static function run(\Closure $function, ?\Closure $defer = null)
88+
public static function run(\Closure $function, ?\Closure $defer = null): void
8989
{
9090
\Co\run(function () use ($function, $defer) {
9191
try {
@@ -98,4 +98,25 @@ public static function run(\Closure $function, ?\Closure $defer = null)
9898
}
9999
});
100100
}
101+
102+
/**
103+
* @param \Closure $function
104+
* @param \Closure|null $defer
105+
* @return int
106+
*/
107+
public static function loop(\Closure $function, ?\Closure $defer = null): int
108+
{
109+
return go(function () use ($function, $defer) {
110+
while (true) {
111+
try {
112+
if (is_callable($defer)) {
113+
defer($defer);
114+
}
115+
$function();
116+
} catch (\Throwable $throwable) {
117+
print_r(ExceptionHelper::convertExceptionToArray($throwable));
118+
}
119+
}
120+
});
121+
}
101122
}

0 commit comments

Comments
 (0)