@@ -3,10 +3,10 @@ local cbEvent = '__ox_cb_%s'
3
3
local callbackTimeout = GetConvarInt (' ox:callbackTimeout' , 300000 )
4
4
5
5
RegisterNetEvent (cbEvent :format (cache .resource ), function (key , ...)
6
- local cb = pendingCallbacks [key ]
6
+ local cb = pendingCallbacks [key ]
7
7
pendingCallbacks [key ] = nil
8
8
9
- return cb and cb (... )
9
+ return cb and cb (... )
10
10
end )
11
11
12
12
--- @param _ any
16
16
--- @param ... any
17
17
--- @return ...
18
18
local function triggerClientCallback (_ , event , playerId , cb , ...)
19
- local key
19
+ assert ( DoesPlayerExist ( playerId --[[ @as string ]] ), ( " target playerId '%s' does not exist " ): format ( playerId ))
20
20
21
- repeat
22
- key = (' %s:%s:%s' ):format (event , math.random (0 , 100000 ), playerId )
23
- until not pendingCallbacks [key ]
21
+ local key
24
22
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 ]
26
26
27
- --- @type promise | false
28
- local promise = not cb and promise .new ()
27
+ TriggerClientEvent (cbEvent :format (event ), playerId , cache .resource , key , ... )
29
28
30
- pendingCallbacks [key ] = function (response , ...)
29
+ --- @type promise | false
30
+ local promise = not cb and promise .new ()
31
+
32
+ pendingCallbacks [key ] = function (response , ...)
31
33
response = { response , ... }
32
34
33
- if promise then
34
- return promise :resolve (response )
35
- end
35
+ if promise then
36
+ return promise :resolve (response )
37
+ end
36
38
37
39
if cb then
38
40
cb (table.unpack (response ))
39
41
end
40
- end
42
+ end
41
43
42
- if promise then
44
+ if promise then
43
45
SetTimeout (callbackTimeout , function () promise :reject ((" callback event '%s' timed out" ):format (key )) end )
44
46
45
- return table.unpack (Citizen .Await (promise ))
46
- end
47
+ return table.unpack (Citizen .Await (promise ))
48
+ end
47
49
end
48
50
49
51
--- @overload fun ( event : string , playerId : number , cb : function , ... )
50
52
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
52
60
})
53
61
54
62
--- @param event string
55
63
--- @param playerId number
56
64
--- Sends an event to a client and halts the current thread until a response is returned.
65
+ --- @diagnostic disable-next-line : duplicate-set-field
57
66
function lib .callback .await (event , playerId , ...)
58
- return triggerClientCallback (nil , event , playerId , false , ... )
67
+ return triggerClientCallback (nil , event , playerId , false , ... )
59
68
end
60
69
61
70
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
66
76
67
- return false
68
- end
77
+ return false
78
+ end
69
79
70
- return result , ...
80
+ return result , ...
71
81
end
72
82
73
83
local pcall = pcall
74
84
75
85
--- @param name string
76
86
--- @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
78
89
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 )
82
93
end
83
94
84
95
return lib .callback
85
-
86
-
0 commit comments