Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allstates helper methods #5195

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That feels like the wrong order to me, "key, state" feels a lot more natural to me.

I know you did that for key to be optional, but I still feel that its the wrong order.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swapped them, and made update function check differences recursively (eg. for overlays)

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
5 changes: 2 additions & 3 deletions WeakAurasOptions/OptionsFrames/TextEditor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ end]=]
name = "Trigger State Updater",
snippet = [=[
function(allstates, event, ...)
allstates[""] = {
return allstates:Update({
show = true,
changed = true,
progressType = "static"||"timed",
Expand All @@ -149,8 +149,7 @@ function(allstates, event, ...)
icon = ,
stacks = ,
index = ,
}
return true
})
end]=]
},
}
Expand Down