Skip to content

Commit

Permalink
feat(resource): load callbacks
Browse files Browse the repository at this point in the history
Load lib.callback for internal use.
  • Loading branch information
thelindat committed Sep 13, 2022
1 parent 0ecb968 commit ed7bf53
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
2 changes: 2 additions & 0 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ shared_scripts {
}

client_scripts {
'imports/callback/client.lua',
'resource/**/client.lua',
'resource/**/client/*.lua'
}

server_scripts {
'imports/callback/server.lua',
'resource/**/server.lua',
'resource/**/server/*.lua'
}
Expand Down
8 changes: 4 additions & 4 deletions imports/callback/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ local function triggerServerCallback(_, event, delay, cb, ...)
end

---@overload fun(event: string, delay: number, cb: function, ...)
local callback = setmetatable({}, {
lib.callback = setmetatable({}, {
__call = triggerServerCallback
})

---@param event string
---@param delay number prevent the event from being called for the given time
--- Sends an event to the server and halts the current thread until a response is returned.
function callback.await(event, delay, ...)
function lib.callback.await(event, delay, ...)
return triggerServerCallback(_, event, delay, false, ...)
end

Expand All @@ -87,10 +87,10 @@ local pcall = pcall
---@param name string
---@param cb function
--- Registers an event handler and callback function to respond to server requests.
function callback.register(name, cb)
function lib.callback.register(name, cb)
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
TriggerServerEvent(cbEvent:format(resource), key, callbackResponse(pcall(cb, ...)))
end)
end

return callback
return lib.callback
8 changes: 4 additions & 4 deletions imports/callback/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ local function triggerClientCallback(_, event, playerId, cb, ...)
end

---@overload fun(event: string, playerId: number, cb: function, ...)
local callback = setmetatable({}, {
lib.callback = setmetatable({}, {
__call = triggerClientCallback
})

---@param event string
---@param playerId number
--- Sends an event to a client and halts the current thread until a response is returned.
function callback.await(event, playerId, ...)
function lib.callback.await(event, playerId, ...)
return triggerClientCallback(_, event, playerId, false, ...)
end

Expand All @@ -68,10 +68,10 @@ local pcall = pcall
---@param name string
---@param cb function
--- Registers an event handler and callback function to respond to client requests.
function callback.register(name, cb)
function lib.callback.register(name, cb)
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
TriggerClientEvent(cbEvent:format(resource), source, key, callbackResponse(pcall(cb, source, ...)))
end)
end

return callback
return lib.callback
4 changes: 1 addition & 3 deletions resource/cache/client.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local cache = {}
local cache = _ENV.cache
cache.playerId = PlayerId()
cache.serverId = GetPlayerServerId(cache.playerId)

Expand Down Expand Up @@ -44,5 +44,3 @@ end)
function lib.cache(key)
return cache[key]
end

_ENV.cache = cache
7 changes: 6 additions & 1 deletion resource/main.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
lib = setmetatable({
name = 'ox_lib'
name = 'ox_lib',
context = IsDuplicityVersion() and 'server' or 'client',
}, {
__newindex = function(self, name, fn)
exports(name, fn)
rawset(self, name, fn)
end
})

cache = {
resource = lib.name
}

if not LoadResourceFile(lib.name, 'web/build/index.html') then
error('Unable to load UI. Build ox_lib or download the latest release.\n ^3https://github.com/overextended/ox_lib/releases/latest/download/ox_lib.zip^0')
end

0 comments on commit ed7bf53

Please sign in to comment.