util/timer.lua
author Waqas Hussain <waqas20@gmail.com>
Wed, 30 Oct 2013 17:56:00 -0400
changeset 5879 95f1d5421fbc
parent 5878 2b1c0c0a2ea6
child 5880 11f14d44438e
permissions -rw-r--r--
util.timer: Fix another variable name typo (thanks again zash).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 math_min = math.min
c2158221e2e6 util.timer: Activate higher timer precision.
Waqas Hussain <waqas20@gmail.com>
parents: 2964
diff changeset
    13
local math_huge = math.huge
c2158221e2e6 util.timer: Activate higher timer precision.
Waqas Hussain <waqas20@gmail.com>
parents: 2964
diff changeset
    14
local get_time = require "socket".gettime;
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    15
local t_insert = table.insert;
4751
0c7ae4bfc835 util.timer: Remove unused function imports
Matthew Wild <mwild1@gmail.com>
parents: 4413
diff changeset
    16
local pairs = pairs;
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    17
local type = type;
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
local data = {};
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    20
local new_data = {};
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    21
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    22
module "timer"
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    23
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    24
local _add_task;
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
    25
if not server.event then
4413
ffa4bed1b716 util.timer: Variable name change (func -> callback)
Matthew Wild <mwild1@gmail.com>
parents: 4385
diff changeset
    26
	function _add_task(delay, callback)
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    27
		local current_time = get_time();
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    28
		delay = delay + current_time;
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    29
		if delay >= current_time then
4413
ffa4bed1b716 util.timer: Variable name change (func -> callback)
Matthew Wild <mwild1@gmail.com>
parents: 4385
diff changeset
    30
			t_insert(new_data, {delay, callback});
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    31
		else
4871
b2d177f2febc util.timer: Always pass the current time to timer callbacks.
Waqas Hussain <waqas20@gmail.com>
parents: 4812
diff changeset
    32
			local r = callback(current_time);
4385
c94167139f27 util.timer: Fix corner case of timer not repeating if it returns <= 0
Matthew Wild <mwild1@gmail.com>
parents: 3683
diff changeset
    33
			if r and type(r) == "number" then
4413
ffa4bed1b716 util.timer: Variable name change (func -> callback)
Matthew Wild <mwild1@gmail.com>
parents: 4385
diff changeset
    34
				return _add_task(r, callback);
4385
c94167139f27 util.timer: Fix corner case of timer not repeating if it returns <= 0
Matthew Wild <mwild1@gmail.com>
parents: 3683
diff changeset
    35
			end
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    36
		end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    37
	end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    38
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
    39
	server._addtimer(function()
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    40
		local current_time = get_time();
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    41
		if #new_data > 0 then
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    42
			for _, d in pairs(new_data) do
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    43
				t_insert(data, d);
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    44
			end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    45
			new_data = {};
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    46
		end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 4871
diff changeset
    47
3683
c2158221e2e6 util.timer: Activate higher timer precision.
Waqas Hussain <waqas20@gmail.com>
parents: 2964
diff changeset
    48
		local next_time = math_huge;
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    49
		for i, d in pairs(data) do
4413
ffa4bed1b716 util.timer: Variable name change (func -> callback)
Matthew Wild <mwild1@gmail.com>
parents: 4385
diff changeset
    50
			local t, callback = d[1], d[2];
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    51
			if t <= current_time then
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    52
				data[i] = nil;
4413
ffa4bed1b716 util.timer: Variable name change (func -> callback)
Matthew Wild <mwild1@gmail.com>
parents: 4385
diff changeset
    53
				local r = callback(current_time);
3683
c2158221e2e6 util.timer: Activate higher timer precision.
Waqas Hussain <waqas20@gmail.com>
parents: 2964
diff changeset
    54
				if type(r) == "number" then
4413
ffa4bed1b716 util.timer: Variable name change (func -> callback)
Matthew Wild <mwild1@gmail.com>
parents: 4385
diff changeset
    55
					_add_task(r, callback);
3683
c2158221e2e6 util.timer: Activate higher timer precision.
Waqas Hussain <waqas20@gmail.com>
parents: 2964
diff changeset
    56
					next_time = math_min(next_time, r);
c2158221e2e6 util.timer: Activate higher timer precision.
Waqas Hussain <waqas20@gmail.com>
parents: 2964
diff changeset
    57
				end
c2158221e2e6 util.timer: Activate higher timer precision.
Waqas Hussain <waqas20@gmail.com>
parents: 2964
diff changeset
    58
			else
c2158221e2e6 util.timer: Activate higher timer precision.
Waqas Hussain <waqas20@gmail.com>
parents: 2964
diff changeset
    59
				next_time = math_min(next_time, t - current_time);
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    60
			end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    61
		end
3683
c2158221e2e6 util.timer: Activate higher timer precision.
Waqas Hussain <waqas20@gmail.com>
parents: 2964
diff changeset
    62
		return next_time;
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    63
	end);
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    64
else
4812
5bcdc384e485 util.timer: Remove unnecessary require calls, fixes a traceback (thanks nulani)
Matthew Wild <mwild1@gmail.com>
parents: 4808
diff changeset
    65
	local event = server.event;
5bcdc384e485 util.timer: Remove unnecessary require calls, fixes a traceback (thanks nulani)
Matthew Wild <mwild1@gmail.com>
parents: 4808
diff changeset
    66
	local event_base = server.event_base;
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    67
	local EVENT_LEAVE = (event.core and event.core.LEAVE) or -1;
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
    68
4413
ffa4bed1b716 util.timer: Variable name change (func -> callback)
Matthew Wild <mwild1@gmail.com>
parents: 4385
diff changeset
    69
	function _add_task(delay, callback)
2964
49b5c87d2fa0 util.timer: When using libevent hold onto the event handle to stop it being collected (and the timer stopping). Fixes BOSH ghosts, thanks Flo, niekie, waqas.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    70
		local event_handle;
49b5c87d2fa0 util.timer: When using libevent hold onto the event handle to stop it being collected (and the timer stopping). Fixes BOSH ghosts, thanks Flo, niekie, waqas.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    71
		event_handle = event_base:addevent(nil, 0, function ()
4871
b2d177f2febc util.timer: Always pass the current time to timer callbacks.
Waqas Hussain <waqas20@gmail.com>
parents: 4812
diff changeset
    72
			local ret = callback(get_time());
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    73
			if ret then
2366
c3a364342cb4 util.timer: Use luaevent's built-in method of repeating an event (fixes a weird crash)
Matthew Wild <mwild1@gmail.com>
parents: 2098
diff changeset
    74
				return 0, ret;
2964
49b5c87d2fa0 util.timer: When using libevent hold onto the event handle to stop it being collected (and the timer stopping). Fixes BOSH ghosts, thanks Flo, niekie, waqas.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    75
			elseif event_handle then
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    76
				return EVENT_LEAVE;
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    77
			end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    78
		end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    79
		, delay);
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    80
	end
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    81
end
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    82
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
    83
--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
    84
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
    85
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
    86
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
    87
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
    88
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
    89
local function _call() return _callback(_now, _id, _param); 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
    90
local function _traceback_handler(err) log("error", "Traceback[timer]: %s", traceback(tostring(err), 2)); 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
    91
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
    92
	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
    93
	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
    94
		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
    95
		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
    96
		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
    97
		_, _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
    98
		_now = now;
5879
95f1d5421fbc util.timer: Fix another variable name typo (thanks again zash).
Waqas Hussain <waqas20@gmail.com>
parents: 5878
diff changeset
    99
		_param = params[_id];
95f1d5421fbc util.timer: Fix another variable name typo (thanks again zash).
Waqas Hussain <waqas20@gmail.com>
parents: 5878
diff changeset
   100
		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
   101
		--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
   102
		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
   103
		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
   104
			h:insert(_callback, err + now, _id); -- re-add
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
   105
		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
   106
	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
   107
	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
   108
	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
   109
		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
   110
	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
   111
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
   112
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
   113
	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
   114
	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
   115
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
   116
	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
   117
	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
   118
	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
   119
		next_time = event_time;
5878
2b1c0c0a2ea6 util.timer: Fix variable name typo.
Waqas Hussain <waqas20@gmail.com>
parents: 5877
diff changeset
   120
		_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
   121
	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
   122
	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
   123
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
   124
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
   125
	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
   126
	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
   127
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
   128
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
   129
	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
   130
	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
   131
	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
   132
	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
   133
		next_time = event_time;
5878
2b1c0c0a2ea6 util.timer: Fix variable name typo.
Waqas Hussain <waqas20@gmail.com>
parents: 5877
diff changeset
   134
		_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
   135
	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
   136
	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
   137
end
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   138
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   139
return _M;