2022-06-09 04:43:37 +10:00
|
|
|
local events = {}
|
|
|
|
|
local timers = {}
|
|
|
|
|
local cbEvent = ('__cb_%s'):format(cache.resource)
|
|
|
|
|
|
|
|
|
|
RegisterNetEvent(cbEvent, function(key, ...)
|
|
|
|
|
local cb = events[key]
|
|
|
|
|
return cb and cb(...)
|
|
|
|
|
end)
|
2022-02-28 16:30:58 +11:00
|
|
|
|
|
|
|
|
---@param event string
|
|
|
|
|
---@param delay number prevent the event from being called for the given time
|
2022-06-09 04:43:37 +10:00
|
|
|
local function eventTimer(event, delay)
|
|
|
|
|
if delay and type(delay) == 'number' and delay > 0 then
|
2022-02-28 16:30:58 +11:00
|
|
|
local time = GetGameTimer()
|
2022-06-09 04:43:37 +10:00
|
|
|
|
|
|
|
|
if (timers[event] or 0) > time then
|
2022-02-28 16:30:58 +11:00
|
|
|
return false
|
|
|
|
|
end
|
2022-06-09 04:43:37 +10:00
|
|
|
|
|
|
|
|
timers[event] = time + delay
|
2022-02-28 16:30:58 +11:00
|
|
|
end
|
2022-06-09 04:43:37 +10:00
|
|
|
|
2022-02-28 16:30:58 +11:00
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
|
2022-06-09 04:43:37 +10:00
|
|
|
---@param _ any
|
|
|
|
|
---@param event string
|
|
|
|
|
---@param delay number
|
|
|
|
|
---@param cb function
|
|
|
|
|
---@param ... unknown
|
|
|
|
|
---@return unknown
|
|
|
|
|
local function triggerServerCallback(_, event, delay, cb, ...)
|
|
|
|
|
if not eventTimer(event, delay) then return end
|
|
|
|
|
|
|
|
|
|
local key
|
|
|
|
|
|
|
|
|
|
repeat
|
|
|
|
|
key = ('%s:%s'):format(event, math.random(0, 100000))
|
|
|
|
|
until not events[key]
|
|
|
|
|
|
|
|
|
|
TriggerServerEvent(('__cb_%s'):format(event), key, ...)
|
|
|
|
|
|
|
|
|
|
local promise = not cb and promise.new()
|
|
|
|
|
|
|
|
|
|
events[key] = function(response)
|
|
|
|
|
events[key] = nil
|
|
|
|
|
|
|
|
|
|
if promise then
|
|
|
|
|
return promise:resolve(response)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
cb(table.unpack(response))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if promise then
|
|
|
|
|
return table.unpack(Citizen.Await(promise))
|
|
|
|
|
end
|
2022-02-28 16:30:58 +11:00
|
|
|
end
|
|
|
|
|
|
2022-06-09 04:43:37 +10:00
|
|
|
---@overload fun(event: string, delay: number, cb: function, ...)
|
|
|
|
|
local callback = setmetatable({}, {
|
|
|
|
|
__call = triggerServerCallback
|
|
|
|
|
})
|
2022-02-28 16:30:58 +11:00
|
|
|
|
|
|
|
|
---@param event string
|
|
|
|
|
---@param delay number prevent the event from being called for the given time
|
|
|
|
|
--- Sends an event to the server and halts the current thread until a response is returned.
|
2022-02-28 17:14:18 +11:00
|
|
|
function callback.await(event, delay, ...)
|
2022-06-09 04:43:37 +10:00
|
|
|
return triggerServerCallback(_, event, delay, false, ...)
|
2022-02-28 16:30:58 +11:00
|
|
|
end
|
|
|
|
|
|
2022-06-09 05:15:27 +10:00
|
|
|
---@param name string
|
|
|
|
|
---@param cb function
|
|
|
|
|
--- Registers an event handler and callback function to respond to server requests.
|
|
|
|
|
function callback.register(name, cb)
|
|
|
|
|
name = ('__cb_%s'):format(name)
|
|
|
|
|
|
|
|
|
|
RegisterNetEvent(name, function(key, ...)
|
|
|
|
|
TriggerServerEvent(cbEvent, key, { cb(...) })
|
|
|
|
|
end)
|
|
|
|
|
end
|
|
|
|
|
|
2022-06-09 04:43:37 +10:00
|
|
|
return callback
|