Software /
code /
prosody
Comparison
util/timer.lua @ 6932:f5fd2c5cdf28
util.timer: If possible, close the existing timer handle in order to have only one
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 22 Nov 2015 17:25:44 +0100 |
parent | 6931:5c2c8aeb4690 |
child | 7015:17e275e8bd79 |
comparison
equal
deleted
inserted
replaced
6931:5c2c8aeb4690 | 6932:f5fd2c5cdf28 |
---|---|
17 | 17 |
18 local _ENV = nil; | 18 local _ENV = nil; |
19 | 19 |
20 local _add_task = server.add_task; | 20 local _add_task = server.add_task; |
21 | 21 |
22 local _server_timer; | |
22 local _active_timers = 0; | 23 local _active_timers = 0; |
23 local h = indexedbheap.create(); | 24 local h = indexedbheap.create(); |
24 local params = {}; | 25 local params = {}; |
25 local next_time = nil; | 26 local next_time = nil; |
26 local _id, _callback, _now, _param; | 27 local _id, _callback, _now, _param; |
64 | 65 |
65 local id = h:insert(callback, event_time); | 66 local id = h:insert(callback, event_time); |
66 params[id] = param; | 67 params[id] = param; |
67 if next_time == nil or event_time < next_time then | 68 if next_time == nil or event_time < next_time then |
68 next_time = event_time; | 69 next_time = event_time; |
69 _active_timers = _active_timers + 1; | 70 if _server_timer then |
70 _add_task(next_time - current_time, _on_timer); | 71 _server_timer:close(); |
72 _server_timer = nil; | |
73 else | |
74 _active_timers = _active_timers + 1; | |
75 end | |
76 _server_timer = _add_task(next_time - current_time, _on_timer); | |
71 end | 77 end |
72 return id; | 78 return id; |
73 end | 79 end |
74 local function stop(id) | 80 local function stop(id) |
75 params[id] = nil; | 81 params[id] = nil; |