Comparison

util/timer.lua @ 841:77ff000c2055

util.timer: Fix crash when loaded but no tasks set, fix skipping some tasks when multiple set, and one removed
author Matthew Wild <mwild1@gmail.com>
date Thu, 26 Feb 2009 21:00:42 +0000
parent 832:282ae70db19f
child 842:4932f94d35c1
comparison
equal deleted inserted replaced
840:ad842df925c7 841:77ff000c2055
8 8
9 9
10 local ns_addtimer = require "net.server".addtimer; 10 local ns_addtimer = require "net.server".addtimer;
11 local get_time = os.time; 11 local get_time = os.time;
12 local t_insert = table.insert; 12 local t_insert = table.insert;
13 local ipairs = ipairs; 13 local t_remove = table.remove;
14 local ipairs, pairs = ipairs, pairs;
14 local type = type; 15 local type = type;
15 16
16 local data = {}; 17 local data = {};
17 local new_data = {}; 18 local new_data = {};
18 19
28 29
29 add_task = _add_task; 30 add_task = _add_task;
30 31
31 ns_addtimer(function() 32 ns_addtimer(function()
32 local current_time = get_time(); 33 local current_time = get_time();
33 for _, d in ipairs(new_data) do 34 if #new_data > 0 then
34 t_insert(data, d); 35 for _, d in ipairs(new_data) do
36 t_insert(data, d);
37 end
38 new_data = {};
39 elseif #data == 0 then
40 return;
35 end 41 end
36 new_data = {}; 42
37 for i = #data,1 do 43 for i, d in pairs(data) do
38 local t, func = data[i][1], data[i][2]; 44 local t, func = d[1], d[2];
39 if t <= current_time then 45 if t <= current_time then
40 data[i] = nil; 46 t_remove(data, i);
41 local r = func(); 47 local r = func();
42 if type(r) == "number" then _add_task(r, func); end 48 if type(r) == "number" then _add_task(r, func); end
43 end 49 end
44 end 50 end
45 end); 51 end);