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 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
|
||||||
|
|||||||
Reference in New Issue
Block a user