Software /
code /
prosody
Annotate
util/timer.lua @ 6514:d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
author | daurnimator <quae@daurnimator.com> |
---|---|
date | Tue, 18 Nov 2014 14:14:41 -0500 |
parent | 6481:dbc72cd1332e |
child | 6791:e813e8cf6046 |
rev | line source |
---|---|
1523
841d61be198f
Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
1 -- Prosody IM |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
1871
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
1871
diff
changeset
|
3 -- Copyright (C) 2008-2010 Waqas Hussain |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
4871
diff
changeset
|
4 -- |
832
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 |
5877
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
9 local indexedbheap = require "util.indexedbheap"; |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
10 local log = require "util.logger".init("timer"); |
4808
07d0a3a75c8a
net.server, net.timer, net.server_select: Rearrange dependencies between these three modules. server.addtimer() is no longer a public function (renamed to _addtimer) and is not available at all from server_event (compat code removed to prevent traceback) (thanks Nulani)
Matthew Wild <mwild1@gmail.com>
parents:
4751
diff
changeset
|
11 local server = require "net.server"; |
3683
c2158221e2e6
util.timer: Activate higher timer precision.
Waqas Hussain <waqas20@gmail.com>
parents:
2964
diff
changeset
|
12 local get_time = require "socket".gettime; |
832
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 local type = type; |
5880
11f14d44438e
util.timer: Import all require upvalues.
Waqas Hussain <waqas20@gmail.com>
parents:
5879
diff
changeset
|
14 local debug_traceback = debug.traceback; |
11f14d44438e
util.timer: Import all require upvalues.
Waqas Hussain <waqas20@gmail.com>
parents:
5879
diff
changeset
|
15 local tostring = tostring; |
11f14d44438e
util.timer: Import all require upvalues.
Waqas Hussain <waqas20@gmail.com>
parents:
5879
diff
changeset
|
16 local xpcall = xpcall; |
832
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 module "timer" |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 |
6481
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
5898
diff
changeset
|
20 local _add_task = server.add_task; |
5877
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
21 --add_task = _add_task; |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
22 |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
23 local h = indexedbheap.create(); |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
24 local params = {}; |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
25 local next_time = nil; |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
26 local _id, _callback, _now, _param; |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
27 local function _call() return _callback(_now, _id, _param); end |
5880
11f14d44438e
util.timer: Import all require upvalues.
Waqas Hussain <waqas20@gmail.com>
parents:
5879
diff
changeset
|
28 local function _traceback_handler(err) log("error", "Traceback[timer]: %s", debug_traceback(tostring(err), 2)); end |
5877
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
29 local function _on_timer(now) |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
30 local peek; |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
31 while true do |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
32 peek = h:peek(); |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
33 if peek == nil or peek > now then break; end |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
34 local _; |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
35 _, _callback, _id = h:pop(); |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
36 _now = now; |
5879
95f1d5421fbc
util.timer: Fix another variable name typo (thanks again zash).
Waqas Hussain <waqas20@gmail.com>
parents:
5878
diff
changeset
|
37 _param = params[_id]; |
95f1d5421fbc
util.timer: Fix another variable name typo (thanks again zash).
Waqas Hussain <waqas20@gmail.com>
parents:
5878
diff
changeset
|
38 params[_id] = nil; |
5877
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
39 --item(now, id, _param); -- FIXME pcall |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
40 local success, err = xpcall(_call, _traceback_handler); |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
41 if success and type(err) == "number" then |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
42 h:insert(_callback, err + now, _id); -- re-add |
5898
bf9aba718c01
util/timer: Re-set params when timer is rescheduled
daurnimator <quae@daurnimator.com>
parents:
5880
diff
changeset
|
43 params[_id] = _param; |
5877
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
44 end |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
45 end |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
46 next_time = peek; |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
47 if peek ~= nil then |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
48 return peek - now; |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
49 end |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
50 end |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
51 function add_task(delay, callback, param) |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
52 local current_time = get_time(); |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
53 local event_time = current_time + delay; |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
54 |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
55 local id = h:insert(callback, event_time); |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
56 params[id] = param; |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
57 if next_time == nil or event_time < next_time then |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
58 next_time = event_time; |
5878
2b1c0c0a2ea6
util.timer: Fix variable name typo.
Waqas Hussain <waqas20@gmail.com>
parents:
5877
diff
changeset
|
59 _add_task(next_time - current_time, _on_timer); |
5877
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
60 end |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
61 return id; |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
62 end |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
63 function stop(id) |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
64 params[id] = nil; |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
65 return h:remove(id); |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
66 end |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
67 function reschedule(id, delay) |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
68 local current_time = get_time(); |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
69 local event_time = current_time + delay; |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
70 h:reprioritize(id, delay); |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
71 if next_time == nil or event_time < next_time then |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
72 next_time = event_time; |
5878
2b1c0c0a2ea6
util.timer: Fix variable name typo.
Waqas Hussain <waqas20@gmail.com>
parents:
5877
diff
changeset
|
73 _add_task(next_time - current_time, _on_timer); |
5877
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
74 end |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
75 return id; |
615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents:
5776
diff
changeset
|
76 end |
832
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 |
282ae70db19f
Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 return _M; |