Comparison

util/timer.lua @ 4413:ffa4bed1b716

util.timer: Variable name change (func -> callback)
author Matthew Wild <mwild1@gmail.com>
date Thu, 03 Nov 2011 12:47:52 +0000
parent 4385:c94167139f27
child 4751:0c7ae4bfc835
comparison
equal deleted inserted replaced
4412:5d7d9a60bc7f 4413:ffa4bed1b716
24 24
25 module "timer" 25 module "timer"
26 26
27 local _add_task; 27 local _add_task;
28 if not event then 28 if not event then
29 function _add_task(delay, func) 29 function _add_task(delay, callback)
30 local current_time = get_time(); 30 local current_time = get_time();
31 delay = delay + current_time; 31 delay = delay + current_time;
32 if delay >= current_time then 32 if delay >= current_time then
33 t_insert(new_data, {delay, func}); 33 t_insert(new_data, {delay, callback});
34 else 34 else
35 local r = func(); 35 local r = callback();
36 if r and type(r) == "number" then 36 if r and type(r) == "number" then
37 return _add_task(r, func); 37 return _add_task(r, callback);
38 end 38 end
39 end 39 end
40 end 40 end
41 41
42 ns_addtimer(function() 42 ns_addtimer(function()
48 new_data = {}; 48 new_data = {};
49 end 49 end
50 50
51 local next_time = math_huge; 51 local next_time = math_huge;
52 for i, d in pairs(data) do 52 for i, d in pairs(data) do
53 local t, func = d[1], d[2]; 53 local t, callback = d[1], d[2];
54 if t <= current_time then 54 if t <= current_time then
55 data[i] = nil; 55 data[i] = nil;
56 local r = func(current_time); 56 local r = callback(current_time);
57 if type(r) == "number" then 57 if type(r) == "number" then
58 _add_task(r, func); 58 _add_task(r, callback);
59 next_time = math_min(next_time, r); 59 next_time = math_min(next_time, r);
60 end 60 end
61 else 61 else
62 next_time = math_min(next_time, t - current_time); 62 next_time = math_min(next_time, t - current_time);
63 end 63 end
64 end 64 end
65 return next_time; 65 return next_time;
66 end); 66 end);
67 else 67 else
68 local EVENT_LEAVE = (event.core and event.core.LEAVE) or -1; 68 local EVENT_LEAVE = (event.core and event.core.LEAVE) or -1;
69 function _add_task(delay, func) 69 function _add_task(delay, callback)
70 local event_handle; 70 local event_handle;
71 event_handle = event_base:addevent(nil, 0, function () 71 event_handle = event_base:addevent(nil, 0, function ()
72 local ret = func(); 72 local ret = callback();
73 if ret then 73 if ret then
74 return 0, ret; 74 return 0, ret;
75 elseif event_handle then 75 elseif event_handle then
76 return EVENT_LEAVE; 76 return EVENT_LEAVE;
77 end 77 end