Repository navigation
Expand file tree
/
Copy pathUnitEventRouter.lua
More file actions
89 lines (78 loc) · 2 KB
/
Copy pathUnitEventRouter.lua
File metadata and controls
89 lines (78 loc) · 2 KB
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
local addonName, ns = ...
-- AceEvent registers unit events globally. This small router keeps high-volume
-- UNIT_* traffic scoped to only the units each ActionHud module displays.
local UnitEventRouter = {}
ns.UnitEventRouter = UnitEventRouter
local unpack = unpack or table.unpack
local function Dispatch(state, event, ...)
local registration = state.registrations[event]
if not registration then
return
end
local owner = state.owner
local callback = registration.callback
if type(callback) == "string" then
owner[callback](owner, event, ...)
else
callback(owner, event, ...)
end
end
local function ApplyRegistrations(state, generation)
if generation ~= state.generation then
return
end
state.scheduled = false
if not state.frame then
state.frame = CreateFrame("Frame")
state.frame:SetScript("OnEvent", function(_, event, ...)
Dispatch(state, event, ...)
end)
end
state.frame:UnregisterAllEvents()
for event, registration in pairs(state.registrations) do
pcall(state.frame.RegisterUnitEvent, state.frame, event, unpack(registration.units))
end
end
local function GetState(owner)
local state = owner.__actionHudUnitEvents
if not state then
state = {
owner = owner,
registrations = {},
generation = 0,
}
owner.__actionHudUnitEvents = state
end
return state
end
local function Schedule(state)
if state.scheduled then
return
end
state.generation = state.generation + 1
state.scheduled = true
local generation = state.generation
C_Timer.After(0, function()
ApplyRegistrations(state, generation)
end)
end
function UnitEventRouter:Register(owner, event, callback, ...)
local state = GetState(owner)
state.registrations[event] = {
callback = callback,
units = { ... },
}
Schedule(state)
end
function UnitEventRouter:UnregisterAll(owner)
local state = owner.__actionHudUnitEvents
if not state then
return
end
wipe(state.registrations)
state.generation = state.generation + 1
state.scheduled = false
if state.frame then
state.frame:UnregisterAllEvents()
end
end