Cacheable emmy lua

main
4n0n 2 years ago
parent ea4a549f35
commit 016491fa1f
  1. 7
      src/Cacheable/Cacheable.lua

@ -30,11 +30,14 @@ function Cacheable:__index(k)
end
-- When the object is accessed return the value
---@return string
function Cacheable:__tostring()
return "Bastion.__Cacheable(" .. tostring(self.value) .. ")"
end
-- Create
---@param value any
---@param cb fun():any
function Cacheable:New(value, cb)
local self = setmetatable({}, Cacheable)
@ -48,6 +51,7 @@ function Cacheable:New(value, cb)
end
-- Try to update the value
---@return nil
function Cacheable:TryUpdate()
if self.cache:IsCached("value") then
self.value = self.callback()
@ -55,16 +59,19 @@ function Cacheable:TryUpdate()
end
-- Update the value
---@return nil
function Cacheable:Update()
self.value = self.callback()
end
-- Set a new value
---@param value any
function Cacheable:Set(value)
self.value = value
end
-- Set a new callback
---@param cb fun():any
function Cacheable:SetCallback(cb)
self.callback = cb
end

Loading…
Cancel
Save