diff options
-rw-r--r-- | script/await.lua | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/script/await.lua b/script/await.lua index 495f381c..bfa8257d 100644 --- a/script/await.lua +++ b/script/await.lua @@ -14,6 +14,16 @@ m.watchList = {} m.needClose = {} m._enable = true +local function setID(id, co, callback) + if not coroutine.isyieldable(co) then + return + end + if not m.idMap[id] then + m.idMap[id] = setmetatable({}, wkmt) + end + m.idMap[id][co] = callback +end + --- 设置错误处理器 ---@param errHandle function {comment = '当有错误发生时,会以错误堆栈为参数调用该函数'} function m.setErrorHandle(errHandle) @@ -41,7 +51,7 @@ function m.call(callback, ...) if not id then break end - m.setID(id, co) + setID(id, co) end local currentCo = coroutine.running() @@ -69,15 +79,9 @@ function m.await(callback, ...) end --- 设置一个id,用于批量关闭任务 -function m.setID(id, co) - co = co or coroutine.running() - if not coroutine.isyieldable(co) then - return - end - if not m.idMap[id] then - m.idMap[id] = setmetatable({}, wkmt) - end - m.idMap[id][co] = true +function m.setID(id, callback) + local co = coroutine.running() + setID(id, co, callback) end --- 根据id批量关闭任务 @@ -87,9 +91,12 @@ function m.close(id) return end m.idMap[id] = nil - for co in pairs(map) do + for co, callback in pairs(map) do if coroutine.status(co) == 'suspended' then map[co] = nil + if callback then + xpcall(callback, log.error) + end coroutine.close(co) end end |