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:
Linden
2024-05-22 16:36:22 +10:00
parent a40be54a9e
commit f0009f0bc8
2 changed files with 54 additions and 37 deletions

View File

@@ -11,7 +11,7 @@ RegisterNetEvent(cbEvent:format(cache.resource), function(key, ...)
end)
---@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)
if delay and type(delay) == 'number' and delay > 0 then
local time = GetGameTimer()
@@ -28,7 +28,7 @@ end
---@param _ any
---@param event string
---@param delay number | false
---@param delay number | false | nil
---@param cb function | false
---@param ... any
---@return ...
@@ -67,12 +67,19 @@ end
---@overload fun(event: string, delay: number | false, cb: function, ...)
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 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.
---@diagnostic disable-next-line: duplicate-set-field
function lib.callback.await(event, delay, ...)
return triggerServerCallback(nil, event, delay, false, ...)
end
@@ -95,6 +102,7 @@ local pcall = pcall
---@param name string
---@param cb function
---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)
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
TriggerServerEvent(cbEvent:format(resource), key, callbackResponse(pcall(cb, ...)))

View File

@@ -16,6 +16,8 @@ end)
---@param ... any
---@return ...
local function triggerClientCallback(_, event, playerId, cb, ...)
assert(DoesPlayerExist(playerId --[[@as string]]), ("target playerId '%s' does not exist"):format(playerId))
local key
repeat
@@ -48,12 +50,19 @@ end
---@overload fun(event: string, playerId: number, cb: function, ...)
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 playerId number
--- 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, ...)
return triggerClientCallback(nil, event, playerId, false, ...)
end
@@ -61,7 +70,8 @@ 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 ''))
return print(('^1SCRIPT ERROR: %s^0\n%s'):format(result,
Citizen.InvokeNative(`FORMAT_STACK_TRACE` & 0xFFFFFFFF, nil, 0, Citizen.ResultAsString()) or ''))
end
return false
@@ -75,6 +85,7 @@ local pcall = pcall
---@param name string
---@param cb function
---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)
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
TriggerClientEvent(cbEvent:format(resource), source, key, callbackResponse(pcall(cb, source, ...)))
@@ -82,5 +93,3 @@ function lib.callback.register(name, cb)
end
return lib.callback