1010#include " engine/EngineOwnData.h"
1111#include " engine/RemoteCall.h"
1212#include " ll/api/chrono/GameChrono.h"
13+ #include " ll/api/coro/CoroTask.h"
1314#include " ll/api/io/FileUtils.h"
14- #include " ll/api/schedule/Scheduler.h"
15- #include " ll/api/schedule/Task.h"
1615#include " ll/api/service/ServerInfo.h"
16+ #include " ll/api/thread/ThreadPoolExecutor.h"
1717#include " ll/api/utils/StringUtils.h"
1818#include " main/Configs.h"
1919#include " main/Global.h"
2020#include " uv/uv.h"
2121#include " v8/v8.h"
2222
2323#include < functional>
24+ #include < ll/api/service/GamingStatus.h>
2425
25- ll::schedule::ServerTimeScheduler nodeScheduler;
2626using ll::chrono_literals::operator " " _tick;
2727
2828// pre-declare
@@ -38,8 +38,8 @@ std::unique_ptr<node::MultiIsolatePlatform>
3838std::unordered_map<script::ScriptEngine*, node::Environment*> environments;
3939std::unordered_map<script::ScriptEngine*, std::unique_ptr<node::CommonEnvironmentSetup>>* setups =
4040 new std::unordered_map<script::ScriptEngine*, std::unique_ptr<node::CommonEnvironmentSetup>>();
41- std::unordered_map<node::Environment*, bool > isRunning;
42- std::unordered_map <node::Environment*, uint64> uvLoopTask;
41+ std::unordered_map<node::Environment*, bool > isRunning;
42+ std::vector <node::Environment*> uvLoopTask;
4343
4444bool initNodeJs () {
4545 // Init NodeJs
@@ -165,22 +165,23 @@ bool loadPluginCode(script::ScriptEngine* engine, std::string entryScriptPath, s
165165 }
166166
167167 // Start libuv event loop
168- uvLoopTask[env] =
169- nodeScheduler
170- .add <ll::schedule::RepeatTask>(
171- 2_tick,
172- [engine, env, isRunningMap{&isRunning}, eventLoop{it->second ->event_loop ()}]() {
173- if (!(ll::getGamingStatus () != ll::GamingStatus::Running) && (*isRunningMap)[env]) {
174- EngineScope enter (engine);
175- uv_run (eventLoop, UV_RUN_NOWAIT);
176- }
177- if ((ll::getGamingStatus () != ll::GamingStatus::Running)) {
178- uv_stop (eventLoop);
179- lse::getSelfPluginInstance ().getLogger ().debug (" Destroy ServerStopping" );
180- }
168+ uvLoopTask.push_back (env);
169+ ll::coro::keepThis (
170+ [engine, env, isRunningMap{&isRunning}, eventLoop{it->second ->event_loop ()}]() -> ll::coro::CoroTask<> {
171+ using namespace ll ::chrono_literals;
172+ while (std::find (uvLoopTask.begin (), uvLoopTask.end (), env) != uvLoopTask.end ()) {
173+ co_await 2_tick;
174+ if (!(ll::getGamingStatus () != ll::GamingStatus::Running) && (*isRunningMap)[env]) {
175+ EngineScope enter (engine);
176+ uv_run (eventLoop, UV_RUN_NOWAIT);
181177 }
182- )
183- ->getId ();
178+ if ((ll::getGamingStatus () != ll::GamingStatus::Running)) {
179+ uv_stop (eventLoop);
180+ lse::getSelfPluginInstance ().getLogger ().debug (" Destroy ServerStopping" );
181+ }
182+ }
183+ }
184+ ).launch (ll::thread::ThreadPoolExecutor::getDefault ());
184185
185186 return true ;
186187 } catch (...) {
@@ -210,9 +211,9 @@ bool stopEngine(node::Environment* env) {
210211 node::Stop (env);
211212
212213 // Stop libuv event loop
213- auto it = uvLoopTask.find ( env);
214+ auto it = std::find ( uvLoopTask.begin (), uvLoopTask. end (), env);
214215 if (it != uvLoopTask.end ()) {
215- nodeScheduler. remove (it-> second );
216+ uvLoopTask. erase (it);
216217 }
217218
218219 return true ;
0 commit comments