Files
ox_lib/imports/callback/server.lua

77 lines
1.9 KiB
Lua
Raw Normal View History

local events = {}
local cbEvent = ('__ox_cb_%s')
RegisterNetEvent(cbEvent:format(cache.resource), function(key, ...)
local cb = events[key]
return cb and cb(...)
end)
---@param _ any
---@param event string
---@param playerId number
2022-08-08 17:16:57 +10:00
---@param cb function|false
---@param ... any
---@return unknown?
local function triggerClientCallback(_, event, playerId, cb, ...)
local key
repeat
key = ('%s:%s:%s'):format(event, math.random(0, 100000), playerId)
until not events[key]
TriggerClientEvent(cbEvent:format(event), playerId, cache.resource, key, ...)
2022-08-08 17:16:57 +10:00
---@type promise | false
local promise = not cb and promise.new()
events[key] = function(response)
events[key] = nil
if promise then
return promise:resolve(response or {})
end
return cb and cb(table.unpack(response or {}))
end
if promise then
return table.unpack(Citizen.Await(promise))
end
end
---@overload fun(event: string, playerId: number, cb: function, ...)
local callback = setmetatable({}, {
__call = triggerClientCallback
})
---@param event string
---@param playerId number
--- Sends an event to a client and halts the current thread until a response is returned.
function callback.await(event, playerId, ...)
return triggerClientCallback(_, event, playerId, false, ...)
end
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
return { result, ... }
end
local pcall = pcall
---@param name string
---@param cb function
--- Registers an event handler and callback function to respond to client requests.
function callback.register(name, cb)
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
TriggerClientEvent(cbEvent:format(resource), source, key, callbackResponse(pcall(cb, source, ...)))
end)
end
return callback