-
Notifications
You must be signed in to change notification settings - Fork 1
/
Compat.lua
72 lines (57 loc) · 1.86 KB
/
Compat.lua
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
62
63
64
65
66
67
68
69
70
71
72
--[[
API: C_Timer
]]
local function GenerateTimer()
local Timer = CreateFrame("Frame")
local TimerObject = {}
Timer.Infinite = 0 -- -1 infinite,0 stop,1..n loop n times
Timer.ElapsedTime = 0
function Timer:Start(duration, callback)
if type(duration) ~= "number" then
duration = 0
end
self:SetScript("OnUpdate", function()
self.ElapsedTime = self.ElapsedTime + arg1
if self.ElapsedTime >= duration and type(callback) == "function" then
callback()
self.ElapsedTime = 0
if self.Infinite == 0 then
self:SetScript("OnUpdate", nil)
elseif self.Infinite > 0 then
self.Infinite = self.Infinite - 1
end
end
end)
end
function TimerObject:IsCancelled()
return not Timer:GetScript("OnUpdate")
end
function TimerObject:Cancel()
if Timer:GetScript("OnUpdate") then
Timer:SetScript("OnUpdate", nil)
Timer.Infinite = 0
Timer.ElapsedTime = 0
end
end
return Timer, TimerObject
end
C_Timer = C_Timer or {
After = function (duration, callback)
GenerateTimer():Start(duration, callback)
end,
NewTimer = function (duration, callback)
local timer, timerObj = GenerateTimer()
timer:Start(duration, callback)
return timerObj
end,
NewTicker = function (duration, callback, ...)
local timer, timerObj = GenerateTimer()
local iterations = unpack(arg)
if type(iterations) ~= "number" or iterations < 0 then
iterations = 0 -- will be infinite
end
timer.Infinite = iterations - 1
timer:Start(duration, callback)
return timerObj
end
}