Cacheable emmy lua

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

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

Loading…
Cancel
Save