Skip to content

Commit

Permalink
refactor(init): type-fixes and minor adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Sep 13, 2022
1 parent 26538b8 commit 32e2da2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
5 changes: 3 additions & 2 deletions imports/getCore/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -75,4 +75,5 @@ function lib.getCore()
return result
end

---@diagnostic disable-next-line: deprecated
return lib.getCore
2 changes: 1 addition & 1 deletion imports/locale/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
58 changes: 37 additions & 21 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@ 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
chunk = (chunk and ('%s\n%s'):format(shared, chunk)) or shared
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

Expand All @@ -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)

Expand All @@ -58,7 +59,7 @@ local function call(self, index, ...)
end

if not ... then
rawset(self, index, method)
self[index] = method
end

return method
Expand All @@ -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)
Expand All @@ -82,22 +85,28 @@ 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
intervals[callback] = interval or 0
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
Expand All @@ -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

Expand All @@ -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)
Expand Down

0 comments on commit 32e2da2

Please sign in to comment.