Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(es_extended): Extra callback functions #1505

Merged
39 changes: 29 additions & 10 deletions [core]/es_extended/client/modules/callback.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
---@diagnostic disable: duplicate-set-field

-- =============================================
-- MARK: Variables
-- =============================================

Callbacks = {}

Callbacks.requests = {}
Callbacks.storage = {}
Callbacks.id = 0

-- =============================================
-- MARK: Internal Functions
-- =============================================

function Callbacks:Trigger(event, cb, invoker, ...)

self.requests[self.id] = {
Expand Down Expand Up @@ -68,11 +76,15 @@ function Callbacks:ClientRecieve(eventName, requestId, invoker, ...)
self:Execute(callback, requestId, returnCb, ...)
end

-- =============================================
-- MARK: ESX Functions
-- =============================================

---@param eventName string
---@param callback function
---@param ... any
---@return nil
ESX.TriggerServerCallback = function(eventName, callback, ...)
function ESX.TriggerServerCallback(eventName, callback, ...)
local invokingResource = GetInvokingResource()
local invoker = (invokingResource and invokingResource ~= "unknown") and invokingResource or "es_extended"

Expand All @@ -82,7 +94,7 @@ end
---@param eventName string
---@param ... any
---@return any
ESX.AwaitServerCallback = function(eventName, ...)
function ESX.AwaitServerCallback(eventName, ...)
local invokingResource = GetInvokingResource()
local invoker = (invokingResource and invokingResource ~= "unknown") and invokingResource or "es_extended"

Expand All @@ -101,24 +113,31 @@ ESX.AwaitServerCallback = function(eventName, ...)
return table.unpack(p.value)
end

ESX.SecureNetEvent("esx:serverCallback", function(...)
Callbacks:ServerRecieve(...)
end)

---@param eventName string
---@param callback function
---@return nil
ESX.RegisterClientCallback = function(eventName, callback)
function ESX.RegisterClientCallback(eventName, callback)
local invokingResource = GetInvokingResource()
local invoker = (invokingResource and invokingResource ~= "Unknown") and invokingResource or "es_extended"

Callbacks:Register(eventName, invoker, callback)
end

---@param eventName string
---@return boolean
function ESX.DoesClientCallbackExist(eventName)
return Callbacks.storage[eventName] ~= nil
end

-- =============================================
-- MARK: Events
-- =============================================

ESX.SecureNetEvent("esx:triggerClientCallback", function(...)
Callbacks:ClientRecieve(...)
end)

ESX.SecureNetEvent("esx:serverCallback", function(...)
Callbacks:ServerRecieve(...)
end)

AddEventHandler("onResourceStop", function(resource)
for k, v in pairs(Callbacks.storage) do
if v.resource == resource then
Expand Down
42 changes: 32 additions & 10 deletions [core]/es_extended/server/modules/callback.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
---@diagnostic disable: duplicate-set-field

-- =============================================
-- MARK: Variables
-- =============================================

Callbacks = {}

Callbacks.requests = {}
Callbacks.storage = {}
Callbacks.id = 0

-- =============================================
-- MARK: Internal Functions
-- =============================================

function Callbacks:Register(name, resource, cb)
self.storage[name] = {
resource = resource,
Expand Down Expand Up @@ -60,15 +68,9 @@ function Callbacks:RecieveClient(requestId, invoker, ...)
self.requests[requestId] = nil
end

RegisterNetEvent("esx:clientCallback", function(requestId, invoker, ...)
Callbacks:RecieveClient(requestId, invoker, ...)
end)

RegisterNetEvent("esx:triggerServerCallback", function(eventName, requestId, invoker, ...)
local source = source
Callbacks:ServerRecieve(source, eventName, requestId, invoker, ...)
end)

-- =============================================
-- MARK: ESX Functions
-- =============================================

---@param player number playerId
---@param eventName string
Expand All @@ -83,13 +85,33 @@ end

---@param eventName string
---@param callback function
ESX.RegisterServerCallback = function(eventName, callback)
---@return nil
function ESX.RegisterServerCallback(eventName, callback)
local invokingResource = GetInvokingResource()
local invoker = (invokingResource and invokingResource ~= "Unknown") and invokingResource or "es_extended"

Callbacks:Register(eventName, invoker, callback)
end

---@param eventName string
---@return boolean
function ESX.DoesServerCallbackExist(eventName)
return Callbacks.storage[eventName] ~= nil
end

-- =============================================
-- MARK: Events
-- =============================================

RegisterNetEvent("esx:clientCallback", function(requestId, invoker, ...)
Callbacks:RecieveClient(requestId, invoker, ...)
end)

RegisterNetEvent("esx:triggerServerCallback", function(eventName, requestId, invoker, ...)
local source = source
Callbacks:ServerRecieve(source, eventName, requestId, invoker, ...)
end)

AddEventHandler("onResourceStop", function(resource)
for k, v in pairs(Callbacks.storage) do
if v.resource == resource then
Expand Down
Loading