refactor(init): type-fixes and minor adjustments

This commit is contained in:
Linden
2022-09-14 01:42:14 +10:00
parent 26538b8148
commit 32e2da2d8f
3 changed files with 41 additions and 24 deletions

View File

@@ -31,10 +31,10 @@ function lib.getCore()
local resource local resource
if framework == Core.Ox then if framework == Core.Ox then
import = ('imports/%s.lua'):format(lib.service) import = ('imports/%s.lua'):format(lib.context)
resource = Core.Ox resource = Core.Ox
else else
import = ('imports/getCore/%s/%s.lua'):format(framework, lib.service) import = ('imports/getCore/%s/%s.lua'):format(framework, lib.context)
resource = lib.name resource = lib.name
end end
@@ -75,4 +75,5 @@ function lib.getCore()
return result return result
end end
---@diagnostic disable-next-line: deprecated
return lib.getCore return lib.getCore

View File

@@ -16,7 +16,7 @@ end
function lib.loadLocale(locale) function lib.loadLocale(locale)
if not locale then 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 end
local resourceName = GetCurrentResourceName() local resourceName = GetCurrentResourceName()

View File

@@ -17,11 +17,11 @@ end
----------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------
local LoadResourceFile = LoadResourceFile local LoadResourceFile = LoadResourceFile
local service = IsDuplicityVersion() and 'server' or 'client' local context = IsDuplicityVersion() and 'server' or 'client'
local function loadModule(self, module) local function loadModule(self, module)
local dir = ('imports/%s'):format(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)) local shared = LoadResourceFile(ox_lib, ('%s/shared.lua'):format(dir))
if shared then if shared then
@@ -29,15 +29,15 @@ local function loadModule(self, module)
end end
if chunk then if chunk then
local err local fn, err = load(chunk, ('@@ox_lib/%s/%s.lua'):format(module, context))
chunk, err = load(chunk, ('@@ox_lib/%s/%s.lua'):format(module, service))
if err then if not fn or err then
error(('\n^1Error importing module (%s): %s^0'):format(dir, err), 3) return error(('\n^1Error importing module (%s): %s^0'):format(dir, err), 3)
else end
local result = chunk() or function() end
rawset(self, module, result) local result = fn()
return self[module] self[module] = result
end return self[module]
end end
end end
@@ -49,6 +49,7 @@ local export = exports[ox_lib]
local function call(self, index, ...) local function call(self, index, ...)
local module = rawget(self, index) local module = rawget(self, index)
if not module then if not module then
module = loadModule(self, index) module = loadModule(self, index)
@@ -58,7 +59,7 @@ local function call(self, index, ...)
end end
if not ... then if not ... then
rawset(self, index, method) self[index] = method
end end
return method return method
@@ -70,7 +71,9 @@ end
lib = setmetatable({ lib = setmetatable({
name = ox_lib, name = ox_lib,
service = service, ---@deprecated
service = context,
context = context,
exports = {}, exports = {},
onCache = function(key, cb) onCache = function(key, cb)
AddEventHandler(('ox_lib:cache:%s'):format(key), cb) AddEventHandler(('ox_lib:cache:%s'):format(key), cb)
@@ -82,12 +85,16 @@ lib = setmetatable({
local intervals = {} local intervals = {}
--- Dream of a world where this PR gets accepted. --- Dream of a world where this PR gets accepted.
---@param callback function ---@param callback function | number
---@param interval? number ---@param interval? number
---@param ... any ---@param ... any
function SetInterval(callback, interval, ...) function SetInterval(callback, interval, ...)
interval = interval or 0 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) local cbType = type(callback)
if cbType == 'number' and intervals[callback] then if cbType == 'number' and intervals[callback] then
@@ -95,9 +102,11 @@ function SetInterval(callback, interval, ...)
return return
end end
assert(cbType == 'function', ('Callback must be a function. Received %s'):format(tostring(cbType))) if cbType ~= 'function' then
local id return error(('Callback must be a function. Received %s'):format(cbType))
local args = { ... } end
local args, id = { ... }
Citizen.CreateThreadNow(function(ref) Citizen.CreateThreadNow(function(ref)
id = ref id = ref
@@ -113,9 +122,16 @@ function SetInterval(callback, interval, ...)
return id return id
end end
---@param id number
function ClearInterval(id) function ClearInterval(id)
assert(type(id) == 'number', ('Interval id must be a number. Received %s'):format(json.encode(id))) if type(id) ~= 'number' then
assert(intervals[id], ('No interval exists with id %s'):format(id)) 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 intervals[id] = -1
end end
@@ -125,7 +141,7 @@ end
cache = { resource = GetCurrentResourceName() } cache = { resource = GetCurrentResourceName() }
if service == 'client' then if context == 'client' then
setmetatable(cache, { setmetatable(cache, {
__index = function(self, key) __index = function(self, key)
AddEventHandler(('ox_lib:cache:%s'):format(key), function(value) AddEventHandler(('ox_lib:cache:%s'):format(key), function(value)