Skip to content

Commit

Permalink
usa metatable
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbuds committed Jun 28, 2024
1 parent 96781cb commit 3be324e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 40 deletions.
71 changes: 32 additions & 39 deletions WeakAuras/TSUHelpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,40 @@ local Private = select(2, ...)
---@alias key string | integer
---@alias states table<key, state>

---@type fun(state: state): boolean
---@type fun(state: state)
local function fixMissingFields(state)
if type(state) ~= "table" then return false end
local changed = false
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

Private.TSU = {}

---@type fun(states: states, key: key?): boolean
Private.TSU.wipe = function(states, key)
---@type fun(states: states): boolean
local removeAll = function(states)
local changed = false
if key then
local state = states[key]
if state then
state.show = false
state.changed = true
changed = true
end
else
for _, state in pairs(states) do
state.show = false
state.changed = true
changed = true
end
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
Private.TSU.update = function(states, newState, key)
local update = function(states, newState, key)
key = key or ""
local changed = false
local state = states[key]
Expand All @@ -63,32 +60,28 @@ Private.TSU.update = function(states, newState, key)
end

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

--- Create, Update or Delete states
--- This function is aliased as "tsu" in aura environment
--- Return true if any change was made, otherwise return false
--- Examples:
--- tsu(states, { name = "moo", duration = 5, spelId = 17}) => create or update a "" clone
--- automatically set fields expirationTime, progressType, icon, show, changed
--- tsu(states, { name = "moo", duration = 5, spelId = 17}, "keyName") => same but set clone with key "KeyName"
--- tsu(states, nil, "keyName") => wipe "keyName" clone
--- tsu(states) => wipe all clones
---@type fun(states: states, newState: state?, key: key?): boolean
Private.TSU.createOrUpdateOrWipe = function(states, newState, key)
if newState == nil then
return Private.TSU.wipe(states, key)
end
---@type fun(states: states, newState: state, key: key?): boolean
local createOrUpdate = function(states, newState, key)
key = key or ""
if states[key] then
return Private.TSU.update(states, newState, key)
return update(states, newState, key)
else
return Private.TSU.create(states, newState, key)
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

0 comments on commit 3be324e

Please sign in to comment.