Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 82 lines (69 sloc) 1.975 kb
198f889 @haste Move the event system to its own file as well.
haste authored
1 local parent, ns = ...
2 local oUF = ns.oUF
3 local Private = oUF.Private
4
5 local argcheck = Private.argcheck
6 local error = Private.error
7 local frame_metatable = Private.frame_metatable
8
9 -- Events
10 Private.OnEvent = function(self, event, ...)
11 if(not self:IsShown()) then return end
12 return self[event](self, event, ...)
13 end
14
15 local event_metatable = {
16 __call = function(funcs, self, ...)
17 for _, func in next, funcs do
18 func(self, ...)
19 end
20 end,
21 }
22
23 local RegisterEvent = frame_metatable.__index.RegisterEvent
24 function frame_metatable.__index:RegisterEvent(event, func)
25 argcheck(event, 2, 'string')
26
27 if(type(func) == 'string' and type(self[func]) == 'function') then
28 func = self[func]
29 end
30
31 local curev = self[event]
ece09d8 @haste Prevent double registration of the same function.
haste authored
32 local kind = type(curev)
198f889 @haste Move the event system to its own file as well.
haste authored
33 if(curev and func) then
ece09d8 @haste Prevent double registration of the same function.
haste authored
34 if(kind == 'function' and curev ~= func) then
198f889 @haste Move the event system to its own file as well.
haste authored
35 self[event] = setmetatable({curev, func}, event_metatable)
ece09d8 @haste Prevent double registration of the same function.
haste authored
36 elseif(kind == 'table') then
198f889 @haste Move the event system to its own file as well.
haste authored
37 for _, infunc in next, curev do
38 if(infunc == func) then return end
39 end
40
41 table.insert(curev, func)
42 end
43 elseif(self:IsEventRegistered(event)) then
44 return
45 else
46 if(type(func) == 'function') then
47 self[event] = func
48 elseif(not self[event]) then
49 return error("Style [%s] attempted to register event [%s] on unit [%s] with a handler that doesn't exist.", self.style, event, self.unit or 'unknown')
50 end
51
52 RegisterEvent(self, event)
53 end
54 end
55
56 local UnregisterEvent = frame_metatable.__index.UnregisterEvent
57 function frame_metatable.__index:UnregisterEvent(event, func)
58 argcheck(event, 2, 'string')
59
60 local curev = self[event]
61 if(type(curev) == 'table' and func) then
62 for k, infunc in next, curev do
63 if(infunc == func) then
88889de @haste Convert our event handler back to a pure function if we no longer have m...
haste authored
64 table.remove(curev, k)
198f889 @haste Move the event system to its own file as well.
haste authored
65
88889de @haste Convert our event handler back to a pure function if we no longer have m...
haste authored
66 local n = #curev
67 if(n == 1) then
68 local _, handler = next(curev)
69 self[event] = handler
70 elseif(n == 0) then
198f889 @haste Move the event system to its own file as well.
haste authored
71 UnregisterEvent(self, event)
72 end
73
74 break
75 end
76 end
a53da94 @haste Don't assume the call to :UnregisterEvent() is correct when there's only...
haste authored
77 elseif(curev == func) then
198f889 @haste Move the event system to its own file as well.
haste authored
78 self[event] = nil
79 UnregisterEvent(self, event)
80 end
81 end
Something went wrong with that request. Please try again.