fix: allow multiple callbacks of a resource

This commit is contained in:
Andyyy7666
2023-10-25 21:47:43 +02:00
parent 87cb479ce4
commit 40e74f6944

View File

@@ -1,25 +1,29 @@
local startedResources = {} local startedResources = {}
local callbacks = {}
local function stateChanged(resourceName, state)
local callbacks = startedResources[resourceName]
if not callbacks then return end
for _, cb do
cb(state)
end
end
AddEventHandler("onResourceStart", function(resourceName) AddEventHandler("onResourceStart", function(resourceName)
startedResources[resourceName] = true stateChanged(resourceName, true)
local callback = callbacks[resourceName]
if not callback then return end
callback(true)
end) end)
AddEventHandler("onResourceStop", function(resourceName) AddEventHandler("onResourceStop", function(resourceName)
startedResources[resourceName] = nil stateChanged(resourceName, false)
local callback = callbacks[resourceName]
if not callback then return end
callback(false)
end) end)
function NDCore.isResourceStarted(resourceName, cb) function NDCore.isResourceStarted(resourceName, cb)
local started = GetResourceState(resourceName) == "started" local started = GetResourceState(resourceName) == "started"
if cb then if cb then
startedResources[resourceName] = started if not startedResources[resourceName] then
callbacks[resourceName] = cb startedResources[resourceName] = {}
end
local invokingResource = GetInvokingResource()
startedResources[resourceName][invokingResource] = cb
cb(started) cb(started)
end end
return started return started