Diff

util/timer.lua @ 11264:2cdcf55c6dd5

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Fri, 08 Jan 2021 23:56:27 +0100
parent 10981:e6c1e92cc7a7
parent 11263:1274deeab39a
child 12975:d10957394a3c
line wrap: on
line diff
--- a/util/timer.lua	Fri Jan 08 17:32:06 2021 +0100
+++ b/util/timer.lua	Fri Jan 08 23:56:27 2021 +0100
@@ -15,6 +15,7 @@
 local tostring = tostring;
 local xpcall = require "util.xpcall".xpcall;
 local math_max = math.max;
+local pairs = pairs;
 
 if server.timer then
 	-- The selected net.server implements this API, so defer to that
@@ -34,6 +35,7 @@
 local function _traceback_handler(err) log("error", "Traceback[timer]: %s", debug_traceback(tostring(err), 2)); end
 local function _on_timer(now)
 	local peek;
+	local readd;
 	while true do
 		peek = h:peek();
 		if peek == nil or peek > now then break; end
@@ -43,11 +45,22 @@
 		--item(now, id, _param);
 		local success, err = xpcall(callback, _traceback_handler, now, id, param);
 		if success and type(err) == "number" then
-			h:insert(callback, err + now, id); -- re-add
+			if readd then
+				readd[id] = { callback, err + now };
+			else
+				readd = { [id] = { callback, err + now } };
+			end
 			params[id] = param;
 		end
 	end
 
+	if readd then
+		for id,timer in pairs(readd) do
+			h:insert(timer[1], timer[2], id);
+		end
+		peek = h:peek();
+	end
+
 	if peek ~= nil and _active_timers > 1 and peek == next_time then
 		-- Another instance of _on_timer already set next_time to the same value,
 		-- so it should be safe to not renew this timer event