core.stanza_router: Remove compatibility with Jabiru not including id attribute ...
[prosody.git] / util / timer.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local server = require "net.server";
10 local math_min = math.min
11 local math_huge = math.huge
12 local get_time = require "socket".gettime;
13 local t_insert = table.insert;
14 local pairs = pairs;
15 local type = type;
16
17 local data = {};
18 local new_data = {};
19
20 local _ENV = nil;
21
22 local _add_task;
23 if not server.event then
24         function _add_task(delay, callback)
25                 local current_time = get_time();
26                 delay = delay + current_time;
27                 if delay >= current_time then
28                         t_insert(new_data, {delay, callback});
29                 else
30                         local r = callback(current_time);
31                         if r and type(r) == "number" then
32                                 return _add_task(r, callback);
33                         end
34                 end
35         end
36
37         server._addtimer(function()
38                 local current_time = get_time();
39                 if #new_data > 0 then
40                         for _, d in pairs(new_data) do
41                                 t_insert(data, d);
42                         end
43                         new_data = {};
44                 end
45
46                 local next_time = math_huge;
47                 for i, d in pairs(data) do
48                         local t, callback = d[1], d[2];
49                         if t <= current_time then
50                                 data[i] = nil;
51                                 local r = callback(current_time);
52                                 if type(r) == "number" then
53                                         _add_task(r, callback);
54                                         next_time = math_min(next_time, r);
55                                 end
56                         else
57                                 next_time = math_min(next_time, t - current_time);
58                         end
59                 end
60                 return next_time;
61         end);
62 else
63         local event = server.event;
64         local event_base = server.event_base;
65         local EVENT_LEAVE = (event.core and event.core.LEAVE) or -1;
66
67         function _add_task(delay, callback)
68                 local event_handle;
69                 event_handle = event_base:addevent(nil, 0, function ()
70                         local ret = callback(get_time());
71                         if ret then
72                                 return 0, ret;
73                         elseif event_handle then
74                                 return EVENT_LEAVE;
75                         end
76                 end
77                 , delay);
78         end
79 end
80
81 return {
82         add_task = _add_task;
83 };