mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-29 01:08:54 +00:00
Merge pull request #1629 from Mirrrrrow/dev
feat(callback): add `AwaitClientCallback` function
This commit is contained in:
@@ -14,8 +14,25 @@ Callbacks.id = 0
|
||||
-- MARK: Internal Functions
|
||||
-- =============================================
|
||||
|
||||
function Callbacks:Trigger(event, cb, invoker, ...)
|
||||
|
||||
function Callbacks:Register(name, resource, cb)
|
||||
self.storage[name] = {
|
||||
resource = resource,
|
||||
cb = cb
|
||||
}
|
||||
end
|
||||
|
||||
function Callbacks:Execute(cb, id, ...)
|
||||
local success, errorString = pcall(cb, ...)
|
||||
|
||||
if not success then
|
||||
print(("[^1ERROR^7] Failed to execute Callback with RequestId: ^5%s^7"):format(id))
|
||||
error(errorString)
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
function Callbacks:Trigger(event, cb, invoker, ...)
|
||||
self.requests[self.id] = {
|
||||
await = type(cb) == "boolean",
|
||||
cb = cb or promise:new()
|
||||
@@ -29,16 +46,6 @@ function Callbacks:Trigger(event, cb, invoker, ...)
|
||||
return table.cb
|
||||
end
|
||||
|
||||
function Callbacks:Execute(cb, id, ...)
|
||||
local success, errorString = pcall(cb, ...)
|
||||
|
||||
if not success then
|
||||
print(("[^1ERROR^7] Failed to execute Callback with RequestId: ^5%s^7"):format(id))
|
||||
error(errorString)
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
function Callbacks:ServerRecieve(requestId, invoker, ...)
|
||||
if not self.requests[requestId] then
|
||||
return error(("Server Callback with requestId ^5%s^1 Was Called by ^5%s^1 but does not exist."):format(requestId, invoker))
|
||||
@@ -49,21 +56,13 @@ function Callbacks:ServerRecieve(requestId, invoker, ...)
|
||||
self.requests[requestId] = nil
|
||||
|
||||
if callback.await then
|
||||
callback.cb:resolve({...})
|
||||
callback.cb:resolve({ ... })
|
||||
else
|
||||
self:Execute(callback.cb, requestId, ...)
|
||||
end
|
||||
end
|
||||
|
||||
function Callbacks:Register(name, resource, cb)
|
||||
self.storage[name] = {
|
||||
resource = resource,
|
||||
cb = cb
|
||||
}
|
||||
end
|
||||
|
||||
function Callbacks:ClientRecieve(eventName, requestId, invoker, ...)
|
||||
|
||||
if not self.storage[eventName] then
|
||||
return error(("Client Callback with requestId ^5%s^1 Was Called by ^5%s^1 but does not exist."):format(eventName, invoker))
|
||||
end
|
||||
@@ -130,14 +129,14 @@ end
|
||||
-- MARK: Events
|
||||
-- =============================================
|
||||
|
||||
ESX.SecureNetEvent("esx:triggerClientCallback", function(...)
|
||||
Callbacks:ClientRecieve(...)
|
||||
end)
|
||||
|
||||
ESX.SecureNetEvent("esx:serverCallback", function(...)
|
||||
Callbacks:ServerRecieve(...)
|
||||
end)
|
||||
|
||||
ESX.SecureNetEvent("esx:triggerClientCallback", function(...)
|
||||
Callbacks:ClientRecieve(...)
|
||||
end)
|
||||
|
||||
AddEventHandler("onResourceStop", function(resource)
|
||||
for k, v in pairs(Callbacks.storage) do
|
||||
if v.resource == resource then
|
||||
|
||||
@@ -33,11 +33,17 @@ function Callbacks:Execute(cb, ...)
|
||||
end
|
||||
|
||||
function Callbacks:Trigger(player, event, cb, invoker, ...)
|
||||
self.requests[self.id] = cb
|
||||
self.requests[self.id] = {
|
||||
await = type(cb) == "boolean",
|
||||
cb = cb or promise:new()
|
||||
}
|
||||
local table = self.requests[self.id]
|
||||
|
||||
TriggerClientEvent("esx:triggerClientCallback", player, event, self.id, invoker, ...)
|
||||
|
||||
self.id += 1
|
||||
|
||||
return table.cb
|
||||
end
|
||||
|
||||
function Callbacks:ServerRecieve(player, event, requestId, invoker, ...)
|
||||
@@ -64,8 +70,12 @@ function Callbacks:RecieveClient(requestId, invoker, ...)
|
||||
|
||||
local callback = self.requests[self.currentId]
|
||||
|
||||
self:Execute(callback, ...)
|
||||
self.requests[requestId] = nil
|
||||
if callback.await then
|
||||
callback.cb:resolve({ ... })
|
||||
else
|
||||
self:Execute(callback.cb, ...)
|
||||
end
|
||||
end
|
||||
|
||||
-- =============================================
|
||||
@@ -83,6 +93,28 @@ function ESX.TriggerClientCallback(player, eventName, callback, ...)
|
||||
Callbacks:Trigger(player, eventName, callback, invoker, ...)
|
||||
end
|
||||
|
||||
---@param player number playerId
|
||||
---@param eventName string
|
||||
---@param ... any
|
||||
---@return any
|
||||
function ESX.AwaitClientCallback(player, eventName, ...)
|
||||
local invokingResource = GetInvokingResource()
|
||||
local invoker = (invokingResource and invokingResource ~= "Unknown") and invokingResource or "es_extended"
|
||||
|
||||
local p = Callbacks:Trigger(player, eventName, false, invoker, ...)
|
||||
if not p then return end
|
||||
|
||||
SetTimeout(15000, function()
|
||||
if p.state == "pending" then
|
||||
p:reject("Server Callback Timed Out")
|
||||
end
|
||||
end)
|
||||
|
||||
Citizen.Await(p)
|
||||
|
||||
return table.unpack(p.value)
|
||||
end
|
||||
|
||||
---@param eventName string
|
||||
---@param callback function
|
||||
---@return nil
|
||||
|
||||
Reference in New Issue
Block a user