mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-29 01:08:54 +00:00
refactor(es_extended/client:) rewrite callbacks
This commit is contained in:
@@ -1,42 +1,81 @@
|
||||
local RequestId = 0
|
||||
local serverRequests = {}
|
||||
Callbacks = {}
|
||||
Callbacks.__index = Callbacks
|
||||
|
||||
local clientCallbacks = {}
|
||||
Callbacks.requests = {}
|
||||
Callbacks.storage = {}
|
||||
Callbacks.id = 0
|
||||
|
||||
function Callbacks:Trigger(event, cb, invoker, ...)
|
||||
self.requests[event] = cb
|
||||
TriggerServerEvent("esx:triggerServerCallback", event, self.id, invoker, ...)
|
||||
|
||||
self.id += 1
|
||||
end
|
||||
|
||||
function Callbacks:Execute(cb, ...)
|
||||
local success, errorString = pcall(cb, ...)
|
||||
|
||||
if not success then
|
||||
error(("Failed to execute Callback with RequestId: ^5%s^1"):format(self.currentId ))
|
||||
error(errorString)
|
||||
return
|
||||
end
|
||||
self.currentId = nil
|
||||
end
|
||||
|
||||
function Callbacks:ServerRecieve(requestId, invoker, ...)
|
||||
self.currentId = requestId
|
||||
|
||||
if not self.requests[self.currentId] then
|
||||
return error(("Server Callback with requestId ^5%s^1 Was Called by ^5%s^1 but does not exist."):format(self.currentId, invoker))
|
||||
end
|
||||
|
||||
local callback = self.requests[self.currentId]
|
||||
|
||||
Callbacks:Execute(callback, ...)
|
||||
end
|
||||
|
||||
function Callbacks:Register(name, cb)
|
||||
self.storage[name] = cb
|
||||
end
|
||||
|
||||
function Callbacks:RecieveClient(eventName, requestId, invoker, ...)
|
||||
self.currentId = requestId
|
||||
|
||||
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
|
||||
|
||||
local returnCb = function(...)
|
||||
TriggerServerEvent("esx:clientCallback", self.currentId, invoker, ...)
|
||||
end
|
||||
local callback = self.storage[eventName]
|
||||
|
||||
Callbacks:Execute(callback, returnCb, ...)
|
||||
end
|
||||
|
||||
---@param eventName string
|
||||
---@param callback function
|
||||
---@param ... any
|
||||
---@return nil
|
||||
ESX.TriggerServerCallback = function(eventName, callback, ...)
|
||||
serverRequests[RequestId] = callback
|
||||
local invokingResource = GetInvokingResource()
|
||||
local invoker = (invokingResource and invokingResource ~= "unknown") and invokingResource or "es_extended"
|
||||
|
||||
TriggerServerEvent("esx:triggerServerCallback", eventName, RequestId, GetInvokingResource() or "unknown", ...)
|
||||
|
||||
RequestId = RequestId + 1
|
||||
Callbacks:Trigger(eventName, callback, invoker, ...)
|
||||
end
|
||||
|
||||
RegisterNetEvent("esx:serverCallback", function(requestId, invoker, ...)
|
||||
if not serverRequests[requestId] then
|
||||
return error(("Server Callback with requestId ^5%s^1 Was Called by ^5%s^1 but does not exist."):format(requestId, invoker))
|
||||
end
|
||||
|
||||
serverRequests[requestId](...)
|
||||
serverRequests[requestId] = nil
|
||||
RegisterNetEvent("esx:serverCallback", function(...)
|
||||
Callbacks:ServerRecieve(...)
|
||||
end)
|
||||
|
||||
---@param eventName string
|
||||
---@param callback function
|
||||
---@return nil
|
||||
ESX.RegisterClientCallback = function(eventName, callback)
|
||||
clientCallbacks[eventName] = callback
|
||||
Callbacks:Register(eventName, callback)
|
||||
end
|
||||
|
||||
RegisterNetEvent("esx:triggerClientCallback", function(eventName, requestId, invoker, ...)
|
||||
if not clientCallbacks[eventName] then
|
||||
return error(("Client Callback not registered, name: ^5%s^1, invoker resource: ^5%s^1"):format(eventName, invoker))
|
||||
end
|
||||
|
||||
clientCallbacks[eventName](function(...)
|
||||
TriggerServerEvent("esx:clientCallback", requestId, invoker, ...)
|
||||
end, ...)
|
||||
RegisterNetEvent("esx:triggerClientCallback", function(...)
|
||||
Callbacks:RecieveClient(...)
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user