From 03945d096e8d68ee3e979e1fa6491935d6284a4e Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Fri, 6 Aug 2021 20:01:59 +0200 Subject: [PATCH] Safer timeout implementation --- timeouts.lua | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/timeouts.lua b/timeouts.lua index b3b69e8..864889e 100644 --- a/timeouts.lua +++ b/timeouts.lua @@ -1,5 +1,6 @@ lua_async.timeouts = { pool = {}, + executing = {}, last_id = 0, } @@ -16,15 +17,22 @@ end function clearTimeout(id) lua_async.timeouts.pool[id] = nil + lua_async.timeouts.executing[id] = nil end function lua_async.timeouts.step(dtime) - for id, timeout in pairs(lua_async.timeouts.pool) do + lua_async.timeouts.executing = lua_async.timeouts.pool + lua_async.timeouts.pool = {} + + for id, timeout in pairs(lua_async.timeouts.executing) do timeout.time_left = timeout.time_left - dtime if timeout.time_left <= 0 then timeout.callback(unpack(timeout.args)) - clearTimeout(id) + else + lua_async.timeouts.pool[id] = timeout end + + lua_async.timeouts.executing[id] = nil end end -- 2.44.0