mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-19 07:56:02 +01:00
refactor(init): cache handling
lib.onCache serves as a helper to setup cache event listeners. Removed _cache table and index method. Only apply metatable to client-side cache (nothing exists on server yet). When cache is indexed, setup an event handler for the key. Remove some ox_core/ox_groups specific code (unused). Remove the updateCache event, and interval.
This commit is contained in:
68
init.lua
68
init.lua
@@ -72,11 +72,9 @@ lib = setmetatable({
|
|||||||
name = ox_lib,
|
name = ox_lib,
|
||||||
service = service,
|
service = service,
|
||||||
exports = {},
|
exports = {},
|
||||||
onCache = setmetatable({}, {
|
onCache = function(key, cb)
|
||||||
__call = function(self, key, cb)
|
AddEventHandler(('ox_lib:cache:%s'):format(key), cb)
|
||||||
self[key] = cb
|
|
||||||
end
|
end
|
||||||
})
|
|
||||||
}, {
|
}, {
|
||||||
__index = call,
|
__index = call,
|
||||||
__call = call,
|
__call = call,
|
||||||
@@ -121,46 +119,23 @@ function ClearInterval(id)
|
|||||||
intervals[id] = -1
|
intervals[id] = -1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
-- Cache
|
-- Cache
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
local ox = GetResourceState('ox_core') ~= 'missing' and setmetatable({}, {
|
cache = { resource = GetCurrentResourceName() }
|
||||||
__index = function()
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
}) or {
|
|
||||||
groups = GetResourceState('ox_groups') ~= 'missing',
|
|
||||||
}
|
|
||||||
|
|
||||||
local _cache = setmetatable({}, {
|
|
||||||
__index = function(self, key)
|
|
||||||
return rawset(self, key, export.cache(nil, key) or false)[key]
|
|
||||||
end,
|
|
||||||
|
|
||||||
__call = function(self)
|
|
||||||
table.wipe(self)
|
|
||||||
|
|
||||||
if ox.groups then
|
|
||||||
self.groups = setmetatable({}, {
|
|
||||||
__index = function(groups, index)
|
|
||||||
groups[index] = GlobalState['group:'..index]
|
|
||||||
return groups[index]
|
|
||||||
end
|
|
||||||
})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
})
|
|
||||||
|
|
||||||
cache = setmetatable({
|
|
||||||
resource = GetCurrentResourceName(),
|
|
||||||
}, {
|
|
||||||
__index = _cache,
|
|
||||||
__metatable = _cache
|
|
||||||
})
|
|
||||||
|
|
||||||
if service == 'client' then
|
if service == 'client' then
|
||||||
|
setmetatable(cache, {
|
||||||
|
__index = function(self, key)
|
||||||
|
AddEventHandler(('ox_lib:cache:%s'):format(key), function(value)
|
||||||
|
self[key] = value
|
||||||
|
end)
|
||||||
|
|
||||||
|
return rawset(self, key, export.cache(nil, key) or false)[key]
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
RegisterNetEvent(('%s:notify'):format(cache.resource), function(data)
|
RegisterNetEvent(('%s:notify'):format(cache.resource), function(data)
|
||||||
if locale then
|
if locale then
|
||||||
if data.title then
|
if data.title then
|
||||||
@@ -184,20 +159,3 @@ else
|
|||||||
TriggerClientEvent(notify, source, data)
|
TriggerClientEvent(notify, source, data)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Citizen.CreateThreadNow(function()
|
|
||||||
while true do
|
|
||||||
_cache()
|
|
||||||
Wait(60000)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
AddEventHandler('ox_lib:updateCache', function(data)
|
|
||||||
for key, value in pairs(data) do
|
|
||||||
if lib.onCache[key] then
|
|
||||||
lib.onCache[key](value)
|
|
||||||
end
|
|
||||||
|
|
||||||
_cache[key] = value
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user