refactor(callbacks): randomise event callback and fix timer logic

Prevent any possibility of listening for server callback or conflicting event handlers on the client.
This commit is contained in:
Linden
2022-01-01 02:26:42 +11:00
parent b8ad392692
commit 2084897785
3 changed files with 26 additions and 20 deletions

View File

@@ -23,7 +23,7 @@ game 'gta5'
--[[ Resource Information ]]-- --[[ Resource Information ]]--
name 'pe-lualib' name 'pe-lualib'
author 'Linden' author 'Linden'
version '1.2.0' version '1.2.1'
repository 'https://github.com/project-error/pe-lualib' repository 'https://github.com/project-error/pe-lualib'
description 'A library of shared functions to utilise in other resources.' description 'A library of shared functions to utilise in other resources.'

View File

@@ -2,7 +2,7 @@ local ServerCallbacks = {}
---@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 callbackTimer(event, delay)
if type(delay) == 'number' then if type(delay) == 'number' then
local time = GetGameTimer() local time = GetGameTimer()
if (ServerCallbacks[event] or 0) > time then if (ServerCallbacks[event] or 0) > time then
@@ -10,6 +10,14 @@ local function CallbackTimer(event, delay)
end end
ServerCallbacks[event] = time + delay ServerCallbacks[event] = time + delay
end end
return true
end
local function startCallback(resource, event, ...)
local id = math.random(0, 100000)
event = ('__cb_%s:%s'):format(resource, event)
TriggerServerEvent(event, id, ...)
return event..id
end end
local ServerCallback = table.create(0, 2) local ServerCallback = table.create(0, 2)
@@ -19,15 +27,14 @@ local ServerCallback = table.create(0, 2)
---@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.
ServerCallback.Await = function(resource, event, delay, ...) ServerCallback.Await = function(resource, event, delay, ...)
CallbackTimer(event, delay) if callbackTimer(event, delay) then
event = ('__cb_%s:%s'):format(resource, event) local promise = promise.new()
TriggerServerEvent(event, ...) event = RegisterNetEvent(startCallback(resource, event, ...), function(...)
local promise = promise.new() promise:resolve({...})
event = RegisterNetEvent(event, function(...) RemoveEventHandler(event)
promise:resolve({...}) end)
RemoveEventHandler(event) return table.unpack(Citizen.Await(promise))
end) end
return table.unpack(Citizen.Await(promise))
end end
---@param resource string ---@param resource string
@@ -36,13 +43,12 @@ end
---@param cb function ---@param cb function
--- Sends an event to the server and triggers a callback function once the response is returned. --- Sends an event to the server and triggers a callback function once the response is returned.
ServerCallback.Async = function(resource, event, delay, cb, ...) ServerCallback.Async = function(resource, event, delay, cb, ...)
CallbackTimer(event, delay) if callbackTimer(event, delay) then
event = ('__cb_%s:%s'):format(resource, event) event = RegisterNetEvent(startCallback(resource, event, ...), function(...)
TriggerServerEvent(event, ...) cb(...)
event = RegisterNetEvent(event, function(...) RemoveEventHandler(event)
cb(...) end)
RemoveEventHandler(event) end
end)
end end
return ServerCallback return ServerCallback

View File

@@ -6,10 +6,10 @@ local ServerCallback = {}
ServerCallback.Register = function(name, callback) ServerCallback.Register = function(name, callback)
name = ('__cb_%s:%s'):format(GetCurrentResourceName(), name) name = ('__cb_%s:%s'):format(GetCurrentResourceName(), name)
RegisterServerEvent(name, function(...) RegisterServerEvent(name, function(id, ...)
local source = source local source = source
callback(source, function(...) callback(source, function(...)
TriggerClientEvent(name, source, ...) TriggerClientEvent(name..id, source, ...)
end, ...) end, ...)
end) end)
end end