Skip to content

Commit

Permalink
fix(callback): handle external resource events
Browse files Browse the repository at this point in the history
Calling registered callback events in another resource should respond to
the invoking resource.
  • Loading branch information
thelindat committed Jun 19, 2022
1 parent db515f5 commit e95ae85
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
12 changes: 5 additions & 7 deletions imports/callback/client.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local events = {}
local timers = {}
local cbEvent = ('__cb_%s'):format(cache.resource)
local cbEvent = ('__ox_cb_%s')

RegisterNetEvent(cbEvent, function(key, ...)
RegisterNetEvent(cbEvent:format(cache.resource), function(key, ...)
local cb = events[key]
return cb and cb(...)
end)
Expand Down Expand Up @@ -38,7 +38,7 @@ local function triggerServerCallback(_, event, delay, cb, ...)
key = ('%s:%s'):format(event, math.random(0, 100000))
until not events[key]

TriggerServerEvent(('__cb_%s'):format(event), key, ...)
TriggerServerEvent(cbEvent:format(event), cache.resource, key, ...)

local promise = not cb and promise.new()

Expand Down Expand Up @@ -73,10 +73,8 @@ end
---@param cb function
--- Registers an event handler and callback function to respond to server requests.
function callback.register(name, cb)
name = ('__cb_%s'):format(name)

RegisterNetEvent(name, function(key, ...)
TriggerServerEvent(cbEvent, key, { cb(...) })
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
TriggerServerEvent(cbEvent:format(resource), key, { cb(...) })
end)
end

Expand Down
12 changes: 5 additions & 7 deletions imports/callback/server.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local events = {}
local cbEvent = ('__cb_%s'):format(cache.resource)
local cbEvent = ('__ox_cb_%s')

RegisterNetEvent(cbEvent, function(key, ...)
RegisterNetEvent(cbEvent:format(cache.resource), function(key, ...)
local cb = events[key]
return cb and cb(...)
end)
Expand All @@ -19,7 +19,7 @@ local function triggerClientCallback(_, event, playerId, cb, ...)
key = ('%s:%s:%s'):format(event, math.random(0, 100000), playerId)
until not events[key]

TriggerClientEvent(('__cb_%s'):format(event), playerId, key, ...)
TriggerClientEvent(cbEvent:format(event), playerId, cache.resource, key, ...)

local promise = not cb and promise.new()

Expand Down Expand Up @@ -54,10 +54,8 @@ end
---@param cb function
--- Registers an event handler and callback function to respond to client requests.
function callback.register(name, cb)
name = ('__cb_%s'):format(name)

RegisterNetEvent(name, function(key, ...)
TriggerClientEvent(cbEvent, source, key, { cb(source, ...) })
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
TriggerClientEvent(cbEvent:format(resource), source, key, { cb(source, ...) })
end)
end

Expand Down

0 comments on commit e95ae85

Please sign in to comment.