refactor(init): export cache and update intervals

BREAKING CHANGE: lib will store cached export functions and no longer uses the : syntax to call functions.

Allow SetInterval to return arguments to the callback function.
This commit is contained in:
Linden
2021-12-27 11:10:05 +11:00
parent 6b3af6f0d9
commit d0c5f722db
4 changed files with 65 additions and 45 deletions

View File

@@ -23,12 +23,13 @@ game 'gta5'
--[[ Resource Information ]]-- --[[ Resource Information ]]--
name 'pe-lualib' name 'pe-lualib'
author 'Linden' author 'Linden'
version '1.0.4' version '1.1.0'
repository 'https://github.com/project-error/pe-lualib' repository 'https://github.com/project-error/pe-lualib'
description 'A library of shared functions to utilise in other resources.' description 'A library of shared functions to utilise in other resources.'
[[ Manifest ]]-- [[ Manifest ]]--
dependencies { dependencies {
'/server:5104',
'/onesync', '/onesync',
} }
@@ -48,3 +49,5 @@ server_scripts {
'version.lua', 'version.lua',
'resource/**/server.lua' 'resource/**/server.lua'
} }
provide 'lualib'

View File

@@ -8,18 +8,18 @@ end
----------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------
-- env -- env
local LIBRARY = 'pe-lualib' local lualib = 'pe-lualib'
local SERVICE = IsDuplicityVersion() and 'server' or 'client' local file = IsDuplicityVersion() and 'server' or 'client'
-- micro-optimise -- micro-optimise
local rawget = rawget local rawget = rawget
local rawset = rawset local rawset = rawset
local LoadResourceFile = LoadResourceFile local LoadResourceFile = LoadResourceFile
local function loadFile(self, file) local function loadFile(self, module)
local dir = ('imports/%s'):format(file) local dir = ('imports/%s'):format(module)
local chunk = LoadResourceFile(LIBRARY, ('%s/%s.lua'):format(dir, SERVICE)) local chunk = LoadResourceFile(lualib, ('%s/%s.lua'):format(dir, file))
local shared = LoadResourceFile(LIBRARY, ('%s/shared.lua'):format(dir)) local shared = LoadResourceFile(lualib, ('%s/shared.lua'):format(dir))
if shared then if shared then
chunk = (chunk and ('%s\n%s'):format(shared, chunk)) or shared chunk = (chunk and ('%s\n%s'):format(shared, chunk)) or shared
@@ -27,12 +27,12 @@ local function loadFile(self, file)
if chunk then if chunk then
local err local err
chunk, err = load(chunk, ('@@%s.lua'):format(SERVICE), 't') chunk, err = load(chunk, ('@@pe-lualib/%s/%s.lua'):format(module, file), 't')
if err then if err then
error(('\n^1Error importing module (%s): %s^0'):format(dir, err), 3) error(('\n^1Error importing module (%s): %s^0'):format(dir, err), 3)
else else
rawset(self, file, chunk()) rawset(self, module, chunk())
return self[file] return self[module]
end end
else error(('\n^3Unable to import module (%s)^0'):format(dir), 3) end else error(('\n^3Unable to import module (%s)^0'):format(dir), 3) end
end end
@@ -48,7 +48,7 @@ end
----------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------
-- Interface -- API
----------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------
import = setmetatable({}, { import = setmetatable({}, {
@@ -57,48 +57,66 @@ import = setmetatable({}, {
__call = getImport, __call = getImport,
__newindex = function() __newindex = function()
error('Cannot add indexes to imports') error('Cannot set index on import')
end end
}) })
local lib = {} lib = setmetatable({}, {
local setmetatable = setmetatable __index = function(self, method)
local export = exports[lualib][method]
rawset(self, method, function(...)
return export(nil, ...)
end)
setmetatable(lib, { return self[method]
__index = exports[LIBRARY], end,
__newindex = function()
error('Cannot set index on lib')
end,
__tostring = function() __tostring = function()
return LIBRARY return lualib
end, end,
}) })
_ENV.lib = lib local intervals = {}
--- Dream of a world where this PR gets accepted. --- Dream of a world where this PR gets accepted.
--- ``` ---@param callback function
--- SetInterval(callback: function, timer: number) ---@param interval? number
--- ``` ---@param ... any
SetInterval = setmetatable({currentId = 0}, { function SetInterval(callback, interval, ...)
__call = function(self, callback, timer) interval = interval or 0
local id = self.currentId + 1 assert(type(interval) == 'number', ('Interval must be a number. Received %s'):format(tostring(interval)))
self.currentId = id local cbType = type(callback)
self[id] = timer or 0
CreateThread(function() if cbType == 'number' and intervals[callback] then
repeat intervals[callback] = interval or 0
local interval = self[id] return
Wait(interval)
callback(interval)
until interval == -1
self[id] = nil
end)
return id
end end
})
assert(cbType == 'function', ('Callback must be a function. Received %s'):format(tostring(cbType)))
local id
local args = {...}
Citizen.CreateThreadNow(function(ref)
id = ref
intervals[id] = interval or 0
repeat
interval = intervals[id]
Wait(interval)
callback(table.unpack(args))
until interval < 0
intervals[id] = nil
end)
return id
end
function ClearInterval(id) function ClearInterval(id)
if SetInterval[id] then assert(type(id) == 'number', ('Interval id must be a number. Received %s'):format(tostring(id)))
SetInterval[id] = -1 assert(intervals[id], ('No interval exists with id %s'):format(id))
end intervals[id] = -1
end end
-- pe-lualib -- pe-lualib

View File

@@ -1,9 +1,9 @@
-- Creates exports from values assigned to lib -- Creates exports from values assigned to lib
-- Hacky workaround for Lua Language Server support -- Hacky workaround for Lua Language Server support
--- Alias for `exports['pe-lualib']` --- Alias for `exports['pe-lualib']`
lib = setmetatable({}, { lib = setmetatable({}, {
__newindex = function(self, name, fn) __newindex = function(_, name, fn)
exports(name, fn) exports(name, fn)
end end
}) })

View File

@@ -5,10 +5,9 @@ CreateThread(function()
local version = GetResourceMetadata(resource, 'version', 0) local version = GetResourceMetadata(resource, 'version', 0)
PerformHttpRequest(('%s/master/fxmanifest.lua'):format(url:gsub('github.com', 'raw.githubusercontent.com')), function(error, response) PerformHttpRequest(('%s/master/fxmanifest.lua'):format(url:gsub('github.com', 'raw.githubusercontent.com')), function(error, response)
if error ~= 200 then if error == 200 then
local latest = response:match('%d%.%d+%.%d+') local latest = response:match('%d%.%d+%.%d+')
print(version, latest) if version < latest then
if version == latest then
local curMajor, curMinor = string.strsplit('.', version) local curMajor, curMinor = string.strsplit('.', version)
local newMajor, newMinor = string.strsplit('.', response:match('%d%.%d+%.%d+')) local newMajor, newMinor = string.strsplit('.', response:match('%d%.%d+%.%d+'))
local link = ('%s/archive/refs/tags/%s.zip'):format(url, latest, latest) local link = ('%s/archive/refs/tags/%s.zip'):format(url, latest, latest)