mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-18 23:46:03 +01:00
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:
@@ -23,7 +23,7 @@ game 'gta5'
|
|||||||
--[[ Resource Information ]]--
|
--[[ Resource Information ]]--
|
||||||
name 'pe-lualib'
|
name 'pe-lualib'
|
||||||
author 'Linden'
|
author 'Linden'
|
||||||
version '1.2.1'
|
version '1.3.0'
|
||||||
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.'
|
||||||
|
|
||||||
|
|||||||
@@ -29,8 +29,8 @@ local ServerCallback = table.create(0, 2)
|
|||||||
ServerCallback.Await = function(resource, event, delay, ...)
|
ServerCallback.Await = function(resource, event, delay, ...)
|
||||||
if callbackTimer(event, delay) then
|
if callbackTimer(event, delay) then
|
||||||
local promise = promise.new()
|
local promise = promise.new()
|
||||||
event = RegisterNetEvent(startCallback(resource, event, ...), function(...)
|
event = RegisterNetEvent(startCallback(resource, event, ...), function(response)
|
||||||
promise:resolve({...})
|
promise:resolve(response)
|
||||||
RemoveEventHandler(event)
|
RemoveEventHandler(event)
|
||||||
end)
|
end)
|
||||||
return table.unpack(Citizen.Await(promise))
|
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.
|
--- 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, ...)
|
||||||
if callbackTimer(event, delay) then
|
if callbackTimer(event, delay) then
|
||||||
event = RegisterNetEvent(startCallback(resource, event, ...), function(...)
|
event = RegisterNetEvent(startCallback(resource, event, ...), function(response)
|
||||||
cb(...)
|
cb(table.unpack(response))
|
||||||
RemoveEventHandler(event)
|
RemoveEventHandler(event)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,17 +1,14 @@
|
|||||||
local ServerCallback = {}
|
local ServerCallback = {}
|
||||||
|
|
||||||
---@param name string
|
---@param name string
|
||||||
---@param callback function
|
---@param callback function
|
||||||
--- Registers an event handler and callback function to respond to client requests.
|
--- Registers an event handler and callback function to respond to client requests.
|
||||||
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(id, ...)
|
RegisterServerEvent(name, function(id, ...)
|
||||||
local source = source
|
TriggerClientEvent(name..id, source, {callback(source, ...)})
|
||||||
callback(source, function(...)
|
|
||||||
TriggerClientEvent(name..id, source, ...)
|
|
||||||
end, ...)
|
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
return ServerCallback
|
return ServerCallback
|
||||||
Reference in New Issue
Block a user