add update function to cache

This commit is contained in:
JericoFX committed 2025-05-12 14:31:33 -03:00
1 parent 3ea66043ea
commit c1c5626df3
1 file changed
+16
+16
View File
@@ -139,6 +139,22 @@ function Cache.OnChange(name, onChange)
cache.OnChange[#cache.OnChange + 1] = onChange
end
---@param name string
---@param newValue any
function Cache.Update(name, newValue)
assert(name, "Cache name is required.")
local _name = tostring(name)
local cache = Cache.Caches[_name]
assert(cache, "Cache with name '" .. _name .. "' does not exist.")
local oldValue = cache.Value
if oldValue ~= newValue then
cache.Value = newValue
for i = 1, #cache.OnChange do
cache.OnChange[i](newValue, oldValue)
end
end
end
---@param name string
function Cache.Remove(name)
assert(name, "Cache name is required.")