From 3e936d2c1c8f1e6fdb48e1388d3147f8671761b7 Mon Sep 17 00:00:00 2001 From: gononono64 <96976330+gononono64@users.noreply.github.com> Date: Mon, 31 Mar 2025 03:43:08 -0500 Subject: [PATCH] callbacks update Restructured callbacks to follow ox' return pattern --- lib/points/client/points.lua | 2 +- lib/utility/shared/callbacks.lua | 32 +++++++++++---------------- unit_tests/callbacks_test.lua | 38 ++++++++++++++------------------ 3 files changed, 31 insertions(+), 41 deletions(-) diff --git a/lib/points/client/points.lua b/lib/points/client/points.lua index 0d003bf..e5e23b5 100644 --- a/lib/points/client/points.lua +++ b/lib/points/client/points.lua @@ -245,7 +245,7 @@ end -- Usage example for checking nearby points -- -- -- ▀█▀ █▄█ █ ▄▀▀ █ ▄▀▀ █▀ █ █ ▄▀▀ █▄▀ █ █▄ █ ▄▀ █▄█ ▄▀▄ ▀█▀ █▀▄ ▄▀▄ █ █▄ █ ▀█▀ ▄▀▀ █▀ ▄▀▄ █▀▄ ▄▀▀ █▀▄ ▄▀▄ █ █ █ ██▀ ▀▄▀ --- █ █ █ █ ▄█▀ █ ▄█▀ █▀ ▀▄█ ▀▄▄ █ █ █ █ ▀█ ▀▄█ █ █ ▀▄▀ █ █▀ ▀▄▀ █ █ ▀█ █ ▄█▀ █▀ ▀▄▀ █▀▄ ▀▄▄ █▀▄ ▀▄▀ ▀▄▀▄▀ █▄▄ █▄▄ █ +-- █ █ █ █ ▄█▀ █ ▄█▀ █▀ ▀▄█ ▀▄▄ █ █ █ █ ▀█ ▀▄█ █ █ ▀▄▀ █ ██ █▀ ▀▄▀ █ █ ▀█ █ ▄█▀ █▀ ▀▄▀ █▀▄ ▀▄▄ █▀▄ ▀▄▀ ▀▄▀▄▀ █▄▄ █▄▄ █ --[[ Point.Register("point1", vector3(100, 100, 30), 5.0, function(point) diff --git a/lib/utility/shared/callbacks.lua b/lib/utility/shared/callbacks.lua index b4a5ce7..4834b62 100644 --- a/lib/utility/shared/callbacks.lua +++ b/lib/utility/shared/callbacks.lua @@ -42,12 +42,8 @@ RegisterNetEvent(clientToServerName, function(name, cbId, ...) local toAll = data.toAll and -1 or src -- Create response handler - local cb = function(...) - TriggerClientEvent(clientToServerBackToClientName, toAll, name, cbId, ...) - end - - -- Call the callback - func(src, cb, ...) + local packed = table.pack(func(src, ...)) + TriggerClientEvent(clientToServerBackToClientName, toAll, name, cbId, table.unpack(packed)) end) -- Server to client trigger function - supports both callback and direct return @@ -79,12 +75,15 @@ function Callback.Trigger(name, src, callbackOrArg, ...) end -- If no callback was provided, wait for the promise and return directly - if not hasCallback then - return Citizen.Await(p) + + local result = Citizen.Await(p) + -- If there's only one value, return it directly + if #result == 1 then + return result[1] + else + return table.unpack(result or {}) end - - -- Otherwise, return nothing (callback will handle it) - return nil + end -- Handle responses to server requests @@ -93,10 +92,10 @@ RegisterNetEvent(serverToClientBackToServerName, function(name, cbId, ...) if not data then return end -- Process callback if provided + if data.cb then - data.cb(...) + data.cb({...}) end - -- Always resolve the promise with all args for direct returns data.p:resolve({...}) cbData[cbId] = nil @@ -188,12 +187,7 @@ RegisterNetEvent(serverToClientName, function(name, cbId, ...) if not func then return end -- Create response handler - local cb = function(...) - TriggerServerEvent(serverToClientBackToServerName, name, cbId, ...) - end - - -- Execute the callback - func(cb, ...) + TriggerServerEvent(serverToClientBackToServerName, name, cbId, func(...)) end) exports('RegisterCallback', Callback.Register) diff --git a/unit_tests/callbacks_test.lua b/unit_tests/callbacks_test.lua index de88fa2..55247cf 100644 --- a/unit_tests/callbacks_test.lua +++ b/unit_tests/callbacks_test.lua @@ -6,20 +6,19 @@ -- ▄█▀ █▄▄ █▀▄ ▀▄▀ █▄▄ █▀▄ -- -- -- -- -- -- -- -- -- -- if not IsDuplicityVersion() then goto client end -exports.community_bridge:RegisterCallback('ClientToServerToClient', function(src, cb, ftpcsftug) - cb(ftpcsftug, 'some random string') + +exports.community_bridge:RegisterCallback('callback_test1_name_here', function(src, var1) + if var1 == "herpaderpa" then + print("this is a test") + end + return 'some random string', "some other string" end) -RegisterCommand('cb_server_test', function(source) - local someData = exports.community_bridge:TriggerCallback('ServerToClientToServer', source, function(ftpcsftug, derpa) - print(ftpcsftug, derpa) - return "This data gets passed up the chain" - end,"herpaderpa") - print("print", someData) +RegisterCommand('callback_test2', function(source) + local someData, other = exports.community_bridge:TriggerCallback('callback_test2_name_here', source, "herpaderpa") + print("Data From Client: ", someData, other) end) - - -- -- -- -- -- -- -- -- -- -- -- ▄▀▀ █ █ ██▀ █▄ █ ▀█▀ -- ▀▄▄ █▄▄ █ █▄▄ █ ▀█ █ @@ -27,18 +26,15 @@ end) if IsDuplicityVersion() then return end ::client:: - -RegisterCommand('cb_client_test', function() - local someData = exports.community_bridge:TriggerCallback('ClientToServerToClient', function(ftpcsftug, derpa) - print(ftpcsftug, derpa, "This will fire on the player who triggered the event") - end,"herpaderpa") - print(someData) +RegisterCommand('callback_test1', function() + local somerandomstring, someotherrandomstring = exports.community_bridge:TriggerCallback('callback_test1_name_here', "herpaderpa") + print("Data From Server: ", somerandomstring, someotherrandomstring) end) -exports.community_bridge:RegisterRebound('ClientToServerToClient', function(src, cb, ftpcsftug) - print("this is a rebound from another player or himself") -end) +-- exports.community_bridge:RegisterRebound('callback_test1', function(src, cb, ftpcsftug) +-- print("this is a rebound to all players") +-- end) -exports.community_bridge:RegisterCallback('ServerToClientToServer', function(cb, ftpcsftug) - cb(ftpcsftug, 'TestyMcTesterton') +exports.community_bridge:RegisterCallback('callback_test2_name_here', function(ftpcsftug) + return ftpcsftug, 'TestyMcTesterton' end)