2022-06-09 04:43:37 +10:00
|
|
|
local events = {}
|
|
|
|
|
local timers = {}
|
2022-06-19 13:08:28 +10:00
|
|
|
local cbEvent = ('__ox_cb_%s')
|
2022-06-09 04:43:37 +10:00
|
|
|
|
2022-06-19 13:08:28 +10:00
|
|
|
RegisterNetEvent(cbEvent:format(cache.resource), function(key, ...)
|
2022-06-09 04:43:37 +10:00
|
|
|
local cb = events[key]
|
|
|
|
|
return cb and cb(...)
|
|
|
|
|
end)
|
2022-02-28 16:30:58 +11:00
|
|
|
|
|
|
|
|
---@param event string
|
2022-09-14 16:12:43 +10:00
|
|
|
---@param delay number | false 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
|
2022-09-14 16:12:43 +10:00
|
|
|
---@param delay number | false
|
2022-08-08 17:16:57 +10:00
|
|
|
---@param cb function|false
|
|
|
|
|
---@param ... any
|
|
|
|
|
---@return unknown?
|
2022-06-09 04:43:37 +10:00
|
|
|
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]
|
|
|
|
|
|
2022-06-19 13:08:28 +10:00
|
|
|
TriggerServerEvent(cbEvent:format(event), cache.resource, key, ...)
|
2022-06-09 04:43:37 +10:00
|
|
|
|
2022-08-08 17:16:57 +10:00
|
|
|
---@type promise | false
|
2022-06-09 04:43:37 +10:00
|
|
|
local promise = not cb and promise.new()
|
|
|
|
|
|
|
|
|
|
events[key] = function(response)
|
|
|
|
|
events[key] = nil
|
|
|
|
|
|
|
|
|
|
if promise then
|
2022-10-08 22:20:46 +11:00
|
|
|
return promise:resolve(response and { msgpack.unpack(response) } or {})
|
2022-06-09 04:43:37 +10:00
|
|
|
end
|
|
|
|
|
|
2022-10-25 03:02:44 +11:00
|
|
|
if cb and response then
|
|
|
|
|
cb(msgpack.unpack(response))
|
|
|
|
|
end
|
2022-06-09 04:43:37 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if promise then
|
|
|
|
|
return table.unpack(Citizen.Await(promise))
|
|
|
|
|
end
|
2022-02-28 16:30:58 +11:00
|
|
|
end
|
|
|
|
|
|
2022-09-14 16:12:43 +10:00
|
|
|
---@overload fun(event: string, delay: number | false, cb: function, ...)
|
2022-09-14 00:50:27 +10:00
|
|
|
lib.callback = setmetatable({}, {
|
2022-06-09 04:43:37 +10:00
|
|
|
__call = triggerServerCallback
|
|
|
|
|
})
|
2022-02-28 16:30:58 +11:00
|
|
|
|
|
|
|
|
---@param event string
|
2022-09-14 16:12:43 +10:00
|
|
|
---@param delay number | false prevent the event from being called for the given time
|
2022-02-28 16:30:58 +11:00
|
|
|
--- Sends an event to the server and halts the current thread until a response is returned.
|
2022-09-14 00:50:27 +10:00
|
|
|
function lib.callback.await(event, delay, ...)
|
2022-09-14 01:20:50 +10:00
|
|
|
return triggerServerCallback(nil, event, delay, false, ...)
|
2022-02-28 16:30:58 +11:00
|
|
|
end
|
|
|
|
|
|
2022-08-25 18:49:07 +10:00
|
|
|
local function callbackResponse(success, result, ...)
|
|
|
|
|
if not success then
|
|
|
|
|
if result then
|
|
|
|
|
return print(('^1SCRIPT ERROR: %s^0\n%s'):format(result , Citizen.InvokeNative(`FORMAT_STACK_TRACE` & 0xFFFFFFFF, nil, 0, Citizen.ResultAsString()) or ''))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
|
2022-10-08 22:20:46 +11:00
|
|
|
return msgpack.pack(result, ...)
|
2022-08-25 18:49:07 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local pcall = pcall
|
|
|
|
|
|
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.
|
2022-09-14 00:50:27 +10:00
|
|
|
function lib.callback.register(name, cb)
|
2022-06-19 13:08:28 +10:00
|
|
|
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
|
2022-08-25 18:49:07 +10:00
|
|
|
TriggerServerEvent(cbEvent:format(resource), key, callbackResponse(pcall(cb, ...)))
|
2022-06-09 05:15:27 +10:00
|
|
|
end)
|
|
|
|
|
end
|
|
|
|
|
|
2022-09-14 00:50:27 +10:00
|
|
|
return lib.callback
|