This commit is contained in:
2021-04-23 10:18:04 +08:00
parent 03115c0591
commit ffdc2a5ebd
+13 -35
View File
@@ -4,11 +4,10 @@ declare(strict_types=1);
namespace Database; namespace Database;
use Snowflake\Abstracts\Component;
use Closure; use Closure;
use Exception; use Exception;
use Snowflake\Abstracts\Component;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Snowflake;
use Swoole\Coroutine; use Swoole\Coroutine;
/** /**
@@ -134,9 +133,7 @@ class Pagination extends Component
*/ */
public function plunk($param = []) public function plunk($param = [])
{ {
$this->_group = new Coroutine\WaitGroup(); $this->loop($param);
Coroutine::create([$this, 'loop'], $param);
$this->_group->wait();
} }
@@ -144,6 +141,7 @@ class Pagination extends Component
* 轮训 * 轮训
* @param $param * @param $param
* @return array * @return array
* @throws Exception
*/ */
public function loop($param): array public function loop($param): array
{ {
@@ -152,7 +150,7 @@ class Pagination extends Component
} }
[$length, $data] = $this->get(); [$length, $data] = $this->get();
$this->runner($data, $param); $this->executed($this->_callback, $data, $param);
unset($data); unset($data);
if ($length < $this->_limit) { if ($length < $this->_limit) {
@@ -171,41 +169,21 @@ class Pagination extends Component
} }
/**
* @param $data
* @param $param
*/
private function runner($data, $param)
{
if (Snowflake::inCoroutine()) {
$this->executed($this->_callback, $data, $param);
} else {
call_user_func($this->_callback, $data, $param);
}
}
/** /**
* @param $callback * @param $callback
* @param $data * @param $data
* @param $param * @param $param
* 解释器 * @throws Exception
* @return mixed
*/ */
private function executed($callback, $data, $param): mixed private function executed($callback, $data, $param): void
{ {
$this->_group->add(1); try {
return Coroutine::create(function ($callback, $data, $param): void { call_user_func($callback, $data, $param);
try { } catch (\Throwable $exception) {
call_user_func($callback, $data, $param); $this->addError($exception, 'throwable');
} catch (\Throwable $exception) { } finally {
$this->addError($exception, 'throwable'); fire(Event::SYSTEM_RESOURCE_RELEASES);
} finally { }
$event = Snowflake::app()->getEvent();
$event->trigger(Event::SYSTEM_RESOURCE_RELEASES);
$this->_group->done();
}
}, $callback, $data, $param);
} }