-
Notifications
You must be signed in to change notification settings - Fork 1
/
Events.lua
113 lines (103 loc) · 3.44 KB
/
Events.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
--[[
Manage events for all state drivers
]]
local ADDON_NAME, Internal = ...
local External = _G[ADDON_NAME]
local EventHandler = CreateFrame("Frame")
EventHandler.targets = {}
local _RegisterEvent = EventHandler.RegisterEvent
local _UnregisterEvent = EventHandler.UnregisterEvent
function EventHandler:RegisterEvent(target, event, callback, prio)
if not self.targets[event] then
self.targets[event] = setmetatable({}, {__mode="k"})
end
if next(self.targets[event]) == nil then
pcall(function ()
_RegisterEvent(self, event)
end)
end
if not self.targets[event][target] then
self.targets[event][target] = {}
end
self.targets[event][target][callback or "OnEvent"] = prio or 0
end
function EventHandler:UnregisterEvent(target, event, callback)
if not self.targets[event] then
return
end
if not self.targets[event][target] then
return
end
if callback == nil then
self.targets[event][target] = nil
else
self.targets[event][target][callback or "OnEvent"] = nil
if next(self.targets[event][target]) == nil then
self.targets[event][target] = nil
end
end
if next(self.targets[event]) == nil then
pcall(function ()
_UnregisterEvent(self, event)
end)
self.targets[event] = nil
end
end
function EventHandler:UnregisterEventsFor(target)
for event in pairs(self.targets) do
self:UnregisterEvent(target, event)
end
end
function EventHandler:OnEvent(event, ...)
if self.targets[event] then
self.event = event
local sorted = {}
for target,callbacks in pairs(self.targets[event]) do
for callback,prio in pairs(callbacks) do
if type(callback) == "string" then
callback = target[callback]
end
sorted[#sorted+1] = {target = target, func = callback, prio = prio}
end
end
table.sort(sorted, function (a, b)
return a.prio < b.prio
end)
for _,callback in ipairs(sorted) do
if callback.target == Internal then
callback.func(event, ...)
else
callback.func(callback.target, event, ...)
end
end
self.event = nil
end
end
EventHandler:SetScript("OnEvent", EventHandler.OnEvent)
function Internal.GetActiveEvent()
return EventHandler.event
end
function External.TriggerEvent(event, ...)
EventHandler:OnEvent(event, ...)
end
function Internal.RegisterEvent(target, event, callback, prio)
if target == nil then
target = Internal
end
if type(target) == "string" then -- Optional target arguement
target, event, callback, prio = Internal, target, event, callback
end
assert(target ~= Internal or callback ~= nil, "callback required with no target set")
EventHandler:RegisterEvent(target, event, callback, prio)
end
function Internal.UnregisterEvent(target, event, callback)
if type(target) == "string" then -- Optional target arguement
target, event, callback = Internal, target, event
end
assert(target ~= Internal or callback ~= nil, "callback required with no target set")
EventHandler:UnregisterEvent(target, event, callback)
end
function Internal.UnregisterEventsFor(target)
assert(target ~= Internal, "cannot clear all events for Internal")
EventHandler:UnregisterEventsFor(target)
end