Skip to content

Commit

Permalink
Add helper methods to allstates table
Browse files Browse the repository at this point in the history
allstates:Update(state, key)
  create or update a state table
  key is nilable and default to ""
  state.show and state.changed are automatically set

allstates:Remove(key)
  set state.show = false & state.changed = true for allstates[key]

allstates:RemoveAll()
  set state.show = false & state.changed = true for each state of allstates

Each method return true if any change was made to the allstates table
  • Loading branch information
mrbuds committed Jun 28, 2024
1 parent 7e748d2 commit cc06b46
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 2 deletions.
2 changes: 1 addition & 1 deletion WeakAuras/GenericTrigger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4267,7 +4267,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);
Expand Down
85 changes: 85 additions & 0 deletions WeakAuras/TSUHelpers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
if not WeakAuras.IsLibsOK() then return end
---@type string
local AddonName = ...
---@class Private
local Private = select(2, ...)

---@alias key string | integer
---@alias states table<key, state>

---@type fun(state: state)
local function fixMissingFields(state)
if type(state) ~= "table" then return end
-- set show
if state.show == nil then
state.show = true
end
end

---@type fun(states: states, key: key): boolean
local remove = function(states, key)
local changed = false
local state = states[key]
if state then
state.show = false
state.changed = true
changed = true
end
return changed
end

---@type fun(states: states): boolean
local removeAll = function(states)
local changed = false
for _, state in pairs(states) do
state.show = false
state.changed = true
changed = true
end
return changed
end

---@type fun(states: states, newState: state, key: key): boolean
local update = function(states, newState, key)
local changed = false
local state = states[key]
if state then
fixMissingFields(newState)
for k, v in pairs(newState) do
if state[k] ~= v then
state[k] = v
changed = true
end
end
if changed then
state.changed = true
end
end
return changed
end

---@type fun(states: states, newState: state, key: key): boolean
local create = function(states, newState, key)
states[key] = newState
states[key].changed = true
fixMissingFields(states[key])
return true
end

---@type fun(states: states, newState: state, key: key?): boolean
local createOrUpdate = function(states, newState, key)
key = key or ""
if states[key] then
return update(states, newState, key)
else
return create(states, newState, key)
end
end

Private.allstatesMetatable = {
__index = {
Update = createOrUpdate,
Remove = remove,
RemoveAll = removeAll
}
}
4 changes: 3 additions & 1 deletion WeakAuras/WeakAuras.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4404,7 +4404,9 @@ function WeakAuras.GetTriggerStateForTrigger(id, triggernum)
if (triggernum == -1) then
return Private.GetGlobalConditionState();
end
triggerState[id][triggernum] = triggerState[id][triggernum] or {}
if triggerState[id][triggernum] == nil then
triggerState[id][triggernum] = setmetatable({}, Private.allstatesMetatable)
end
return triggerState[id][triggernum];
end

Expand Down
1 change: 1 addition & 0 deletions WeakAuras/WeakAuras.toc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ GenericTrigger.lua
BossMods.lua

# Helper Systems
TSUHelpers.lua
AuraWarnings.lua
AuraEnvironment.lua
AuraEnvironmentWrappedSystems.lua
Expand Down
1 change: 1 addition & 0 deletions WeakAuras/WeakAuras_Cata.toc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ GenericTrigger.lua
BossMods.lua

# Helper Systems
TSUHelpers.lua
AuraWarnings.lua
AuraEnvironment.lua
AuraEnvironmentWrappedSystems.lua
Expand Down
1 change: 1 addition & 0 deletions WeakAuras/WeakAuras_Vanilla.toc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ GenericTrigger.lua
BossMods.lua

# Helper Systems
TSUHelpers.lua
AuraWarnings.lua
AuraEnvironment.lua
AuraEnvironmentWrappedSystems.lua
Expand Down

0 comments on commit cc06b46

Please sign in to comment.