|
|
|
@ -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 |
|
|
|
|