refactor(callbacks): remove the cb argument

BREAKING CHANGE: Registered callbacks now return values rather than sending them as a callback.

Previously the cb needed to be called to return to the client, now it will happen once the function closes. i.e.

cb(string, string, false) -> return string, string, false
This commit is contained in:
Linden
2022-01-01 20:43:06 +11:00
parent 2084897785
commit 72575c90ee
3 changed files with 14 additions and 17 deletions

View File

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

View File

@@ -29,8 +29,8 @@ local ServerCallback = table.create(0, 2)
ServerCallback.Await = function(resource, event, delay, ...)
if callbackTimer(event, delay) then
local promise = promise.new()
event = RegisterNetEvent(startCallback(resource, event, ...), function(...)
promise:resolve({...})
event = RegisterNetEvent(startCallback(resource, event, ...), function(response)
promise:resolve(response)
RemoveEventHandler(event)
end)
return table.unpack(Citizen.Await(promise))
@@ -44,8 +44,8 @@ end
--- Sends an event to the server and triggers a callback function once the response is returned.
ServerCallback.Async = function(resource, event, delay, cb, ...)
if callbackTimer(event, delay) then
event = RegisterNetEvent(startCallback(resource, event, ...), function(...)
cb(...)
event = RegisterNetEvent(startCallback(resource, event, ...), function(response)
cb(table.unpack(response))
RemoveEventHandler(event)
end)
end

View File

@@ -1,17 +1,14 @@
local ServerCallback = {}
---@param name string
---@param callback function
--- Registers an event handler and callback function to respond to client requests.
ServerCallback.Register = function(name, callback)
name = ('__cb_%s:%s'):format(GetCurrentResourceName(), name)
---@param name string
---@param callback function
--- Registers an event handler and callback function to respond to client requests.
ServerCallback.Register = function(name, callback)
name = ('__cb_%s:%s'):format(GetCurrentResourceName(), name)
RegisterServerEvent(name, function(id, ...)
local source = source
callback(source, function(...)
TriggerClientEvent(name..id, source, ...)
end, ...)
end)
end
RegisterServerEvent(name, function(id, ...)
TriggerClientEvent(name..id, source, {callback(source, ...)})
end)
end
return ServerCallback