forked from Ostoic/RaidBrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.lua
More file actions
61 lines (50 loc) · 1.36 KB
/
timer.lua
File metadata and controls
61 lines (50 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
local function printf(...) DEFAULT_CHAT_FRAME:AddMessage('|cff0061ff[RaidBrowser]: ' .. format(...)) end
local function script_error(type, err)
local name, line, msg = err:match('%[string (".-")%]:(%d+): (.*)')
printf('%s error%s:\n %s',
type,
name and format(' in %s at line %d', name, line, msg) or '',
err
)
end
local timers = {}
function RaidBrowser.set_timer(interval, callback, recur, ...)
local timer = {
interval = interval,
callback = callback,
recur = recur,
update = 0,
...
}
timers[timer] = timer
return timer
end
function RaidBrowser.kill_timer(timer)
timers[timer] = nil
end
-- How often to check timers. Lower values are more CPU intensive.
local granularity = 0.1
local totalElapsed = 0
---@param self any
---@param elapsed integer
---@diagnostic disable-next-line: unused-local
local function OnUpdate(self, elapsed)
totalElapsed = totalElapsed + elapsed
if totalElapsed > granularity then
for _, t in pairs(timers) do
t.update = t.update + totalElapsed
if t.update > t.interval then
---@diagnostic disable-next-line: deprecated
local success, rv = pcall(t.callback, unpack(t))
if not rv and t.recur then
t.update = 0
else
timers[t] = nil
if not success then script_error('timer callback', rv) end
end
end
end
totalElapsed = 0
end
end
CreateFrame('Frame'):SetScript('OnUpdate', OnUpdate)