-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.lua
79 lines (66 loc) · 1.72 KB
/
event.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
73
74
75
76
77
78
79
local _, ns = ...
local SyLevel = ns.SyLevel
local argcheck = SyLevel.argcheck
local eventFrame = CreateFrame("Frame")
eventFrame:SetScript("OnEvent", function(self, event, ...)
return SyLevel[event](SyLevel, event, ...)
end)
local eventMetatable = {
__call = function(funcs, self, ...)
for _, func in next, funcs do
func(self, ...)
end
end,
}
function SyLevel:RegisterEvent(event, func)
argcheck(event, 2, "string")
if (type(func) == "string" and type(self[func]) == "function") then
func = self[func]
end
local curev = self[event]
local kind = type(curev)
if (curev and func) then
if (kind == "function" and curev ~= func) then
self[event] = setmetatable({curev, func}, eventMetatable)
elseif (kind == "table") then
for _, infunc in next, curev do
if (infunc == func) then return end
end
table.insert(curev, func)
end
elseif (eventFrame:IsEventRegistered(event)) then
return
else
if (type(func) == "function") then
self[event] = func
elseif (not self[event]) then
return error("Handler for event [%s] does not exist.", event)
end
eventFrame:RegisterEvent(event)
end
end
function SyLevel:IsEventRegistered(event)
return eventFrame:IsEventRegistered(event)
end
function SyLevel:UnregisterEvent(event, func)
argcheck(event, 2, "string")
local curev = self[event]
if (type(curev) == "table" and func) then
for k, infunc in next, curev do
if (infunc == func) then
table.remove(curev, k)
local n = #curev
if (n == 1) then
local _, handler = next(curev)
self[event] = handler
elseif (n == 0) then
eventFrame:UnregisterEvent(event)
end
break
end
end
elseif (curev == func) then
self[event] = nil
eventFrame:UnregisterEvent(event)
end
end