Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use promises in callbacks + cleanup #1160

Merged
merged 5 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions client/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,22 @@ end)

-- Client Callback
RegisterNetEvent('QBCore:Client:TriggerClientCallback', function(name, ...)
QBCore.Functions.TriggerClientCallback(name, function(...)
if not QBCore.ClientCallbacks[name] then return end

QBCore.ClientCallbacks[name](function(...)
TriggerServerEvent('QBCore:Server:TriggerClientCallback', name, ...)
end, ...)
end)

-- Server Callback
RegisterNetEvent('QBCore:Client:TriggerCallback', function(name, ...)
if QBCore.ServerCallbacks[name] then
QBCore.ServerCallbacks[name](...)
QBCore.ServerCallbacks[name].promise:resolve(...)

if QBCore.ServerCallbacks[name].callback then
QBCore.ServerCallbacks[name].callback(...)
end

QBCore.ServerCallbacks[name] = nil
end
end)
Expand Down
26 changes: 19 additions & 7 deletions client/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,26 @@ function QBCore.Functions.CreateClientCallback(name, cb)
QBCore.ClientCallbacks[name] = cb
end

function QBCore.Functions.TriggerClientCallback(name, cb, ...)
if not QBCore.ClientCallbacks[name] then return end
QBCore.ClientCallbacks[name](cb, ...)
end
function QBCore.Functions.TriggerCallback(name, ...)
local cb = nil
local args = { ... }

if QBCore.Shared.IsFunction(args[1]) then
cb = args[1]
table.remove(args, 1)
end

function QBCore.Functions.TriggerCallback(name, cb, ...)
QBCore.ServerCallbacks[name] = cb
TriggerServerEvent('QBCore:Server:TriggerCallback', name, ...)
QBCore.ServerCallbacks[name] = {
callback = cb,
promise = promise.new()
}

TriggerServerEvent('QBCore:Server:TriggerCallback', name, table.unpack(args))

if cb == nil then
Citizen.Await(QBCore.ServerCallbacks[name].promise)
return QBCore.ServerCallbacks[name].promise.value
end
end

function QBCore.Debug(resource, obj, depth)
Expand Down
14 changes: 11 additions & 3 deletions server/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ local databaseConnected, bansTableExists = readyFunction == nil, readyFunction =
if readyFunction ~= nil then
MySQL.ready(function()
databaseConnected = true

local DatabaseInfo = QBCore.Functions.GetDatabaseInfo()
if not DatabaseInfo or not DatabaseInfo.exists then return end

Expand Down Expand Up @@ -125,15 +125,23 @@ end)
-- Client Callback
RegisterNetEvent('QBCore:Server:TriggerClientCallback', function(name, ...)
if QBCore.ClientCallbacks[name] then
QBCore.ClientCallbacks[name](...)
QBCore.ClientCallbacks[name].promise:resolve(...)

if QBCore.ClientCallbacks[name].callback then
QBCore.ClientCallbacks[name].callback(...)
end

QBCore.ClientCallbacks[name] = nil
end
end)

-- Server Callback
RegisterNetEvent('QBCore:Server:TriggerCallback', function(name, ...)
if not QBCore.ServerCallbacks[name] then return end

local src = source
QBCore.Functions.TriggerCallback(name, src, function(...)

QBCore.ServerCallbacks[name](src, function(...)
TriggerClientEvent('QBCore:Client:TriggerCallback', src, name, ...)
end, ...)
end)
Expand Down
35 changes: 21 additions & 14 deletions server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,26 @@ end
---@param source any
---@param cb function
---@param ... any
function QBCore.Functions.TriggerClientCallback(name, source, cb, ...)
QBCore.ClientCallbacks[name] = cb
TriggerClientEvent('QBCore:Client:TriggerClientCallback', source, name, ...)
function QBCore.Functions.TriggerClientCallback(name, source, ...)
local cb = nil
local args = { ... }

if QBCore.Shared.IsFunction(args[1]) then
cb = args[1]
table.remove(args, 1)
end

QBCore.ClientCallbacks[name] = {
callback = cb,
promise = promise.new()
}

TriggerClientEvent('QBCore:Client:TriggerClientCallback', source, name, table.unpack(args))

if cb == nil then
Citizen.Await(QBCore.ClientCallbacks[name].promise)
return QBCore.ClientCallbacks[name].promise.value
end
end

---Create Server Callback
Expand All @@ -446,16 +463,6 @@ function QBCore.Functions.CreateCallback(name, cb)
QBCore.ServerCallbacks[name] = cb
end

---Trigger Serv er Callback
---@param name string
---@param source any
---@param cb function
---@param ... any
function QBCore.Functions.TriggerCallback(name, source, cb, ...)
if not QBCore.ServerCallbacks[name] then return end
QBCore.ServerCallbacks[name](source, cb, ...)
end

-- Items

---Create a usable item
Expand All @@ -475,7 +482,7 @@ function QBCore.Functions.CreateUseableItem(item, data)
elseif type(data) == "function" then
rawFunc = data
end

if rawFunc then
QBCore.UsableItems[item] = {
func = rawFunc,
Expand Down
8 changes: 8 additions & 0 deletions shared/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ function QBShared.ChangeVehicleExtra(vehicle, extra, enable)
end
end

function QBShared.IsFunction(value)
if type(value) == 'table' then
return value.__cfx_functionReference ~= nil and type(value.__cfx_functionReference) == "string"
end

return type(value) == 'function'
end

function QBShared.SetDefaultVehicleExtras(vehicle, config)
-- Clear Extras
for i = 1, 20 do
Expand Down
Loading