forked from Kruithne/Shame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvents.lua
74 lines (63 loc) · 1.78 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
--[[
Shame (C) Kruithne <[email protected]>
Licensed under GNU General Public Licence version 3.
https://github.com/Kruithne/Shame
Events.lua - Module for handling/routing game events.
]]--
do
-- [[ Local Optimization ]] --
local Shame = Shame;
local type = type;
local pairs = pairs;
--[[
Shame.OnEvent_AddonLoaded
Invoked when the ADDON_LOADED event triggers.
self - Reference to the addon container.
addonName - Name of the addon which just loaded.
]]--
Shame.OnAddonLoaded = function(self, addonName)
if addonName == self.ADDON_NAME then
self:RemoveEventHandler("ADDON_LOADED");
self:OnLoad();
end
end
--[[
Shame.OnEvent
Invoked when a registered event occurred.
]]--
Shame.OnEvent = function(self, event, ...)
local handler = Shame.eventHandlers[event];
if handler then handler(Shame, ...); end
end
--[[
Shame.SetEventHandler
Register an event handler.
self - Reference to the addon container.
event - Identifer for the event.
handler - Function to handle the callback.
]]--
Shame.SetEventHandler = function(self, event, handler)
if type(event) == "table" then
for key, value in pairs(event) do
self:SetEventHandler(key, value);
end
else
self.eventFrame:RegisterEvent(event);
self.eventHandlers[event] = handler;
end
end
--[[
Shame.RemoveEventHandler
Unregister an existing event handler.
self - Reference to the addon container.
event - Identifer for the event.
]]--
Shame.RemoveEventHandler = function(self, event)
self.eventFrame:UnregisterEvent(event);
self.eventHandlers[event] = nil;
end
Shame.eventHandlers = {}; -- Stores event handlers.
Shame.eventFrame = CreateFrame("FRAME");
Shame.eventFrame:SetScript("OnEvent", Shame.OnEvent);
Shame:SetEventHandler("ADDON_LOADED", Shame.OnAddonLoaded);
end