Skip to content

Commit

Permalink
some general fixes and updates after major refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
gesslar committed Nov 23, 2024
1 parent 33f155f commit 330fec7
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 35 deletions.
2 changes: 1 addition & 1 deletion mfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"package": "Glu",
"title": "Utility functions for Lua",
"version": "1.14.9",
"version": "1.15.0",
"author": "gesslar",
"icon": "liquid-glue.png",
"dependencies": "",
Expand Down
29 changes: 18 additions & 11 deletions src/scripts/dependency_queue.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,31 @@ local DependencyQueueClass = Glu.glass.register({
function self.new_dependency_queue(packages, cb)
local installed = getPackages()
local not_installed = table.n_filter(packages, function(package)
return ___.table.index_of(installed, package.name) == nil
return table.index_of(installed, package.name) == nil
end) or {}

-- We have no packages not installed, so just return as if we're done.
if #not_installed == 0 then
cecho("All dependencies are already installed.\n")
cb(true, nil)
cb(true, "All dependencies are already installed.")
return
end

local id = ___.id()
___.table.add(self, {
id = id,
local this = {
id = ___.id(),
cb = cb,
queue = self.new_queue(),
packages = not_installed,
handler_name = f "dependency_{id}_installed",
})
}
___.table.add(self, this)

for _, package in ipairs(not_installed) do
self.push(self.id, function()
cecho(f "Installing dependency `<b>{package.name}</b>`...\n")
local func = function()
cecho("Installing dependency `<b>" .. package.name .. "</b>`...\n")
installPackage(package.url)
end)
end

self.queue.push(func)
end

registerNamedEventHandler("glu", self.handler_name, "sysInstall",
Expand Down Expand Up @@ -63,8 +65,13 @@ local DependencyQueueClass = Glu.glass.register({
end

function self.start()
self.execute()
if not self.queue then
return nil, "Queue not found"
end
return self.queue.execute()
end

return self
end
end
})
8 changes: 6 additions & 2 deletions src/scripts/glu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ if not _G["Glu"] then
local parent_name = glu_class.inherit_from
local parent = into.get_object(parent_name)

if not parent or (parent.glass and parent.glass.setup) then
if not parent then
local parent_class = glu.get_glass(parent_name)
-- Recursively instantiate the parent class
return instantiate(glu, parent_class, ops, into)
instantiate(glu, parent_class, ops, into)
-- error("Parent class `" .. glu_class.inherit_from .. "` not found for `" .. glu_class.name .. "`")
end
end
Expand Down Expand Up @@ -248,6 +248,10 @@ if not _G["Glu"] then

error("Invalid type to argument " ..
argument_index .. ". Expected " .. expected_type .. ", got " .. type(value) .. " in\n" .. last)
end,
not_nil = function(value, argument_index)
local last = instance.get_last_traceback_line()
assert(value ~= nil, "value must not be nil for argument " .. argument_index .. " in\n" .. last)
end
}

Expand Down
9 changes: 5 additions & 4 deletions src/scripts/http_request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ local HttpRequestClass = Glu.glass.register({
local ob_id = response_data.id
local ob = owner.find_request(ob_id)

___.v.type(ob, "table", 0, "HTTP request not found")

local result = {}
if self.options.saveTo and not response_data.error then
result.write = { ___.fd.write_file(self.options.saveTo, response_data.data) }
Expand Down Expand Up @@ -93,13 +95,12 @@ local HttpRequestClass = Glu.glass.register({
local func_name = string.format("%sHTTP", lc)
local func = _G[func_name]

assert(func, "HTTP method " .. func_name .. " not found")
assert(type(func) == "function", "HTTP method " .. func_name .. " is not a function")
___.v.type(func, "function", 0, "HTTP method " .. func_name .. " not found")

local ok, err, result = pcall(
self.custom and
function() return func(self.options.method, self.options.url, self.options.headers) end or
function() return func(self.options.url, self.options.headers) end
function() return func(self.options.method, self.options.url, self.options.headers) end or
function() return func(self.options.url, self.options.headers) end
)

if not ok then
Expand Down
12 changes: 6 additions & 6 deletions src/scripts/queue.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local QueueClass = Glu.glass.register({
name = "queue",
class_name = "QueueClass",
call = "new_queue",
dependencies = { "table" },
setup = function(___, self)
self.queues = {}
Expand All @@ -11,22 +12,21 @@ local QueueClass = Glu.glass.register({
--- is available both through the queue object and the functions from
--- this module. The ID is in the form of a v4 UUID.
---
--- @param funcs table - A table of functions to be added to the queue
--- @param funcs table? - A table of functions to be added to the queue
--- @return table - The new queue object
---
--- @example
--- ```lua
--- local queue = queue.new(parent).new({})
--- ```
function self.new(funcs)
function self.new_queue(funcs)
___.v.type(funcs, "table", 1, true)
___.v.n_uniform(funcs, "function", 1, false)
___.v.n_uniform(funcs, "function", 1, true)

funcs = funcs or {}
local queue = ___.queue_stack(funcs, self)
local queue = Glu.get_glass("queue_stack").new({ funcs = funcs }, self)
___.table.push(self.queues, queue)

---@diagnostic disable-next-line: return-type-mismatch
return queue
end

Expand Down Expand Up @@ -58,7 +58,7 @@ local QueueClass = Glu.glass.register({
___.v.type(id, "string", 1, false)
___.v.type(f, "function", 2, false)

local q, err = self:get(id)
local q, err = self.get(id)
if not q then return nil, err end

return q.push(f)
Expand Down
6 changes: 1 addition & 5 deletions src/scripts/queue_stack.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ local QueueStackClass = Glu.glass.register({
class_name = "QueueStackClass",
dependencies = { "table" },
setup = function(___, self, opts, container)
if not opts.funcs then return end

local funcs = opts.funcs or {}

funcs = ___.table.n_cast(funcs)
local funcs = ___.table.n_cast(opts.funcs or {})
___.v.n_uniform(funcs, "function", 2, false)

self.stack = funcs
Expand Down
1 change: 0 additions & 1 deletion src/scripts/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ local TableClass = Glu.glass.register({

function self.n_uniform(t, typ)
___.v.type(t, "table", 1, false)
___.v.not_empty(t, 1, false)
___.v.indexed(t, 1, false)
___.v.type(typ, "string", 2, true)

Expand Down
7 changes: 2 additions & 5 deletions src/scripts/timer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local TimerClass = Glu.glass.register({
self.multi_timers = {}

local function perform_multi_timer_function(name)
local timer_function = mod.multi_timers[name]
local timer_function = self.multi_timers[name]
if not timer_function then
return false
end
Expand Down Expand Up @@ -48,9 +48,7 @@ local TimerClass = Glu.glass.register({
--- { func = function() echo("amazing\n") end },
--- { func = function() echo("developer\n") end },
--- }, 5)
--- ```
---
--- ```lua
--- -- After 1s, print "hi", after 3s, print "there", after 6s, print "you",
--- -- after 10s, print "amazing", and after 15s, print "developer"
--- timer.multi("Greetings", {
Expand All @@ -76,9 +74,8 @@ local TimerClass = Glu.glass.register({
-- Record the initial information
self.multi_timers[name] = { def = def }

local timer_result
local timer_id = tempTimer(def[1].delay, function()
timer_result = perform_multi_timer_function(name)
perform_multi_timer_function(name)
end)

if not timer_id then
Expand Down

0 comments on commit 330fec7

Please sign in to comment.