mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
refactor: rename imports
BREAKING! Since the folder name determines the import name (i.e. lib.callbacks), use better names and lowerCamelCase.
This commit is contained in:
@@ -23,11 +23,11 @@ game 'gta5'
|
||||
--[[ Resource Information ]]--
|
||||
name 'ox_lib'
|
||||
author 'Linden'
|
||||
version '1.5.0'
|
||||
version '2.0.0'
|
||||
repository 'https://github.com/overextended/ox_lib'
|
||||
description 'A library of shared functions to utilise in other resources.'
|
||||
|
||||
[[ Manifest ]]--
|
||||
--[[ Manifest ]]--
|
||||
dependencies {
|
||||
'/server:5104',
|
||||
'/onesync',
|
||||
|
||||
@@ -1,54 +1,54 @@
|
||||
local ServerCallbacks = {}
|
||||
|
||||
---@param event string
|
||||
---@param delay number prevent the event from being called for the given time
|
||||
local function callbackTimer(event, delay)
|
||||
if type(delay) == 'number' then
|
||||
local time = GetGameTimer()
|
||||
if (ServerCallbacks[event] or 0) > time then
|
||||
return false
|
||||
end
|
||||
ServerCallbacks[event] = time + delay
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local function startCallback(resource, event, ...)
|
||||
local id = math.random(0, 100000)
|
||||
event = ('__cb_%s:%s'):format(resource, event)
|
||||
TriggerServerEvent(event, id, ...)
|
||||
return event..id
|
||||
end
|
||||
|
||||
local ServerCallback = table.create(0, 2)
|
||||
|
||||
---@param resource string
|
||||
---@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.
|
||||
ServerCallback.Await = function(resource, event, delay, ...)
|
||||
if callbackTimer(event, delay) then
|
||||
local promise = promise.new()
|
||||
event = RegisterNetEvent(startCallback(resource, event, ...), function(response)
|
||||
promise:resolve(response)
|
||||
RemoveEventHandler(event)
|
||||
end)
|
||||
return table.unpack(Citizen.Await(promise))
|
||||
end
|
||||
end
|
||||
|
||||
---@param resource string
|
||||
---@param event string
|
||||
---@param delay number
|
||||
---@param cb function
|
||||
--- Sends an event to the server and triggers a callback function once the response is returned.
|
||||
ServerCallback.Async = function(resource, event, delay, cb, ...)
|
||||
if callbackTimer(event, delay) then
|
||||
event = RegisterNetEvent(startCallback(resource, event, ...), function(response)
|
||||
cb(table.unpack(response))
|
||||
RemoveEventHandler(event)
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
return ServerCallback
|
||||
local ServerCallbacks = {}
|
||||
|
||||
---@param event string
|
||||
---@param delay number prevent the event from being called for the given time
|
||||
local function callbackTimer(event, delay)
|
||||
if type(delay) == 'number' then
|
||||
local time = GetGameTimer()
|
||||
if (ServerCallbacks[event] or 0) > time then
|
||||
return false
|
||||
end
|
||||
ServerCallbacks[event] = time + delay
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local function startCallback(resource, event, ...)
|
||||
local id = math.random(0, 100000)
|
||||
event = ('__cb_%s:%s'):format(resource, event)
|
||||
TriggerServerEvent(event, id, ...)
|
||||
return event..id
|
||||
end
|
||||
|
||||
local ServerCallback = table.create(0, 2)
|
||||
|
||||
---@param resource string
|
||||
---@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.
|
||||
ServerCallback.Await = function(resource, event, delay, ...)
|
||||
if callbackTimer(event, delay) then
|
||||
local promise = promise.new()
|
||||
event = RegisterNetEvent(startCallback(resource, event, ...), function(response)
|
||||
promise:resolve(response)
|
||||
RemoveEventHandler(event)
|
||||
end)
|
||||
return table.unpack(Citizen.Await(promise))
|
||||
end
|
||||
end
|
||||
|
||||
---@param resource string
|
||||
---@param event string
|
||||
---@param delay number
|
||||
---@param cb function
|
||||
--- Sends an event to the server and triggers a callback function once the response is returned.
|
||||
ServerCallback.Async = function(resource, event, delay, cb, ...)
|
||||
if callbackTimer(event, delay) then
|
||||
event = RegisterNetEvent(startCallback(resource, event, ...), function(response)
|
||||
cb(table.unpack(response))
|
||||
RemoveEventHandler(event)
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
return ServerCallback
|
||||
@@ -1,14 +1,14 @@
|
||||
local ServerCallback = {}
|
||||
|
||||
---@param name string
|
||||
---@param callback function
|
||||
--- Registers an event handler and callback function to respond to client requests.
|
||||
ServerCallback.Register = function(name, callback)
|
||||
name = ('__cb_%s:%s'):format(GetCurrentResourceName(), name)
|
||||
|
||||
RegisterServerEvent(name, function(id, ...)
|
||||
TriggerClientEvent(name..id, source, {callback(source, ...)})
|
||||
end)
|
||||
end
|
||||
|
||||
local ServerCallback = {}
|
||||
|
||||
---@param name string
|
||||
---@param callback function
|
||||
--- Registers an event handler and callback function to respond to client requests.
|
||||
ServerCallback.Register = function(name, callback)
|
||||
name = ('__cb_%s:%s'):format(GetCurrentResourceName(), name)
|
||||
|
||||
RegisterServerEvent(name, function(id, ...)
|
||||
TriggerClientEvent(name..id, source, {callback(source, ...)})
|
||||
end)
|
||||
end
|
||||
|
||||
return ServerCallback
|
||||
@@ -1,11 +1,11 @@
|
||||
--- Call on frame to disable all stored keys.
|
||||
--- ```
|
||||
--- DisableControlActions()
|
||||
--- disableControls()
|
||||
--- ```
|
||||
local DisableControlActions = {}
|
||||
local disableControls = {}
|
||||
|
||||
---@param ... number
|
||||
function DisableControlActions:Add(...)
|
||||
function disableControls:Add(...)
|
||||
local keys = type(...) == 'table' and ... or {...}
|
||||
for i=1, #keys do
|
||||
local key = keys[i]
|
||||
@@ -18,7 +18,7 @@ function DisableControlActions:Add(...)
|
||||
end
|
||||
|
||||
---@param ... number
|
||||
function DisableControlActions:Remove(...)
|
||||
function disableControls:Remove(...)
|
||||
local keys = type(...) == 'table' and ... or {...}
|
||||
for i=1, #keys do
|
||||
local key = keys[i]
|
||||
@@ -32,22 +32,22 @@ function DisableControlActions:Remove(...)
|
||||
end
|
||||
|
||||
---@param ... number
|
||||
function DisableControlActions:Clear(...)
|
||||
function disableControls:Clear(...)
|
||||
local keys = type(...) == 'table' and ... or {...}
|
||||
for i=1, #keys do
|
||||
self[keys[i]] = nil
|
||||
end
|
||||
end
|
||||
|
||||
local Keys = {}
|
||||
local keys = {}
|
||||
local DisableControlAction = DisableControlAction
|
||||
local pairs = pairs
|
||||
|
||||
return setmetatable(DisableControlActions, {
|
||||
__index = Keys,
|
||||
__newindex = Keys,
|
||||
return setmetatable(disableControls, {
|
||||
__index = keys,
|
||||
__newindex = keys,
|
||||
__call = function()
|
||||
for k in pairs(Keys) do
|
||||
for k in pairs(keys) do
|
||||
DisableControlAction(0, k, true)
|
||||
end
|
||||
end
|
||||
7
init.lua
7
init.lua
@@ -2,19 +2,18 @@ if not _VERSION:find('5.4') then
|
||||
error('^1Lua 5.4 must be enabled in the resource manifest!^0', 3)
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------------------------------
|
||||
-- Module
|
||||
-----------------------------------------------------------------------------------------------
|
||||
|
||||
-- env
|
||||
local lualib = 'ox_lib'
|
||||
local file = IsDuplicityVersion() and 'server' or 'client'
|
||||
|
||||
-- micro-optimise
|
||||
local rawget = rawget
|
||||
local rawset = rawset
|
||||
local LoadResourceFile = LoadResourceFile
|
||||
local file = IsDuplicityVersion() and 'server' or 'client'
|
||||
local rawset = rawset
|
||||
local rawget = rawget
|
||||
|
||||
local function loadModule(self, module)
|
||||
local dir = ('imports/%s'):format(module)
|
||||
|
||||
Reference in New Issue
Block a user