callbacks update

Restructured callbacks to follow ox' return pattern
This commit is contained in:
gononono64 committed 2025-03-31 03:43:08 -05:00
1 parent 48476e319b
commit 3e936d2c1c
3 files changed
+31 -41

No files matched your search

+1 -1
View File
@@ -245,7 +245,7 @@ end
-- Usage example for checking nearby points
-- --
-- ▀█▀ █▄█ █ ▄▀▀ █ ▄▀▀ █▀ █ █ ▄▀▀ █▄▀ █ █▄ █ ▄▀ █▄█ ▄▀▄ ▀█▀ █▀▄ ▄▀▄ █ █▄ █ ▀█▀ ▄▀▀ █▀ ▄▀▄ █▀▄ ▄▀▀ █▀▄ ▄▀▄ █ █ █ ██▀ ▀▄▀
-- █ █ █ █ ▄█▀ █ ▄█▀ █▀ ▀▄█ ▀▄▄ █ █ █ █ ▀█ ▀▄█ █ █ ▀▄▀ █ █▀ ▀▄▀ █ █ ▀█ █ ▄█▀ █▀ ▀▄▀ █▀▄ ▀▄▄ █▀▄ ▀▄▀ ▀▄▀▄▀ █▄▄ █▄▄ █
-- █ █ █ █ ▄█▀ █ ▄█▀ █▀ ▀▄█ ▀▄▄ █ █ █ █ ▀█ ▀▄█ █ █ ▀▄▀ █ ██ █▀ ▀▄▀ █ █ ▀█ █ ▄█▀ █▀ ▀▄▀ █▀▄ ▀▄▄ █▀▄ ▀▄▀ ▀▄▀▄▀ █▄▄ █▄▄ █
--[[
Point.Register("point1", vector3(100, 100, 30), 5.0,
function(point)
+13 -19
View File
@@ -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)
+17 -21
View File
@@ -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)