mirror of
https://github.com/ND-Framework/ND_Core.git
synced 2026-08-17 05:56:01 +01:00
fix: allow multiple callbacks of a resource
This commit is contained in:
@@ -1,25 +1,29 @@
|
||||
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)
|
||||
startedResources[resourceName] = true
|
||||
local callback = callbacks[resourceName]
|
||||
if not callback then return end
|
||||
callback(true)
|
||||
stateChanged(resourceName, true)
|
||||
end)
|
||||
|
||||
AddEventHandler("onResourceStop", function(resourceName)
|
||||
startedResources[resourceName] = nil
|
||||
local callback = callbacks[resourceName]
|
||||
if not callback then return end
|
||||
callback(false)
|
||||
stateChanged(resourceName, false)
|
||||
end)
|
||||
|
||||
function NDCore.isResourceStarted(resourceName, cb)
|
||||
local started = GetResourceState(resourceName) == "started"
|
||||
if cb then
|
||||
startedResources[resourceName] = started
|
||||
callbacks[resourceName] = cb
|
||||
if not startedResources[resourceName] then
|
||||
startedResources[resourceName] = {}
|
||||
end
|
||||
local invokingResource = GetInvokingResource()
|
||||
startedResources[resourceName][invokingResource] = cb
|
||||
cb(started)
|
||||
end
|
||||
return started
|
||||
|
||||
Reference in New Issue
Block a user