Skip to content

Commit a67c47f

Browse files
committed
perf: UnitSet conditions now unregister their requesting icon as a condition update requester when that icon is hidden/inactive.
1 parent 22273f6 commit a67c47f

File tree

1 file changed

+65
-23
lines changed

1 file changed

+65
-23
lines changed

Components/Core/Units/Units.lua

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,52 @@ local unitSetsByEvent = setmetatable({}, {
7575
})
7676

7777

78+
79+
-- Create a single UpdateTableManager instance to track all unit condition mappings
80+
local UpdateManager = TMW.Classes.UpdateTableManager:New()
81+
UpdateManager:UpdateTable_Set()
82+
local ByConditionObject = UpdateManager:UpdateTable_CreateIndexedView("ByConditionObject", function(target) return target.ConditionObject end)
83+
local ByUnitSet = UpdateManager:UpdateTable_CreateIndexedView("ByUnitSet", function(target) return target.UnitSet end)
84+
local ByRequester = UpdateManager:UpdateTable_CreateIndexedView("ByRequester", function(target) return target.Requester end)
85+
86+
local function TMW_CNDT_OBJ_PASSING_CHANGED(event, ConditionObject, failed)
87+
local targets = ByConditionObject[ConditionObject]
88+
if targets then
89+
for _, target in ipairs(targets) do
90+
target.UnitSet:Update()
91+
end
92+
end
93+
end
94+
95+
local function TMW_ICON_DATA_CHANGED_SHOWN(event, icon, shown)
96+
local targets = ByRequester[icon]
97+
if targets then
98+
for _, target in ipairs(targets) do
99+
target.ConditionObject:RequestAutoUpdates(icon, shown)
100+
end
101+
end
102+
end
103+
104+
UpdateManager.UpdateTable_OnUsed = function(self)
105+
TMW:RegisterCallback("TMW_CNDT_OBJ_PASSING_CHANGED", TMW_CNDT_OBJ_PASSING_CHANGED)
106+
TMW:RegisterCallback("TMW_ICON_DATA_CHANGED_SHOWN", TMW_ICON_DATA_CHANGED_SHOWN)
107+
end
108+
109+
UpdateManager.UpdateTable_OnUnused = function(self)
110+
TMW:UnregisterCallback("TMW_CNDT_OBJ_PASSING_CHANGED", TMW_CNDT_OBJ_PASSING_CHANGED)
111+
TMW:UnregisterCallback("TMW_ICON_DATA_CHANGED_SHOWN", TMW_ICON_DATA_CHANGED_SHOWN)
112+
end
113+
114+
TMW:RegisterCallback("TMW_GLOBAL_UPDATE", function()
115+
for _, target in ipairs(UpdateManager.UpdateTable_UpdateTable) do
116+
target.ConditionObject:RequestAutoUpdates(target.Requester, false)
117+
end
118+
119+
UpdateManager:UpdateTable_UnregisterAll()
120+
end)
121+
122+
123+
78124
-- Public Methods/Stuff:
79125
function TMW:GetUnits(icon, setting, Conditions)
80126
local iconName = (icon and icon:GetName() or "<icon>")
@@ -96,16 +142,27 @@ function TMW:GetUnits(icon, setting, Conditions)
96142
local UnitSet = UNITS:GetUnitSet(setting, serializedConditions, nil, Conditions, conditionsParent)
97143

98144
if UnitSet.ConditionObjects then
145+
-- Only request condition updates for icons when they are shown
146+
local shouldRequestUpdates = true
147+
local requester = icon or UnitSet
148+
if requester.IsIcon then
149+
shouldRequestUpdates = icon.attributes.shown
150+
end
151+
152+
-- Register condition objects with the shared UpdateManager
99153
for i, ConditionObject in ipairs(UnitSet.ConditionObjects) do
100-
ConditionObject:RequestAutoUpdates(UnitSet, true)
154+
ConditionObject:RequestAutoUpdates(requester, shouldRequestUpdates)
155+
-- Register for state changes AFTER requesting updates.
156+
-- Requesting updates will trigger checks on all the objects.
157+
-- We don't want each check to update the unit set one by one.
158+
-- We'll update after they're all registered.
159+
UpdateManager:UpdateTable_Register({
160+
UnitSet = UnitSet,
161+
ConditionObject = ConditionObject,
162+
Requester = requester
163+
})
101164
end
102165

103-
-- Register for state changes AFTER requesting updates.
104-
-- Requesting updates will trigger checks on all the objects.
105-
-- We don't want each check to update the unit set one by one.
106-
-- We'll update after they're all registered.
107-
TMW:RegisterCallback("TMW_CNDT_OBJ_PASSING_CHANGED", UnitSet)
108-
109166
-- Perform an update now that all of the conditions (if any) are in their initial state.
110167
UnitSet:Update()
111168
end
@@ -329,11 +386,7 @@ local UnitSet = TMW:NewClass("UnitSet"){
329386
self:Update()
330387
end,
331388

332-
TMW_CNDT_OBJ_PASSING_CHANGED = function(self, event, ConditionObject, failed)
333-
if self.ConditionObjects[ConditionObject] then
334-
self:Update()
335-
end
336-
end,
389+
337390

338391
Update = function(self, forceNoExists)
339392
local originalUnits, exposedUnits, translatedUnits =
@@ -446,17 +499,6 @@ local UnitSet = TMW:NewClass("UnitSet"){
446499
end,
447500
}
448501

449-
TMW:RegisterCallback("TMW_GLOBAL_UPDATE", function()
450-
for i, unitSet in ipairs(UnitSet.instances) do
451-
if unitSet.ConditionObjects then
452-
for i, ConditionObject in ipairs(unitSet.ConditionObjects) do
453-
ConditionObject:RequestAutoUpdates(unitSet, false)
454-
end
455-
TMW:UnregisterCallback("TMW_CNDT_OBJ_PASSING_CHANGED", unitSet)
456-
end
457-
end
458-
end)
459-
460502
function UNITS:NormalizeUnitString(unitSettings)
461503
unitSettings = TMW:CleanString(unitSettings)
462504
:lower() -- all units should be lowercase

0 commit comments

Comments
 (0)