Skip to content

Commit 6f8faf0

Browse files
committed
fix: fix clearInterval #200
1 parent 14e8abc commit 6f8faf0

File tree

3 files changed

+30
-46
lines changed

3 files changed

+30
-46
lines changed

src/legacy/engine/TimeTaskSystem.cpp

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,13 @@ int NewTimeout(Local<Function> func, std::vector<Local<Value>> paras, int timeou
5858
data.engine = EngineScope::currentEngine();
5959
for (auto& para : paras) data.paras.emplace_back(std::move(para));
6060

61-
ll::coro::keepThis([timeout, tid, data = std::move(data)]() -> ll::coro::CoroTask<> {
61+
ll::coro::keepThis([timeout, tid, data]() -> ll::coro::CoroTask<> {
6262
co_await std::chrono::milliseconds(timeout);
6363
try {
6464
if ((ll::getGamingStatus() == ll::GamingStatus::Stopping) || !EngineManager::isValid(data.engine))
6565
co_return;
6666

6767
EngineScope scope(data.engine);
68-
6968
if (!data.func.isEmpty()) {
7069
std::vector<Local<Value>> args;
7170
for (auto& para : data.paras) {
@@ -80,7 +79,7 @@ int NewTimeout(Local<Function> func, std::vector<Local<Value>> paras, int timeou
8079
}).launch(ll::thread::ServerThreadExecutor::getDefault());
8180

8281
std::lock_guard lock(locker);
83-
timeTaskMap[tid] = std::move(data);
82+
timeTaskMap[tid] = data;
8483
return tid;
8584
}
8685

@@ -91,34 +90,24 @@ int NewTimeout(Local<String> func, int timeout) {
9190
data.code = func;
9291
data.engine = EngineScope::currentEngine();
9392

94-
ll::coro::keepThis([timeout, tid, data = std::move(data)]() -> ll::coro::CoroTask<> {
93+
ll::coro::keepThis([timeout, tid, data]() -> ll::coro::CoroTask<> {
9594
co_await std::chrono::milliseconds(timeout);
9695
try {
9796
if ((ll::getGamingStatus() == ll::GamingStatus::Stopping) || !EngineManager::isValid(data.engine))
9897
co_return;
9998

10099
EngineScope scope(data.engine);
101-
{
102-
std::lock_guard lock(locker);
103-
auto it = timeTaskMap.find(tid);
104-
if (it == timeTaskMap.end()) co_return;
105-
106-
auto& taskData = it->second;
107-
108-
if (!taskData.code.isEmpty()) {
109-
auto code = taskData.code.get().toString();
110-
data.engine->eval(code);
111-
}
112-
113-
// 清理任务
114-
timeTaskMap.erase(tid);
100+
if (!data.code.isEmpty()) {
101+
auto code = data.code.get().toString();
102+
data.engine->eval(code);
115103
}
116104
}
117105
TIMETASK_CATCH("setTimeout-String");
106+
ClearTimeTask(tid);
118107
}).launch(ll::thread::ServerThreadExecutor::getDefault());
119108

120109
std::lock_guard lock(locker);
121-
timeTaskMap[tid] = std::move(data);
110+
timeTaskMap[tid] = data;
122111
return tid;
123112
}
124113

@@ -130,10 +119,14 @@ int NewInterval(Local<Function> func, std::vector<Local<Value>> paras, int timeo
130119
data.engine = EngineScope::currentEngine();
131120
for (auto& para : paras) data.paras.emplace_back(std::move(para));
132121

133-
ll::coro::keepThis([timeout, tid, data = std::move(data)]() -> ll::coro::CoroTask<> {
122+
ll::coro::keepThis([timeout, tid, data]() -> ll::coro::CoroTask<> {
134123
while (true) {
135124
co_await std::chrono::milliseconds(timeout);
136125
try {
126+
if (!CheckTimeTask(tid)) {
127+
co_return;
128+
}
129+
137130
if ((ll::getGamingStatus() == ll::GamingStatus::Stopping) || !EngineManager::isValid(data.engine)) {
138131
ClearTimeTask(tid);
139132
co_return;
@@ -155,7 +148,7 @@ int NewInterval(Local<Function> func, std::vector<Local<Value>> paras, int timeo
155148
}).launch(ll::thread::ServerThreadExecutor::getDefault());
156149

157150
std::lock_guard lock(locker);
158-
timeTaskMap[tid] = std::move(data);
151+
timeTaskMap[tid] = data;
159152
return tid;
160153
}
161154

@@ -166,51 +159,41 @@ int NewInterval(Local<String> func, int timeout) {
166159
data.code = func;
167160
data.engine = EngineScope::currentEngine();
168161

169-
ll::coro::keepThis([timeout, tid, data = std::move(data)]() -> ll::coro::CoroTask<> {
162+
ll::coro::keepThis([timeout, tid, data]() -> ll::coro::CoroTask<> {
170163
while (true) {
171164
co_await std::chrono::milliseconds(timeout);
172165
try {
173-
if ((ll::getGamingStatus() == ll::GamingStatus::Stopping)) {
174-
ClearTimeTask(tid);
166+
if (!CheckTimeTask(tid)) {
175167
co_return;
176168
}
177-
178-
if (!EngineManager::isValid(data.engine)) {
169+
if ((ll::getGamingStatus() == ll::GamingStatus::Stopping) || !EngineManager::isValid(data.engine)) {
179170
ClearTimeTask(tid);
180171
co_return;
181172
}
182173

183174
EngineScope scope(data.engine);
184-
std::string code;
185-
{
186-
std::lock_guard lock(locker);
187-
188-
auto it = timeTaskMap.find(tid);
189-
if (it == timeTaskMap.end()) co_return;
190-
191-
auto& taskData = it->second;
192-
193-
if (taskData.code.isEmpty()) co_return;
194-
code = taskData.code.get().toString();
195-
}
196-
if (!code.empty()) {
197-
data.engine->eval(code);
175+
if (!data.code.isEmpty()) {
176+
data.engine->eval(data.code.get().toString());
198177
}
199178
}
200179
TIMETASK_CATCH("setInterval-String");
201180
}
202181
}).launch(ll::thread::ServerThreadExecutor::getDefault());
203182

204183
std::lock_guard lock(locker);
205-
timeTaskMap[tid] = std::move(data);
184+
timeTaskMap[tid] = data;
206185
return tid;
207186
}
208187

209-
bool ClearTimeTask(int id) {
188+
bool CheckTimeTask(int const& id) {
189+
std::lock_guard lock(locker);
190+
return timeTaskMap.find(id) != timeTaskMap.end();
191+
}
192+
193+
bool ClearTimeTask(int const& id) {
210194
try {
211195
std::lock_guard lock(locker);
212-
auto it = timeTaskMap.find(id);
213-
if (it != timeTaskMap.end()) {
196+
if (timeTaskMap.find(id) != timeTaskMap.end()) {
214197
timeTaskMap.erase(id);
215198
}
216199
} catch (...) {

src/legacy/engine/TimeTaskSystem.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ int NewTimeout(Local<Function> func, std::vector<Local<Value>> paras, int timeo
1717
int NewTimeout(Local<String> func, int timeout);
1818
int NewInterval(Local<Function> func, std::vector<Local<Value>> paras, int timeout);
1919
int NewInterval(Local<String> func, int timeout);
20-
bool ClearTimeTask(int id);
20+
bool ClearTimeTask(int const& id);
21+
bool CheckTimeTask(int const& id);
2122

2223
///////////////////////// Func /////////////////////////
2324

src/lse/events/EventHooks.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ LL_TYPE_INSTANCE_HOOK(
11081108
}
11091109
}
11101110
IF_LISTENED_END(EVENT_TYPES::onSetArmor);
1111-
origin(std::move(armorSlot), item);
1111+
origin(armorSlot, item);
11121112
}
11131113

11141114
LL_TYPE_INSTANCE_HOOK(

0 commit comments

Comments
 (0)