mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-18 15:36:02 +01:00
feat(resource): load callbacks
Load lib.callback for internal use.
This commit is contained in:
@@ -37,11 +37,13 @@ shared_scripts {
|
|||||||
}
|
}
|
||||||
|
|
||||||
client_scripts {
|
client_scripts {
|
||||||
|
'imports/callback/client.lua',
|
||||||
'resource/**/client.lua',
|
'resource/**/client.lua',
|
||||||
'resource/**/client/*.lua'
|
'resource/**/client/*.lua'
|
||||||
}
|
}
|
||||||
|
|
||||||
server_scripts {
|
server_scripts {
|
||||||
|
'imports/callback/server.lua',
|
||||||
'resource/**/server.lua',
|
'resource/**/server.lua',
|
||||||
'resource/**/server/*.lua'
|
'resource/**/server/*.lua'
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,14 +59,14 @@ local function triggerServerCallback(_, event, delay, cb, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---@overload fun(event: string, delay: number, cb: function, ...)
|
---@overload fun(event: string, delay: number, cb: function, ...)
|
||||||
local callback = setmetatable({}, {
|
lib.callback = setmetatable({}, {
|
||||||
__call = triggerServerCallback
|
__call = triggerServerCallback
|
||||||
})
|
})
|
||||||
|
|
||||||
---@param event string
|
---@param event string
|
||||||
---@param delay number prevent the event from being called for the given time
|
---@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.
|
--- 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, ...)
|
return triggerServerCallback(_, event, delay, false, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -87,10 +87,10 @@ local pcall = pcall
|
|||||||
---@param name string
|
---@param name string
|
||||||
---@param cb function
|
---@param cb function
|
||||||
--- Registers an event handler and callback function to respond to server requests.
|
--- 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, ...)
|
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
|
||||||
TriggerServerEvent(cbEvent:format(resource), key, callbackResponse(pcall(cb, ...)))
|
TriggerServerEvent(cbEvent:format(resource), key, callbackResponse(pcall(cb, ...)))
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
return callback
|
return lib.callback
|
||||||
|
|||||||
@@ -40,14 +40,14 @@ local function triggerClientCallback(_, event, playerId, cb, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---@overload fun(event: string, playerId: number, cb: function, ...)
|
---@overload fun(event: string, playerId: number, cb: function, ...)
|
||||||
local callback = setmetatable({}, {
|
lib.callback = setmetatable({}, {
|
||||||
__call = triggerClientCallback
|
__call = triggerClientCallback
|
||||||
})
|
})
|
||||||
|
|
||||||
---@param event string
|
---@param event string
|
||||||
---@param playerId number
|
---@param playerId number
|
||||||
--- Sends an event to a client and halts the current thread until a response is returned.
|
--- 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, ...)
|
return triggerClientCallback(_, event, playerId, false, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -68,10 +68,10 @@ local pcall = pcall
|
|||||||
---@param name string
|
---@param name string
|
||||||
---@param cb function
|
---@param cb function
|
||||||
--- Registers an event handler and callback function to respond to client requests.
|
--- 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, ...)
|
RegisterNetEvent(cbEvent:format(name), function(resource, key, ...)
|
||||||
TriggerClientEvent(cbEvent:format(resource), source, key, callbackResponse(pcall(cb, source, ...)))
|
TriggerClientEvent(cbEvent:format(resource), source, key, callbackResponse(pcall(cb, source, ...)))
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
return callback
|
return lib.callback
|
||||||
4
resource/cache/client.lua
vendored
4
resource/cache/client.lua
vendored
@@ -1,4 +1,4 @@
|
|||||||
local cache = {}
|
local cache = _ENV.cache
|
||||||
cache.playerId = PlayerId()
|
cache.playerId = PlayerId()
|
||||||
cache.serverId = GetPlayerServerId(cache.playerId)
|
cache.serverId = GetPlayerServerId(cache.playerId)
|
||||||
|
|
||||||
@@ -44,5 +44,3 @@ end)
|
|||||||
function lib.cache(key)
|
function lib.cache(key)
|
||||||
return cache[key]
|
return cache[key]
|
||||||
end
|
end
|
||||||
|
|
||||||
_ENV.cache = cache
|
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
lib = setmetatable({
|
lib = setmetatable({
|
||||||
name = 'ox_lib'
|
name = 'ox_lib',
|
||||||
|
context = IsDuplicityVersion() and 'server' or 'client',
|
||||||
}, {
|
}, {
|
||||||
__newindex = function(self, name, fn)
|
__newindex = function(self, name, fn)
|
||||||
exports(name, fn)
|
exports(name, fn)
|
||||||
@@ -7,6 +8,10 @@ lib = setmetatable({
|
|||||||
end
|
end
|
||||||
})
|
})
|
||||||
|
|
||||||
|
cache = {
|
||||||
|
resource = lib.name
|
||||||
|
}
|
||||||
|
|
||||||
if not LoadResourceFile(lib.name, 'web/build/index.html') then
|
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')
|
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user