Scripts/resources/[standalone]/ox_lib/imports/callback/client.lua

118 lines
3.4 KiB
Lua
Raw Normal View History

2024-12-30 10:15:34 +00:00
local pendingCallbacks = {}
2024-12-29 20:02:43 +00:00
local timers = {}
2024-12-30 10:15:34 +00:00
local cbEvent = '__ox_cb_%s'
local callbackTimeout = GetConvarInt('ox:callbackTimeout', 300000)
2024-12-29 20:02:43 +00:00
RegisterNetEvent(cbEvent:format(cache.resource), function(key, ...)
2024-12-30 10:15:34 +00:00
local cb = pendingCallbacks[key]
pendingCallbacks[key] = nil
return cb and cb(...)
2024-12-29 20:02:43 +00:00
end)
---@param event string
2024-12-30 10:15:34 +00:00
---@param delay? number | false prevent the event from being called for the given time
2024-12-29 20:02:43 +00:00
local function eventTimer(event, delay)
2024-12-30 10:15:34 +00:00
if delay and type(delay) == 'number' and delay > 0 then
local time = GetGameTimer()
2024-12-29 20:02:43 +00:00
2024-12-30 10:15:34 +00:00
if (timers[event] or 0) > time then
return false
end
2024-12-29 20:02:43 +00:00
2024-12-30 10:15:34 +00:00
timers[event] = time + delay
end
2024-12-29 20:02:43 +00:00
2024-12-30 10:15:34 +00:00
return true
2024-12-29 20:02:43 +00:00
end
---@param _ any
---@param event string
2024-12-30 10:15:34 +00:00
---@param delay number | false | nil
---@param cb function | false
2024-12-29 20:02:43 +00:00
---@param ... any
---@return ...
local function triggerServerCallback(_, event, delay, cb, ...)
2024-12-30 10:15:34 +00:00
if not eventTimer(event, delay) then return end
2024-12-29 20:02:43 +00:00
2024-12-30 10:15:34 +00:00
local key
2024-12-29 20:02:43 +00:00
2024-12-30 10:15:34 +00:00
repeat
key = ('%s:%s'):format(event, math.random(0, 100000))
until not pendingCallbacks[key]
2024-12-29 20:02:43 +00:00
2024-12-30 10:15:34 +00:00
TriggerServerEvent(cbEvent:format(event), cache.resource, key, ...)
2024-12-29 20:02:43 +00:00
2024-12-30 10:15:34 +00:00
---@type promise | false
local promise = not cb and promise.new()
2024-12-29 20:02:43 +00:00
2024-12-30 10:15:34 +00:00
pendingCallbacks[key] = function(response, ...)
2024-12-29 20:02:43 +00:00
response = { response, ... }
2024-12-30 10:15:34 +00:00
if promise then
return promise:resolve(response)
end
2024-12-29 20:02:43 +00:00
if cb then
cb(table.unpack(response))
end
2024-12-30 10:15:34 +00:00
end
if promise then
SetTimeout(callbackTimeout, function() promise:reject(("callback event '%s' timed out"):format(key)) end)
2024-12-29 20:02:43 +00:00
2024-12-30 10:15:34 +00:00
return table.unpack(Citizen.Await(promise))
end
2024-12-29 20:02:43 +00:00
end
---@overload fun(event: string, delay: number | false, cb: function, ...)
lib.callback = setmetatable({}, {
2024-12-30 10:15:34 +00:00
__call = function(_, event, delay, cb, ...)
if not cb then
warn(("callback event '%s' does not have a function to callback to and will instead await\nuse lib.callback.await or a regular event to remove this warning")
:format(event))
else
local cbType = type(cb)
assert(cbType == 'function', ("expected argument 3 to have type 'function' (received %s)"):format(cbType))
end
return triggerServerCallback(_, event, delay, cb, ...)
end
2024-12-29 20:02:43 +00:00
})
---@param event string
---@param delay? number | false 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.
2024-12-30 10:15:34 +00:00
---@diagnostic disable-next-line: duplicate-set-field
2024-12-29 20:02:43 +00:00
function lib.callback.await(event, delay, ...)
2024-12-30 10:15:34 +00:00
return triggerServerCallback(nil, event, delay, false, ...)
2024-12-29 20:02:43 +00:00
end
local function callbackResponse(success, result, ...)
2024-12-30 10:15:34 +00:00
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
2024-12-29 20:02:43 +00:00
2024-12-30 10:15:34 +00:00
return false
end
2024-12-29 20:02:43 +00:00
2024-12-30 10:15:34 +00:00
return result, ...
2024-12-29 20:02:43 +00:00
end
local pcall = pcall
---@param name string
---@param cb function
2024-12-30 10:15:34 +00:00
---Registers an event handler and callback function to respond to server requests.
---@diagnostic disable-next-line: duplicate-set-field
2024-12-29 20:02:43 +00:00
function lib.callback.register(name, cb)
2024-12-30 10:15:34 +00:00
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
TriggerServerEvent(cbEvent:format(resource), key, callbackResponse(pcall(cb, ...)))
end)
2024-12-29 20:02:43 +00:00
end
return lib.callback