aboutsummaryrefslogtreecommitdiff
path: root/timeouts.lua
diff options
context:
space:
mode:
authorElias Fleckenstein <eliasfleckenstein@web.de>2021-08-06 20:01:59 +0200
committerElias Fleckenstein <eliasfleckenstein@web.de>2021-08-06 20:01:59 +0200
commit03945d096e8d68ee3e979e1fa6491935d6284a4e (patch)
tree1fc6e1cd9655a45bfc9bb1c665a45191eb6375bb /timeouts.lua
parentc3909348044e26d642e4c7f196eb27f1f6255eda (diff)
downloadlua_async-03945d096e8d68ee3e979e1fa6491935d6284a4e.tar.xz
Safer timeout implementation
Diffstat (limited to 'timeouts.lua')
-rw-r--r--timeouts.lua12
1 files 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