callbacks update
Restructured callbacks to follow ox' return pattern
This commit is contained in:
1 parent
48476e319b
commit
3e936d2c1c
3 files changed
+31
-41
No files matched your search
@@ -245,7 +245,7 @@ end
|
|||||||
-- Usage example for checking nearby points
|
-- Usage example for checking nearby points
|
||||||
-- --
|
-- --
|
||||||
-- ▀█▀ █▄█ █ ▄▀▀ █ ▄▀▀ █▀ █ █ ▄▀▀ █▄▀ █ █▄ █ ▄▀ █▄█ ▄▀▄ ▀█▀ █▀▄ ▄▀▄ █ █▄ █ ▀█▀ ▄▀▀ █▀ ▄▀▄ █▀▄ ▄▀▀ █▀▄ ▄▀▄ █ █ █ ██▀ ▀▄▀
|
-- ▀█▀ █▄█ █ ▄▀▀ █ ▄▀▀ █▀ █ █ ▄▀▀ █▄▀ █ █▄ █ ▄▀ █▄█ ▄▀▄ ▀█▀ █▀▄ ▄▀▄ █ █▄ █ ▀█▀ ▄▀▀ █▀ ▄▀▄ █▀▄ ▄▀▀ █▀▄ ▄▀▄ █ █ █ ██▀ ▀▄▀
|
||||||
-- █ █ █ █ ▄█▀ █ ▄█▀ █▀ ▀▄█ ▀▄▄ █ █ █ █ ▀█ ▀▄█ █ █ ▀▄▀ █ █▀ ▀▄▀ █ █ ▀█ █ ▄█▀ █▀ ▀▄▀ █▀▄ ▀▄▄ █▀▄ ▀▄▀ ▀▄▀▄▀ █▄▄ █▄▄ █
|
-- █ █ █ █ ▄█▀ █ ▄█▀ █▀ ▀▄█ ▀▄▄ █ █ █ █ ▀█ ▀▄█ █ █ ▀▄▀ █ ██ █▀ ▀▄▀ █ █ ▀█ █ ▄█▀ █▀ ▀▄▀ █▀▄ ▀▄▄ █▀▄ ▀▄▀ ▀▄▀▄▀ █▄▄ █▄▄ █
|
||||||
--[[
|
--[[
|
||||||
Point.Register("point1", vector3(100, 100, 30), 5.0,
|
Point.Register("point1", vector3(100, 100, 30), 5.0,
|
||||||
function(point)
|
function(point)
|
||||||
|
|||||||
@@ -42,12 +42,8 @@ RegisterNetEvent(clientToServerName, function(name, cbId, ...)
|
|||||||
local toAll = data.toAll and -1 or src
|
local toAll = data.toAll and -1 or src
|
||||||
|
|
||||||
-- Create response handler
|
-- Create response handler
|
||||||
local cb = function(...)
|
local packed = table.pack(func(src, ...))
|
||||||
TriggerClientEvent(clientToServerBackToClientName, toAll, name, cbId, ...)
|
TriggerClientEvent(clientToServerBackToClientName, toAll, name, cbId, table.unpack(packed))
|
||||||
end
|
|
||||||
|
|
||||||
-- Call the callback
|
|
||||||
func(src, cb, ...)
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- Server to client trigger function - supports both callback and direct return
|
-- Server to client trigger function - supports both callback and direct return
|
||||||
@@ -79,12 +75,15 @@ function Callback.Trigger(name, src, callbackOrArg, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- If no callback was provided, wait for the promise and return directly
|
-- 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
|
end
|
||||||
|
|
||||||
-- Otherwise, return nothing (callback will handle it)
|
|
||||||
return nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Handle responses to server requests
|
-- Handle responses to server requests
|
||||||
@@ -93,10 +92,10 @@ RegisterNetEvent(serverToClientBackToServerName, function(name, cbId, ...)
|
|||||||
if not data then return end
|
if not data then return end
|
||||||
|
|
||||||
-- Process callback if provided
|
-- Process callback if provided
|
||||||
if data.cb then
|
|
||||||
data.cb(...)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
if data.cb then
|
||||||
|
data.cb({...})
|
||||||
|
end
|
||||||
-- Always resolve the promise with all args for direct returns
|
-- Always resolve the promise with all args for direct returns
|
||||||
data.p:resolve({...})
|
data.p:resolve({...})
|
||||||
cbData[cbId] = nil
|
cbData[cbId] = nil
|
||||||
@@ -188,12 +187,7 @@ RegisterNetEvent(serverToClientName, function(name, cbId, ...)
|
|||||||
if not func then return end
|
if not func then return end
|
||||||
|
|
||||||
-- Create response handler
|
-- Create response handler
|
||||||
local cb = function(...)
|
TriggerServerEvent(serverToClientBackToServerName, name, cbId, func(...))
|
||||||
TriggerServerEvent(serverToClientBackToServerName, name, cbId, ...)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Execute the callback
|
|
||||||
func(cb, ...)
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
exports('RegisterCallback', Callback.Register)
|
exports('RegisterCallback', Callback.Register)
|
||||||
|
|||||||
@@ -6,20 +6,19 @@
|
|||||||
-- ▄█▀ █▄▄ █▀▄ ▀▄▀ █▄▄ █▀▄
|
-- ▄█▀ █▄▄ █▀▄ ▀▄▀ █▄▄ █▀▄
|
||||||
-- -- -- -- -- -- -- -- -- --
|
-- -- -- -- -- -- -- -- -- --
|
||||||
if not IsDuplicityVersion() then goto client end
|
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)
|
end)
|
||||||
|
|
||||||
RegisterCommand('cb_server_test', function(source)
|
RegisterCommand('callback_test2', function(source)
|
||||||
local someData = exports.community_bridge:TriggerCallback('ServerToClientToServer', source, function(ftpcsftug, derpa)
|
local someData, other = exports.community_bridge:TriggerCallback('callback_test2_name_here', source, "herpaderpa")
|
||||||
print(ftpcsftug, derpa)
|
print("Data From Client: ", someData, other)
|
||||||
return "This data gets passed up the chain"
|
|
||||||
end,"herpaderpa")
|
|
||||||
print("print", someData)
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- -- -- -- -- -- -- -- -- --
|
-- -- -- -- -- -- -- -- -- --
|
||||||
-- ▄▀▀ █ █ ██▀ █▄ █ ▀█▀
|
-- ▄▀▀ █ █ ██▀ █▄ █ ▀█▀
|
||||||
-- ▀▄▄ █▄▄ █ █▄▄ █ ▀█ █
|
-- ▀▄▄ █▄▄ █ █▄▄ █ ▀█ █
|
||||||
@@ -27,18 +26,15 @@ end)
|
|||||||
if IsDuplicityVersion() then return end
|
if IsDuplicityVersion() then return end
|
||||||
::client::
|
::client::
|
||||||
|
|
||||||
|
RegisterCommand('callback_test1', function()
|
||||||
RegisterCommand('cb_client_test', function()
|
local somerandomstring, someotherrandomstring = exports.community_bridge:TriggerCallback('callback_test1_name_here', "herpaderpa")
|
||||||
local someData = exports.community_bridge:TriggerCallback('ClientToServerToClient', function(ftpcsftug, derpa)
|
print("Data From Server: ", somerandomstring, someotherrandomstring)
|
||||||
print(ftpcsftug, derpa, "This will fire on the player who triggered the event")
|
|
||||||
end,"herpaderpa")
|
|
||||||
print(someData)
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
exports.community_bridge:RegisterRebound('ClientToServerToClient', function(src, cb, ftpcsftug)
|
-- exports.community_bridge:RegisterRebound('callback_test1', function(src, cb, ftpcsftug)
|
||||||
print("this is a rebound from another player or himself")
|
-- print("this is a rebound to all players")
|
||||||
end)
|
-- end)
|
||||||
|
|
||||||
exports.community_bridge:RegisterCallback('ServerToClientToServer', function(cb, ftpcsftug)
|
exports.community_bridge:RegisterCallback('callback_test2_name_here', function(ftpcsftug)
|
||||||
cb(ftpcsftug, 'TestyMcTesterton')
|
return ftpcsftug, 'TestyMcTesterton'
|
||||||
end)
|
end)
|
||||||
Reference in new issue
Block a user