Skip to content

Commit 67f6f8c

Browse files
mrbudsemptyrivers
authored andcommitted
TSUHelpers: add states:Replace() and states:Get() functions, + bug fixes
new functions: states:Replace(key, newState) works like the Update function but set to nil fields missing in the new table states:Get:(key, field?) returns a state table, or one of it's fields if the parameter "field" is set, returns nil if it doesn't exists bug fixes: recurseUpdate should copy content of new tables instead of it's address don't copy the fields trigger & triggernum into allStates (this needs to be properly addressed)
1 parent dec0f24 commit 67f6f8c

File tree

1 file changed

+59
-11
lines changed

1 file changed

+59
-11
lines changed

Diff for: WeakAuras/TSUHelpers.lua

+59-11
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,52 @@ local removeAll = function(states)
4343
return changed
4444
end
4545

46-
local function recurseUpdate(t1, t2)
46+
local skipKeys = {
47+
trigger = true,
48+
triggernum = true
49+
}
50+
51+
local function recurseReplaceOrUpdate(t1, t2, isRoot, replace)
4752
local changed = false
48-
for k, v in pairs(t2) do
49-
if type(v) == "table" and type(t1[k]) == "table" then
50-
if recurseUpdate(t1[k], v) then
53+
if replace then
54+
-- Remove keys in t1 that are not in t2
55+
for k in pairs(t1) do
56+
if t2[k] == nil then
57+
t1[k] = nil
5158
changed = true
5259
end
60+
end
61+
end
62+
for k, v in pairs(t2) do
63+
if isRoot and skipKeys[k] then
64+
-- skip this key
5365
else
54-
if t1[k] ~= v then
55-
t1[k] = v
56-
changed = true
66+
if type(v) == "table" then
67+
if type(t1[k]) ~= "table" then
68+
t1[k] = {}
69+
changed = true
70+
end
71+
if recurseReplaceOrUpdate(t1[k], v, false, replace) then
72+
changed = true
73+
end
74+
else
75+
if t1[k] ~= v then
76+
t1[k] = v
77+
changed = true
78+
end
5779
end
5880
end
5981
end
6082
return changed
6183
end
6284

6385
---@type fun(states: states, key: key, newState: state): boolean
64-
local update = function(states, key, newState)
86+
local replaceOrUpdate = function(states, key, newState, replace)
6587
local changed = false
6688
local state = states[key]
6789
if state then
6890
fixMissingFields(newState)
69-
changed = recurseUpdate(state, newState)
91+
changed = recurseReplaceOrUpdate(state, newState, true, replace)
7092
if changed then
7193
state.changed = true
7294
states.__changed = true
@@ -88,7 +110,31 @@ end
88110
local createOrUpdate = function(states, key, newState)
89111
key = key or ""
90112
if states[key] then
91-
return update(states, key, newState)
113+
return replaceOrUpdate(states, key, newState, false)
114+
else
115+
return create(states, key, newState)
116+
end
117+
end
118+
119+
---@type fun(states: states, key: key, field: any?): any
120+
---return a state for a key, or a field of a state for a key/field
121+
local get = function(states, key, field)
122+
key = key or ""
123+
local state = states[key]
124+
if state then
125+
if field == nil then
126+
return state
127+
end
128+
return state[field] or nil
129+
end
130+
return nil
131+
end
132+
133+
---@type fun(states: states, key: key?, newState: state): boolean
134+
local createOrReplace = function(states, key, newState)
135+
key = key or ""
136+
if states[key] then
137+
return replaceOrUpdate(states, key, newState, true)
92138
else
93139
return create(states, key, newState)
94140
end
@@ -97,7 +143,9 @@ end
97143
Private.allstatesMetatable = {
98144
__index = {
99145
Update = createOrUpdate,
146+
Replace = createOrReplace,
100147
Remove = remove,
101-
RemoveAll = removeAll
148+
RemoveAll = removeAll,
149+
Get = get
102150
}
103151
}

0 commit comments

Comments
 (0)