mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-16 22:46:03 +01:00
refactor(init): type-fixes and minor adjustments
This commit is contained in:
@@ -31,10 +31,10 @@ function lib.getCore()
|
||||
local resource
|
||||
|
||||
if framework == Core.Ox then
|
||||
import = ('imports/%s.lua'):format(lib.service)
|
||||
import = ('imports/%s.lua'):format(lib.context)
|
||||
resource = Core.Ox
|
||||
else
|
||||
import = ('imports/getCore/%s/%s.lua'):format(framework, lib.service)
|
||||
import = ('imports/getCore/%s/%s.lua'):format(framework, lib.context)
|
||||
resource = lib.name
|
||||
end
|
||||
|
||||
@@ -75,4 +75,5 @@ function lib.getCore()
|
||||
return result
|
||||
end
|
||||
|
||||
---@diagnostic disable-next-line: deprecated
|
||||
return lib.getCore
|
||||
|
||||
@@ -16,7 +16,7 @@ end
|
||||
|
||||
function lib.loadLocale(locale)
|
||||
if not locale then
|
||||
locale = lib.service == 'server' and lib.getServerLocale() or GetExternalKvpString('ox_lib', 'locale') or 'en'
|
||||
locale = lib.context == 'server' and lib.getServerLocale() or GetExternalKvpString('ox_lib', 'locale') or 'en'
|
||||
end
|
||||
|
||||
local resourceName = GetCurrentResourceName()
|
||||
|
||||
58
init.lua
58
init.lua
@@ -17,11 +17,11 @@ end
|
||||
-----------------------------------------------------------------------------------------------
|
||||
|
||||
local LoadResourceFile = LoadResourceFile
|
||||
local service = IsDuplicityVersion() and 'server' or 'client'
|
||||
local context = IsDuplicityVersion() and 'server' or 'client'
|
||||
|
||||
local function loadModule(self, module)
|
||||
local dir = ('imports/%s'):format(module)
|
||||
local chunk = LoadResourceFile(ox_lib, ('%s/%s.lua'):format(dir, service))
|
||||
local chunk = LoadResourceFile(ox_lib, ('%s/%s.lua'):format(dir, context))
|
||||
local shared = LoadResourceFile(ox_lib, ('%s/shared.lua'):format(dir))
|
||||
|
||||
if shared then
|
||||
@@ -29,15 +29,15 @@ local function loadModule(self, module)
|
||||
end
|
||||
|
||||
if chunk then
|
||||
local err
|
||||
chunk, err = load(chunk, ('@@ox_lib/%s/%s.lua'):format(module, service))
|
||||
if err then
|
||||
error(('\n^1Error importing module (%s): %s^0'):format(dir, err), 3)
|
||||
else
|
||||
local result = chunk() or function() end
|
||||
rawset(self, module, result)
|
||||
return self[module]
|
||||
end
|
||||
local fn, err = load(chunk, ('@@ox_lib/%s/%s.lua'):format(module, context))
|
||||
|
||||
if not fn or err then
|
||||
return error(('\n^1Error importing module (%s): %s^0'):format(dir, err), 3)
|
||||
end
|
||||
|
||||
local result = fn()
|
||||
self[module] = result
|
||||
return self[module]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -49,6 +49,7 @@ local export = exports[ox_lib]
|
||||
|
||||
local function call(self, index, ...)
|
||||
local module = rawget(self, index)
|
||||
|
||||
if not module then
|
||||
module = loadModule(self, index)
|
||||
|
||||
@@ -58,7 +59,7 @@ local function call(self, index, ...)
|
||||
end
|
||||
|
||||
if not ... then
|
||||
rawset(self, index, method)
|
||||
self[index] = method
|
||||
end
|
||||
|
||||
return method
|
||||
@@ -70,7 +71,9 @@ end
|
||||
|
||||
lib = setmetatable({
|
||||
name = ox_lib,
|
||||
service = service,
|
||||
---@deprecated
|
||||
service = context,
|
||||
context = context,
|
||||
exports = {},
|
||||
onCache = function(key, cb)
|
||||
AddEventHandler(('ox_lib:cache:%s'):format(key), cb)
|
||||
@@ -82,12 +85,16 @@ lib = setmetatable({
|
||||
|
||||
local intervals = {}
|
||||
--- Dream of a world where this PR gets accepted.
|
||||
---@param callback function
|
||||
---@param callback function | number
|
||||
---@param interval? number
|
||||
---@param ... any
|
||||
function SetInterval(callback, interval, ...)
|
||||
interval = interval or 0
|
||||
assert(type(interval) == 'number', ('Interval must be a number. Received %s'):format(json.encode(interval)))
|
||||
|
||||
if type(interval) ~= 'number' then
|
||||
return error(('Interval must be a number. Received %s'):format(json.encode(interval --[[@as unknown]])))
|
||||
end
|
||||
|
||||
local cbType = type(callback)
|
||||
|
||||
if cbType == 'number' and intervals[callback] then
|
||||
@@ -95,9 +102,11 @@ function SetInterval(callback, interval, ...)
|
||||
return
|
||||
end
|
||||
|
||||
assert(cbType == 'function', ('Callback must be a function. Received %s'):format(tostring(cbType)))
|
||||
local id
|
||||
local args = { ... }
|
||||
if cbType ~= 'function' then
|
||||
return error(('Callback must be a function. Received %s'):format(cbType))
|
||||
end
|
||||
|
||||
local args, id = { ... }
|
||||
|
||||
Citizen.CreateThreadNow(function(ref)
|
||||
id = ref
|
||||
@@ -113,9 +122,16 @@ function SetInterval(callback, interval, ...)
|
||||
return id
|
||||
end
|
||||
|
||||
---@param id number
|
||||
function ClearInterval(id)
|
||||
assert(type(id) == 'number', ('Interval id must be a number. Received %s'):format(json.encode(id)))
|
||||
assert(intervals[id], ('No interval exists with id %s'):format(id))
|
||||
if type(id) ~= 'number' then
|
||||
return error(('Interval id must be a number. Received %s'):format(json.encode(id --[[@as unknown]])))
|
||||
end
|
||||
|
||||
if not intervals[id] then
|
||||
return error(('No interval exists with id %s'):format(id))
|
||||
end
|
||||
|
||||
intervals[id] = -1
|
||||
end
|
||||
|
||||
@@ -125,7 +141,7 @@ end
|
||||
|
||||
cache = { resource = GetCurrentResourceName() }
|
||||
|
||||
if service == 'client' then
|
||||
if context == 'client' then
|
||||
setmetatable(cache, {
|
||||
__index = function(self, key)
|
||||
AddEventHandler(('ox_lib:cache:%s'):format(key), function(value)
|
||||
|
||||
Reference in New Issue
Block a user