Software /
code /
prosody
Annotate
util/timer.lua @ 832:282ae70db19f
Added util/timer.lua - a timer API
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sat, 21 Feb 2009 01:13:53 +0500 |
child | 841:77ff000c2055 |
rev | line source |
---|---|
832
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 -- Prosody IM v0.3 |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 -- Copyright (C) 2008-2009 Matthew Wild |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 -- Copyright (C) 2008-2009 Waqas Hussain |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 -- |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 -- COPYING file in the source package for more information. |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
7 -- |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 local ns_addtimer = require "net.server".addtimer; |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 local get_time = os.time; |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 local t_insert = table.insert; |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 local ipairs = ipairs; |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 local type = type; |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 local data = {}; |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 local new_data = {}; |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 module "timer" |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 local function _add_task(delay, func) |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 local current_time = get_time(); |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 delay = delay + current_time; |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 if delay >= current_time then |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 t_insert(new_data, {delay, func}); |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 else func(); end |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 end |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 add_task = _add_task; |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 ns_addtimer(function() |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 local current_time = get_time(); |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 for _, d in ipairs(new_data) do |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 t_insert(data, d); |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 end |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 new_data = {}; |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 for i = #data,1 do |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 local t, func = data[i][1], data[i][2]; |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 if t <= current_time then |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 data[i] = nil; |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 local r = func(); |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 if type(r) == "number" then _add_task(r, func); end |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 end |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 end |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 end); |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 return _M; |