Fix server lag when loading scripts

This was my bad, set up functions that requested the cache from server, didn't realise it was duplicating it and spamming it

This moves the requests to a separate file and uses an export instead

Also hopefully fixes ox_lib images being an arse
This commit is contained in:
Jim Shield
2025-06-26 23:15:33 +01:00
parent e204e6153d
commit e87aa8d74d
3 changed files with 68 additions and 43 deletions

32
clientFrameworkCache.lua Normal file
View File

@@ -0,0 +1,32 @@
if _G.__bridge_cache_loaded then return end
_G.__bridge_cache_loaded = true
_G.BridgeCache = {
hasCache = false,
data = nil,
queue = {},
}
RegisterNetEvent("jim_bridge:receiveCache", function(data)
if _G.BridgeCache.hasCache then return end
_G.BridgeCache.data = data
_G.BridgeCache.hasCache = true
for _, cb in ipairs(_G.BridgeCache.queue) do
cb(data)
end
_G.BridgeCache.queue = nil
end)
-- Only send the request ONCE per client session
TriggerServerEvent("jim_bridge:requestCache")
-- Exported async function
function GetBridgeCache(callback)
if _G.BridgeCache.hasCache then
callback(_G.BridgeCache.data)
else
table.insert(_G.BridgeCache.queue, callback)
end
end
exports("GetBridgeCache", GetBridgeCache)

View File

@@ -22,5 +22,6 @@ server_scripts {
}
client_scripts {
'clientFrameworkCache.lua',
'ui_modules/*.lua'
}

View File

@@ -64,22 +64,8 @@ if IsDuplicityVersion() then
debugPrint("^6Bridge^7: ^2Shared cache successfully loaded from export^7.")
--print(countTable(Items), countTable(Vehicles), countTable(Jobs))
else
local hasCache = false
-- 🔹 Client Side: Request from server
cache = {}
RegisterNetEvent("jim_bridge:receiveCache", function(data)
if not hasCache then
cache = data
hasCache = true
else
return
end
end)
TriggerServerEvent("jim_bridge:requestCache")
while not cache or not next(cache) do Wait(50) end
-- Shared bridge cache - prevent duplicate listeners
exports["jim_bridge"]:GetBridgeCache(function(cache)
Items = cache.Items or {}
Vehicles = cache.Vehicles or {}
Jobs = cache.Jobs or {}
@@ -87,6 +73,7 @@ else
InventoryWeight = cache.InventoryWeight or InventoryWeight
InventorySlots = cache.InventorySlots or 50
CreateThread(function()
if isStarted(ESXExport) then
for _, v in pairs(Vehicles) do
Vehicles[v.model] = {
@@ -98,15 +85,20 @@ else
}
end
end
if isStarted(OXInv) then
for k, v in pairsByKeys(Items) do
for k, v in pairs(Items) do
local tempInfo = exports[OXInv]:Items(k)
if tempInfo and tempInfo.client then
Items[k].image = (tempInfo.client and tempInfo.client.image) and tempInfo.client.image:gsub("nui://"..OXInv.."/web/images/", "") or k..".png"
Items[k].hunger = tempInfo.client and tempInfo.client.hunger
Items[k].thirst = tempInfo.client and tempInfo.client.thirst
Items[k].image = k..".png" -- Set default image
if tempInfo and tempInfo.client then -- if client info, check for hard set image
Items[k].image = (tempInfo.client.image) and tempInfo.client.image:gsub("nui://"..OXInv.."/web/images/", "") or Items[k].image
Items[k].hunger = tempInfo.client.hunger
Items[k].thirst = tempInfo.client.thirst
end
end
end
end)
debugPrint("^6Bridge^7: ^2Shared cache successfully loaded from export^7.")
end)
end