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:
NoM0Re
2025-02-24 22:10:45 +01:00
committed by GitHub
parent 0e0c49ec70
commit 7c7869494e
22 changed files with 140 additions and 1689 deletions
+5 -4
View File
@@ -667,11 +667,12 @@ local function RunTriggerFunc(allStates, data, id, triggernum, event, arg1, arg2
else
ok, returnValue = pcall(data.triggerFunc, allStates, event, arg1, arg2, ...);
end
if not ok then
errorHandler(returnValue)
elseif ok and returnValue then
if (ok and (returnValue or (returnValue ~= false and allStates.__changed))) then
updateTriggerState = true;
elseif not ok then
errorHandler(returnValue)
end
allStates.__changed = nil
for key, state in pairs(allStates) do
if (type(state) ~= "table") then
errorHandler(string.format(L["All States table contains a non table at key: '%s'."], key))
@@ -3852,7 +3853,7 @@ function GenericTrigger.GetOverlayInfo(data, triggernum)
count = variables.additionalProgress;
end
else
local allStates = {};
local allStates = setmetatable({}, Private.allstatesMetatable)
Private.ActivateAuraEnvironment(data.id);
RunTriggerFunc(allStates, events[data.id][triggernum], data.id, triggernum, "OPTIONS");
Private.ActivateAuraEnvironment(nil);