5.19.3
Add methods to the states/allstates table that helps with creating,
updating or remove states in an optimized way
## Advantage of using this function instead of doing a states[key] = {
... }
- If already created, update existing state, and return true if any
value was changed, this can help reduce amount of resources an aura use
- Automatically `return true` when using these functions and any change
was made
## Examples
```Lua
function(states, event, ...)
if event == "PLAYER_TARGET_CHANGED" then
if UnitExists("target") then
-- if state exists it's updated, not replaced
-- show & changed fields can be skipped
states:Update("", {
name = UnitName("target"),
duration = 5,
expirationTime = GetTime() + 5,
progressType = "timed",
autoHide = true
})
else
-- wipe
states:RemoveAll()
end
end
-- no need to return true
end
```
with clones
```Lua
function(states, event, ...)
local currentEssence = UnitPower("player", Enum.PowerType.Essence)
local maxEssence = UnitPowerMax("player", Enum.PowerType.Essence)
for i = 1, 6 do
if i > maxEssence then
states:Remove(i) -- wipe allstates[6]
else
local value = currentEssence >= i and 1 or 0
local newState = {
progressType = "static",
value = value,
total = 1
}
states:Update(i, newState)
end
end
-- no need to return true
end
```
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
if not WeakAuras.IsLibsOK() then return end
|
||||
local AddonName, Private = ...
|
||||
|
||||
local function fixMissingFields(state)
|
||||
if type(state) ~= "table" then return end
|
||||
-- set show
|
||||
if state.show == nil then
|
||||
state.show = true
|
||||
end
|
||||
end
|
||||
|
||||
local remove = function(states, key)
|
||||
local changed = false
|
||||
local state = states[key]
|
||||
if state then
|
||||
state.show = false
|
||||
state.changed = true
|
||||
states.__changed = true
|
||||
changed = true
|
||||
end
|
||||
return changed
|
||||
end
|
||||
|
||||
local removeAll = function(states)
|
||||
local changed = false
|
||||
for _, state in pairs(states) do
|
||||
state.show = false
|
||||
state.changed = true
|
||||
changed = true
|
||||
end
|
||||
if changed then
|
||||
states.__changed = true
|
||||
end
|
||||
return changed
|
||||
end
|
||||
|
||||
local function recurseUpdate(t1, t2)
|
||||
local changed = false
|
||||
for k, v in pairs(t2) do
|
||||
if type(v) == "table" and type(t1[k]) == "table" then
|
||||
if recurseUpdate(t1[k], v) then
|
||||
changed = true
|
||||
end
|
||||
else
|
||||
if t1[k] ~= v then
|
||||
t1[k] = v
|
||||
changed = true
|
||||
end
|
||||
end
|
||||
end
|
||||
return changed
|
||||
end
|
||||
|
||||
local update = function(states, key, newState)
|
||||
local changed = false
|
||||
local state = states[key]
|
||||
if state then
|
||||
fixMissingFields(newState)
|
||||
changed = recurseUpdate(state, newState)
|
||||
if changed then
|
||||
state.changed = true
|
||||
states.__changed = true
|
||||
end
|
||||
end
|
||||
return changed
|
||||
end
|
||||
|
||||
local create = function(states, key, newState)
|
||||
states[key] = newState
|
||||
states[key].changed = true
|
||||
states.__changed = true
|
||||
fixMissingFields(states[key])
|
||||
return true
|
||||
end
|
||||
|
||||
local createOrUpdate = function(states, key, newState)
|
||||
key = key or ""
|
||||
if states[key] then
|
||||
return update(states, key, newState)
|
||||
else
|
||||
return create(states, key, newState)
|
||||
end
|
||||
end
|
||||
|
||||
Private.allstatesMetatable = {
|
||||
__index = {
|
||||
Update = createOrUpdate,
|
||||
Remove = remove,
|
||||
RemoveAll = removeAll
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user