mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
refactor(callback): rewrite
Register a single net event to handle callback responses (per resource). Previous implementation registered new events for each callback.
This commit is contained in:
@@ -1,54 +1,72 @@
|
|||||||
local ServerCallbacks = {}
|
local events = {}
|
||||||
|
local timers = {}
|
||||||
|
local cbEvent = ('__cb_%s'):format(cache.resource)
|
||||||
|
|
||||||
|
RegisterNetEvent(cbEvent, function(key, ...)
|
||||||
|
local cb = events[key]
|
||||||
|
return cb and cb(...)
|
||||||
|
end)
|
||||||
|
|
||||||
---@param event string
|
---@param event string
|
||||||
---@param delay number prevent the event from being called for the given time
|
---@param delay number prevent the event from being called for the given time
|
||||||
local function callbackTimer(event, delay)
|
local function eventTimer(event, delay)
|
||||||
if type(delay) == 'number' then
|
if delay and type(delay) == 'number' and delay > 0 then
|
||||||
local time = GetGameTimer()
|
local time = GetGameTimer()
|
||||||
if (ServerCallbacks[event] or 0) > time then
|
|
||||||
|
if (timers[event] or 0) > time then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
ServerCallbacks[event] = time + delay
|
|
||||||
|
timers[event] = time + delay
|
||||||
end
|
end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
local function triggerCallback(event, ...)
|
---@param _ any
|
||||||
local id = math.random(0, 100000)
|
---@param event string
|
||||||
event = ('__cb_%s'):format(event)
|
---@param delay number
|
||||||
TriggerServerEvent(event, id, ...)
|
---@param cb function
|
||||||
return event..id
|
---@param ... unknown
|
||||||
|
---@return unknown
|
||||||
|
local function triggerServerCallback(_, event, delay, cb, ...)
|
||||||
|
if not eventTimer(event, delay) then return end
|
||||||
|
|
||||||
|
local key
|
||||||
|
|
||||||
|
repeat
|
||||||
|
key = ('%s:%s'):format(event, math.random(0, 100000))
|
||||||
|
until not events[key]
|
||||||
|
|
||||||
|
TriggerServerEvent(('__cb_%s'):format(event), key, ...)
|
||||||
|
|
||||||
|
local promise = not cb and promise.new()
|
||||||
|
|
||||||
|
events[key] = function(response)
|
||||||
|
events[key] = nil
|
||||||
|
|
||||||
|
if promise then
|
||||||
|
return promise:resolve(response)
|
||||||
|
end
|
||||||
|
|
||||||
|
cb(table.unpack(response))
|
||||||
|
end
|
||||||
|
|
||||||
|
if promise then
|
||||||
|
return table.unpack(Citizen.Await(promise))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---Sends an event to the server and triggers a callback function once the response is returned.
|
---@overload fun(event: string, delay: number, cb: function, ...)
|
||||||
---```
|
local callback = setmetatable({}, {
|
||||||
---callback(event: string, delay: number, function(...)
|
__call = triggerServerCallback
|
||||||
--- print(...)
|
})
|
||||||
---end)
|
|
||||||
---```
|
|
||||||
local callback = {}
|
|
||||||
|
|
||||||
---@param event string
|
---@param event string
|
||||||
---@param delay number prevent the event from being called for the given time
|
---@param delay number prevent the event from being called for the given time
|
||||||
--- Sends an event to the server and halts the current thread until a response is returned.
|
--- Sends an event to the server and halts the current thread until a response is returned.
|
||||||
function callback.await(event, delay, ...)
|
function callback.await(event, delay, ...)
|
||||||
if callbackTimer(event, delay) then
|
return triggerServerCallback(_, event, delay, false, ...)
|
||||||
local promise = promise.new()
|
|
||||||
event = RegisterNetEvent(triggerCallback(event, ...), function(response)
|
|
||||||
promise:resolve(response)
|
|
||||||
RemoveEventHandler(event)
|
|
||||||
end)
|
|
||||||
return table.unpack(Citizen.Await(promise))
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return setmetatable(callback, {
|
return callback
|
||||||
__call = function(self, event, delay, cb, ...)
|
|
||||||
if callbackTimer(event, delay) then
|
|
||||||
event = RegisterNetEvent(triggerCallback(event, ...), function(response)
|
|
||||||
cb(table.unpack(response))
|
|
||||||
RemoveEventHandler(event)
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
})
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
local callback = {}
|
local callback = {}
|
||||||
|
local cbEvent = ('__cb_%s'):format(cache.resource)
|
||||||
|
|
||||||
---@param name string
|
---@param name string
|
||||||
---@param cb function
|
---@param cb function
|
||||||
@@ -6,8 +7,8 @@ local callback = {}
|
|||||||
function callback.register(name, cb)
|
function callback.register(name, cb)
|
||||||
name = ('__cb_%s'):format(name)
|
name = ('__cb_%s'):format(name)
|
||||||
|
|
||||||
RegisterServerEvent(name, function(id, ...)
|
RegisterServerEvent(name, function(key, ...)
|
||||||
TriggerClientEvent(name..id, source, {cb(source, ...)})
|
TriggerClientEvent(cbEvent, source, key, { cb(source, ...) })
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user