Skip to content

feat(callbacks): latent callbcks #656

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

Closed
wants to merge 1 commit into from
Closed
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
53 changes: 48 additions & 5 deletions imports/callback/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ end
---@param cb function | false
---@param ... any
---@return ...
local function triggerServerCallback(_, event, delay, cb, ...)
local function triggerServerCallback(_, event, delay, cb, bps, ...)
if not eventTimer(event, delay) then return end

local key
Expand All @@ -41,7 +41,11 @@ local function triggerServerCallback(_, event, delay, cb, ...)
key = ('%s:%s'):format(event, math.random(0, 100000))
until not pendingCallbacks[key]

TriggerServerEvent(cbEvent:format(event), cache.resource, key, ...)
if bps then
TriggerLatentServerEvent(cbEvent:format(event), bps, cache.resource, key, ...)
else
TriggerServerEvent(cbEvent:format(event), cache.resource, key, ...)
end

---@type promise | false
local promise = not cb and promise.new()
Expand Down Expand Up @@ -77,7 +81,7 @@ lib.callback = setmetatable({}, {
assert(cbType == 'function', ("expected argument 3 to have type 'function' (received %s)"):format(cbType))
end

return triggerServerCallback(_, event, delay, cb, ...)
return triggerServerCallback(_, event, delay, cb, nil, ...)
end
})

Expand All @@ -86,7 +90,32 @@ lib.callback = setmetatable({}, {
---Sends an event to the server and halts the current thread until a response is returned.
---@diagnostic disable-next-line: duplicate-set-field
function lib.callback.await(event, delay, ...)
return triggerServerCallback(nil, event, delay, false, ...)
return triggerServerCallback(nil, event, delay, false, nil, ...)
end

---@param event string
---@param bps number
---@param cb function
---Works the same as lib.callback with this difference that this function sends latent client event
---@diagnostic disable-next-line: duplicate-set-field
function lib.callback.latent(event, bps, cb, ...)
if not bps or type(bps) ~= 'number' then
bps = 50000
end

return triggerServerCallback(nil, event, delay, cb, bps, ...)
end

---@param event string
---@param bps number
--- Sends an latent event to a client and halts the current thread until a response is returned.
---@diagnostic disable-next-line: duplicate-set-field
function lib.callback.latentAwait(event, bps, ...)
if not bps or type(bps) ~= 'number' then
bps = 50000
end

return triggerServerCallback(nil, event, delay, false, bps, ...)
end

local function callbackResponse(success, result, ...)
Expand Down Expand Up @@ -114,4 +143,18 @@ function lib.callback.register(name, cb)
end)
end

return lib.callback
---@param name string
---@param cb function
---@param bps number
---Works almost the same as lib.callback.register with the difference that this function sends latent server event
function lib.callback.registerLatent(name, bps, cb)
if not bps or type(bps) ~= 'number' then
bps = 50000
end

RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
TriggerLatentServerEvent(cbEvent:format(resource), bps, key, callbackResponse(pcall(cb, ...)))
end)
end

return lib.callback
51 changes: 47 additions & 4 deletions imports/callback/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ end)
---@param cb function|false
---@param ... any
---@return ...
local function triggerClientCallback(_, event, playerId, cb, ...)
local function triggerClientCallback(_, event, playerId, cb, bps, ...)
assert(DoesPlayerExist(playerId --[[@as string]]), ("target playerId '%s' does not exist"):format(playerId))

local key
Expand All @@ -24,7 +24,11 @@ local function triggerClientCallback(_, event, playerId, cb, ...)
key = ('%s:%s:%s'):format(event, math.random(0, 100000), playerId)
until not pendingCallbacks[key]

TriggerClientEvent(cbEvent:format(event), playerId, cache.resource, key, ...)
if bps then
TriggerLatentClientEvent(cbEvent:format(event), bps, playerId, cache.resource, key, ...)
else
TriggerClientEvent(cbEvent:format(event), playerId, cache.resource, key, ...)
end

---@type promise | false
local promise = not cb and promise.new()
Expand Down Expand Up @@ -60,7 +64,7 @@ lib.callback = setmetatable({}, {
assert(cbType == 'function', ("expected argument 3 to have type 'function' (received %s)"):format(cbType))
end

return triggerClientCallback(_, event, playerId, cb, ...)
return triggerClientCallback(_, event, playerId, cb, nil, ...)
end
})

Expand All @@ -69,7 +73,32 @@ lib.callback = setmetatable({}, {
--- Sends an event to a client and halts the current thread until a response is returned.
---@diagnostic disable-next-line: duplicate-set-field
function lib.callback.await(event, playerId, ...)
return triggerClientCallback(nil, event, playerId, false, ...)
return triggerClientCallback(nil, event, playerId, false, nil, ...)
end

---@param event string
---@param playerId number
---@param cb function
---Works the same as lib.callback with this difference that this function sends latent client event
---@diagnostic disable-next-line: duplicate-set-field
function lib.callback.latent(event, playerId, bps, cb, ...)
if not bps or type(bps) ~= 'number' then
bps = 50000
end

return triggerClientCallback(nil, event, playerId, cb, bps, ...)
end

---@param event string
---@param playerId number
--- Sends an latent event to a client and halts the current thread until a response is returned.
---@diagnostic disable-next-line: duplicate-set-field
function lib.callback.latentAwait(event, playerId, bps, ...)
if not bps or type(bps) ~= 'number' then
bps = 50000
end

return triggerClientCallback(nil, event, playerId, false, bps, ...)
end

local function callbackResponse(success, result, ...)
Expand Down Expand Up @@ -97,4 +126,18 @@ function lib.callback.register(name, cb)
end)
end

---@param name string
---@param cb function
---@param bps number
---Works almost the same as lib.callback.register with the difference that this function sends latent client event
function lib.callback.registerLatent(name, bps, cb)
if not bps or type(bps) ~= 'number' then
bps = 50000
end

RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
TriggerLatentClientEvent(cbEvent:format(resource), source, bps, key, callbackResponse(pcall(cb, source, ...)))
end)
end

return lib.callback