Skip to content

Commit f0009f0

Browse files
committed
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).
1 parent a40be54 commit f0009f0

File tree

2 files changed

+54
-37
lines changed

2 files changed

+54
-37
lines changed

imports/callback/client.lua

+13-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ RegisterNetEvent(cbEvent:format(cache.resource), function(key, ...)
1111
end)
1212

1313
---@param event string
14-
---@param delay number | false prevent the event from being called for the given time
14+
---@param delay? number | false prevent the event from being called for the given time
1515
local function eventTimer(event, delay)
1616
if delay and type(delay) == 'number' and delay > 0 then
1717
local time = GetGameTimer()
@@ -28,8 +28,8 @@ end
2828

2929
---@param _ any
3030
---@param event string
31-
---@param delay number | false
32-
---@param cb function|false
31+
---@param delay number | false | nil
32+
---@param cb function | false
3333
---@param ... any
3434
---@return ...
3535
local function triggerServerCallback(_, event, delay, cb, ...)
@@ -67,12 +67,19 @@ end
6767

6868
---@overload fun(event: string, delay: number | false, cb: function, ...)
6969
lib.callback = setmetatable({}, {
70-
__call = triggerServerCallback
70+
__call = function(_, event, delay, cb, ...)
71+
local cbType = type(cb)
72+
73+
assert(cbType == 'function', ("expected argument 3 to have type 'function' (received %s)"):format(cbType))
74+
75+
return triggerServerCallback(_, event, delay, cb, ...)
76+
end
7177
})
7278

7379
---@param event string
7480
---@param delay? number | false prevent the event from being called for the given time.
7581
---Sends an event to the server and halts the current thread until a response is returned.
82+
---@diagnostic disable-next-line: duplicate-set-field
7683
function lib.callback.await(event, delay, ...)
7784
return triggerServerCallback(nil, event, delay, false, ...)
7885
end
@@ -94,7 +101,8 @@ local pcall = pcall
94101

95102
---@param name string
96103
---@param cb function
97-
--- Registers an event handler and callback function to respond to server requests.
104+
---Registers an event handler and callback function to respond to server requests.
105+
---@diagnostic disable-next-line: duplicate-set-field
98106
function lib.callback.register(name, cb)
99107
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
100108
TriggerServerEvent(cbEvent:format(resource), key, callbackResponse(pcall(cb, ...)))

imports/callback/server.lua

+41-32
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ local cbEvent = '__ox_cb_%s'
33
local callbackTimeout = GetConvarInt('ox:callbackTimeout', 300000)
44

55
RegisterNetEvent(cbEvent:format(cache.resource), function(key, ...)
6-
local cb = pendingCallbacks[key]
6+
local cb = pendingCallbacks[key]
77
pendingCallbacks[key] = nil
88

9-
return cb and cb(...)
9+
return cb and cb(...)
1010
end)
1111

1212
---@param _ any
@@ -16,71 +16,80 @@ end)
1616
---@param ... any
1717
---@return ...
1818
local function triggerClientCallback(_, event, playerId, cb, ...)
19-
local key
19+
assert(DoesPlayerExist(playerId --[[@as string]]), ("target playerId '%s' does not exist"):format(playerId))
2020

21-
repeat
22-
key = ('%s:%s:%s'):format(event, math.random(0, 100000), playerId)
23-
until not pendingCallbacks[key]
21+
local key
2422

25-
TriggerClientEvent(cbEvent:format(event), playerId, cache.resource, key, ...)
23+
repeat
24+
key = ('%s:%s:%s'):format(event, math.random(0, 100000), playerId)
25+
until not pendingCallbacks[key]
2626

27-
---@type promise | false
28-
local promise = not cb and promise.new()
27+
TriggerClientEvent(cbEvent:format(event), playerId, cache.resource, key, ...)
2928

30-
pendingCallbacks[key] = function(response, ...)
29+
---@type promise | false
30+
local promise = not cb and promise.new()
31+
32+
pendingCallbacks[key] = function(response, ...)
3133
response = { response, ... }
3234

33-
if promise then
34-
return promise:resolve(response)
35-
end
35+
if promise then
36+
return promise:resolve(response)
37+
end
3638

3739
if cb then
3840
cb(table.unpack(response))
3941
end
40-
end
42+
end
4143

42-
if promise then
44+
if promise then
4345
SetTimeout(callbackTimeout, function() promise:reject(("callback event '%s' timed out"):format(key)) end)
4446

45-
return table.unpack(Citizen.Await(promise))
46-
end
47+
return table.unpack(Citizen.Await(promise))
48+
end
4749
end
4850

4951
---@overload fun(event: string, playerId: number, cb: function, ...)
5052
lib.callback = setmetatable({}, {
51-
__call = triggerClientCallback
53+
__call = function(_, event, playerId, cb, ...)
54+
local cbType = type(cb)
55+
56+
assert(cbType == 'function', ("expected argument 3 to have type 'function' (received %s)"):format(cbType))
57+
58+
return triggerClientCallback(_, event, playerId, cb, ...)
59+
end
5260
})
5361

5462
---@param event string
5563
---@param playerId number
5664
--- Sends an event to a client and halts the current thread until a response is returned.
65+
---@diagnostic disable-next-line: duplicate-set-field
5766
function lib.callback.await(event, playerId, ...)
58-
return triggerClientCallback(nil, event, playerId, false, ...)
67+
return triggerClientCallback(nil, event, playerId, false, ...)
5968
end
6069

6170
local function callbackResponse(success, result, ...)
62-
if not success then
63-
if result then
64-
return print(('^1SCRIPT ERROR: %s^0\n%s'):format(result , Citizen.InvokeNative(`FORMAT_STACK_TRACE` & 0xFFFFFFFF, nil, 0, Citizen.ResultAsString()) or ''))
65-
end
71+
if not success then
72+
if result then
73+
return print(('^1SCRIPT ERROR: %s^0\n%s'):format(result,
74+
Citizen.InvokeNative(`FORMAT_STACK_TRACE` & 0xFFFFFFFF, nil, 0, Citizen.ResultAsString()) or ''))
75+
end
6676

67-
return false
68-
end
77+
return false
78+
end
6979

70-
return result, ...
80+
return result, ...
7181
end
7282

7383
local pcall = pcall
7484

7585
---@param name string
7686
---@param cb function
77-
--- Registers an event handler and callback function to respond to client requests.
87+
---Registers an event handler and callback function to respond to client requests.
88+
---@diagnostic disable-next-line: duplicate-set-field
7889
function lib.callback.register(name, cb)
79-
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
80-
TriggerClientEvent(cbEvent:format(resource), source, key, callbackResponse(pcall(cb, source, ...)))
81-
end)
90+
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
91+
TriggerClientEvent(cbEvent:format(resource), source, key, callbackResponse(pcall(cb, source, ...)))
92+
end)
8293
end
8394

8495
return lib.callback
85-
86-

0 commit comments

Comments
 (0)