mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-18 15:36:02 +01:00
refactor(callback): assertions
Both client and server callbacks will check if "cb" is a function, to reduce the number of people using callbacks as a replacement for events. Server->client callbacks will now check if the target playerId exists, as people apparently use it for unintended values (i.e. -1).
This commit is contained in:
@@ -11,7 +11,7 @@ RegisterNetEvent(cbEvent:format(cache.resource), function(key, ...)
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
---@param event string
|
---@param event string
|
||||||
---@param delay number | false prevent the event from being called for the given time
|
---@param delay? number | false prevent the event from being called for the given time
|
||||||
local function eventTimer(event, delay)
|
local function eventTimer(event, delay)
|
||||||
if delay and type(delay) == 'number' and delay > 0 then
|
if delay and type(delay) == 'number' and delay > 0 then
|
||||||
local time = GetGameTimer()
|
local time = GetGameTimer()
|
||||||
@@ -28,8 +28,8 @@ end
|
|||||||
|
|
||||||
---@param _ any
|
---@param _ any
|
||||||
---@param event string
|
---@param event string
|
||||||
---@param delay number | false
|
---@param delay number | false | nil
|
||||||
---@param cb function|false
|
---@param cb function | false
|
||||||
---@param ... any
|
---@param ... any
|
||||||
---@return ...
|
---@return ...
|
||||||
local function triggerServerCallback(_, event, delay, cb, ...)
|
local function triggerServerCallback(_, event, delay, cb, ...)
|
||||||
@@ -67,12 +67,19 @@ end
|
|||||||
|
|
||||||
---@overload fun(event: string, delay: number | false, cb: function, ...)
|
---@overload fun(event: string, delay: number | false, cb: function, ...)
|
||||||
lib.callback = setmetatable({}, {
|
lib.callback = setmetatable({}, {
|
||||||
__call = triggerServerCallback
|
__call = function(_, event, delay, cb, ...)
|
||||||
|
local cbType = type(cb)
|
||||||
|
|
||||||
|
assert(cbType == 'function', ("expected argument 3 to have type 'function' (received %s)"):format(cbType))
|
||||||
|
|
||||||
|
return triggerServerCallback(_, event, delay, cb, ...)
|
||||||
|
end
|
||||||
})
|
})
|
||||||
|
|
||||||
---@param event string
|
---@param event string
|
||||||
---@param delay? number | false prevent the event from being called for the given time.
|
---@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.
|
---Sends an event to the server and halts the current thread until a response is returned.
|
||||||
|
---@diagnostic disable-next-line: duplicate-set-field
|
||||||
function lib.callback.await(event, delay, ...)
|
function lib.callback.await(event, delay, ...)
|
||||||
return triggerServerCallback(nil, event, delay, false, ...)
|
return triggerServerCallback(nil, event, delay, false, ...)
|
||||||
end
|
end
|
||||||
@@ -94,7 +101,8 @@ local pcall = pcall
|
|||||||
|
|
||||||
---@param name string
|
---@param name string
|
||||||
---@param cb function
|
---@param cb function
|
||||||
--- Registers an event handler and callback function to respond to server requests.
|
---Registers an event handler and callback function to respond to server requests.
|
||||||
|
---@diagnostic disable-next-line: duplicate-set-field
|
||||||
function lib.callback.register(name, cb)
|
function lib.callback.register(name, cb)
|
||||||
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
|
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
|
||||||
TriggerServerEvent(cbEvent:format(resource), key, callbackResponse(pcall(cb, ...)))
|
TriggerServerEvent(cbEvent:format(resource), key, callbackResponse(pcall(cb, ...)))
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ local cbEvent = '__ox_cb_%s'
|
|||||||
local callbackTimeout = GetConvarInt('ox:callbackTimeout', 300000)
|
local callbackTimeout = GetConvarInt('ox:callbackTimeout', 300000)
|
||||||
|
|
||||||
RegisterNetEvent(cbEvent:format(cache.resource), function(key, ...)
|
RegisterNetEvent(cbEvent:format(cache.resource), function(key, ...)
|
||||||
local cb = pendingCallbacks[key]
|
local cb = pendingCallbacks[key]
|
||||||
pendingCallbacks[key] = nil
|
pendingCallbacks[key] = nil
|
||||||
|
|
||||||
return cb and cb(...)
|
return cb and cb(...)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
---@param _ any
|
---@param _ any
|
||||||
@@ -16,71 +16,80 @@ end)
|
|||||||
---@param ... any
|
---@param ... any
|
||||||
---@return ...
|
---@return ...
|
||||||
local function triggerClientCallback(_, event, playerId, cb, ...)
|
local function triggerClientCallback(_, event, playerId, cb, ...)
|
||||||
local key
|
assert(DoesPlayerExist(playerId --[[@as string]]), ("target playerId '%s' does not exist"):format(playerId))
|
||||||
|
|
||||||
repeat
|
local key
|
||||||
key = ('%s:%s:%s'):format(event, math.random(0, 100000), playerId)
|
|
||||||
until not pendingCallbacks[key]
|
|
||||||
|
|
||||||
TriggerClientEvent(cbEvent:format(event), playerId, cache.resource, key, ...)
|
repeat
|
||||||
|
key = ('%s:%s:%s'):format(event, math.random(0, 100000), playerId)
|
||||||
|
until not pendingCallbacks[key]
|
||||||
|
|
||||||
---@type promise | false
|
TriggerClientEvent(cbEvent:format(event), playerId, cache.resource, key, ...)
|
||||||
local promise = not cb and promise.new()
|
|
||||||
|
|
||||||
pendingCallbacks[key] = function(response, ...)
|
---@type promise | false
|
||||||
|
local promise = not cb and promise.new()
|
||||||
|
|
||||||
|
pendingCallbacks[key] = function(response, ...)
|
||||||
response = { response, ... }
|
response = { response, ... }
|
||||||
|
|
||||||
if promise then
|
if promise then
|
||||||
return promise:resolve(response)
|
return promise:resolve(response)
|
||||||
end
|
end
|
||||||
|
|
||||||
if cb then
|
if cb then
|
||||||
cb(table.unpack(response))
|
cb(table.unpack(response))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if promise then
|
if promise then
|
||||||
SetTimeout(callbackTimeout, function() promise:reject(("callback event '%s' timed out"):format(key)) end)
|
SetTimeout(callbackTimeout, function() promise:reject(("callback event '%s' timed out"):format(key)) end)
|
||||||
|
|
||||||
return table.unpack(Citizen.Await(promise))
|
return table.unpack(Citizen.Await(promise))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@overload fun(event: string, playerId: number, cb: function, ...)
|
---@overload fun(event: string, playerId: number, cb: function, ...)
|
||||||
lib.callback = setmetatable({}, {
|
lib.callback = setmetatable({}, {
|
||||||
__call = triggerClientCallback
|
__call = function(_, event, playerId, cb, ...)
|
||||||
|
local cbType = type(cb)
|
||||||
|
|
||||||
|
assert(cbType == 'function', ("expected argument 3 to have type 'function' (received %s)"):format(cbType))
|
||||||
|
|
||||||
|
return triggerClientCallback(_, event, playerId, cb, ...)
|
||||||
|
end
|
||||||
})
|
})
|
||||||
|
|
||||||
---@param event string
|
---@param event string
|
||||||
---@param playerId number
|
---@param playerId number
|
||||||
--- Sends an event to a client and halts the current thread until a response is returned.
|
--- Sends an event to a client and halts the current thread until a response is returned.
|
||||||
|
---@diagnostic disable-next-line: duplicate-set-field
|
||||||
function lib.callback.await(event, playerId, ...)
|
function lib.callback.await(event, playerId, ...)
|
||||||
return triggerClientCallback(nil, event, playerId, false, ...)
|
return triggerClientCallback(nil, event, playerId, false, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function callbackResponse(success, result, ...)
|
local function callbackResponse(success, result, ...)
|
||||||
if not success then
|
if not success then
|
||||||
if result 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 ''))
|
return print(('^1SCRIPT ERROR: %s^0\n%s'):format(result,
|
||||||
end
|
Citizen.InvokeNative(`FORMAT_STACK_TRACE` & 0xFFFFFFFF, nil, 0, Citizen.ResultAsString()) or ''))
|
||||||
|
end
|
||||||
|
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
return result, ...
|
return result, ...
|
||||||
end
|
end
|
||||||
|
|
||||||
local pcall = pcall
|
local pcall = pcall
|
||||||
|
|
||||||
---@param name string
|
---@param name string
|
||||||
---@param cb function
|
---@param cb function
|
||||||
--- Registers an event handler and callback function to respond to client requests.
|
---Registers an event handler and callback function to respond to client requests.
|
||||||
|
---@diagnostic disable-next-line: duplicate-set-field
|
||||||
function lib.callback.register(name, cb)
|
function lib.callback.register(name, cb)
|
||||||
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
|
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
|
||||||
TriggerClientEvent(cbEvent:format(resource), source, key, callbackResponse(pcall(cb, source, ...)))
|
TriggerClientEvent(cbEvent:format(resource), source, key, callbackResponse(pcall(cb, source, ...)))
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
return lib.callback
|
return lib.callback
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user