mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-18 15:36:02 +01:00
feat(init): cache.__call metamethod for caching function results
See #385. Included formatting changes (tabs->spaces).
This commit is contained in:
53
init.lua
53
init.lua
@@ -1,6 +1,7 @@
|
|||||||
-- ox_lib <https://github.com/overextended/ox_lib>
|
---@meta
|
||||||
-- Copyright (C) 2021 Linden <https://github.com/thelindat>
|
---ox_lib <https://github.com/overextended/ox_lib>
|
||||||
-- LGPL-3.0-or-later <https://www.gnu.org/licenses/lgpl-3.0.en.html>
|
---Copyright (C) 2021 Linden <https://github.com/thelindat>
|
||||||
|
---LGPL-3.0-or-later <https://www.gnu.org/licenses/lgpl-3.0.en.html>
|
||||||
|
|
||||||
if not _VERSION:find('5.4') then
|
if not _VERSION:find('5.4') then
|
||||||
error('^1Lua 5.4 must be enabled in the resource manifest!^0', 2)
|
error('^1Lua 5.4 must be enabled in the resource manifest!^0', 2)
|
||||||
@@ -144,25 +145,51 @@ function ClearInterval(id)
|
|||||||
intervals[id] = -1
|
intervals[id] = -1
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
--[[
|
||||||
-- Cache
|
lua language server doesn't support generics when using @overload
|
||||||
-----------------------------------------------------------------------------------------------
|
see https://github.com/LuaLS/lua-language-server/issues/723
|
||||||
|
this function stub allows the following to work
|
||||||
|
|
||||||
cache = { game = GetGameName(), resource = GetCurrentResourceName() }
|
local key = cache('key', function() return 'abc' end) -- fff: 'abc'
|
||||||
local notify = ('__ox_notify_%s'):format(cache.resource)
|
local game = cache.game -- game: string
|
||||||
|
]]
|
||||||
|
|
||||||
if context == 'client' then
|
---@generic T
|
||||||
setmetatable(cache, {
|
---@param key string
|
||||||
__index = function(self, key)
|
---@param func fun(...: any): T
|
||||||
|
---@param timeout? number
|
||||||
|
---@return T
|
||||||
|
---Caches the result of a function, optionally clearing it after timeout ms.
|
||||||
|
function cache(key, func, timeout) end
|
||||||
|
|
||||||
|
cache = setmetatable({ game = GetGameName(), resource = GetCurrentResourceName() }, {
|
||||||
|
__index = context == 'client' and function(self, key)
|
||||||
AddEventHandler(('ox_lib:cache:%s'):format(key), function(value)
|
AddEventHandler(('ox_lib:cache:%s'):format(key), function(value)
|
||||||
self[key] = value
|
self[key] = value
|
||||||
end)
|
end)
|
||||||
|
|
||||||
return rawset(self, key, export.cache(nil, key) or false)[key]
|
return rawset(self, key, export.cache(nil, key) or false)[key]
|
||||||
|
end or nil,
|
||||||
|
|
||||||
|
__call = function(self, key, func, timeout)
|
||||||
|
local value = rawget(self, key)
|
||||||
|
|
||||||
|
if not value then
|
||||||
|
value = func()
|
||||||
|
|
||||||
|
rawset(self, key, value)
|
||||||
|
|
||||||
|
if timeout then SetTimeout(timeout, function() self[key] = nil end) end
|
||||||
|
end
|
||||||
|
|
||||||
|
return value
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
RegisterNetEvent(notify, function(data)
|
local notifyEvent = ('__ox_notify_%s'):format(cache.resource)
|
||||||
|
|
||||||
|
if context == 'client' then
|
||||||
|
RegisterNetEvent(notifyEvent, function(data)
|
||||||
if locale then
|
if locale then
|
||||||
if data.title then
|
if data.title then
|
||||||
data.title = locale(data.title) or data.title
|
data.title = locale(data.title) or data.title
|
||||||
@@ -188,6 +215,6 @@ else
|
|||||||
---@deprecated
|
---@deprecated
|
||||||
---@diagnostic disable-next-line: duplicate-set-field
|
---@diagnostic disable-next-line: duplicate-set-field
|
||||||
function lib.notify(playerId, data)
|
function lib.notify(playerId, data)
|
||||||
TriggerClientEvent(notify, playerId, data)
|
TriggerClientEvent(notifyEvent, playerId, data)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user