Skip to content

Commit

Permalink
Add event to enable UI updates + cleanup + fixes (#162)
Browse files Browse the repository at this point in the history
* Add update spinner event to listen to with UI and clean up timers to
create only one
  • Loading branch information
V1OL3TF0X authored Nov 4, 2024
1 parent 268f466 commit 3de4f35
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
63 changes: 36 additions & 27 deletions lua/package-info/ui/generic/loading-status.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ local M = {
index = 1,
is_running = false,
notification = nil,
timer = nil,
},
}

-- nvim-notify support
local nvim_notify = pcall(require, "notify")
local title = "package-info.nvim"
local constants = require("package-info.utils.constants")

--- Spawn a new loading instance
-- @param log: string - message to display in the loading status
Expand All @@ -47,7 +49,12 @@ M.new = function(message)

table.insert(M.queue, instance)

M.update_spinner(message)
if not M.state.timer then
M.state.timer = vim.loop.new_timer()
M.state.timer:start(60, 60, function()
M.update_spinner(message)
end)
end

return instance.id
end
Expand Down Expand Up @@ -109,7 +116,7 @@ end
M.update_spinner = function(message)
M.state.current_spinner = SPINNERS[M.state.index]

M.state.index = (M.state.index + 1) % #SPINNERS
M.state.index = M.state.index % #SPINNERS + 1

if nvim_notify and M.state.notification then
local new_notif = vim.notify(message, vim.log.levels.INFO, {
Expand All @@ -121,41 +128,43 @@ M.update_spinner = function(message)
M.state.notification = new_notif
end

vim.fn.timer_start(60, function()
M.update_spinner()
-- this can be used to post updates (ex. refresh the statusline)
vim.schedule(function()
vim.api.nvim_exec_autocmds("User", {
group = constants.AUTOGROUP,
pattern = constants.LOAD_EVENT,
})
end)
end

--- Get the first ready instance message if there are instances
-- @return string
M.get = function()
local active_instance = nil

for _, instance in pairs(M.queue) do
if not active_instance and instance.is_ready then
active_instance = instance
if instance.is_ready then
if M.state.is_running then
return instance.message
end
M.state.is_running = true
return instance.message
end
end

if not active_instance then
-- FIXME: this is killing all timers, so if a user has any timers, it will interrupt them
-- like lsp status
-- vim.fn.timer_stopall()

M.state.is_running = false
M.state.current_spinner = ""
M.state.index = 1

return ""
end

if active_instance and not M.state.is_running then
M.state.is_running = true

M.update_spinner(active_instance.message)
M.state.is_running = false
M.state.current_spinner = ""
M.state.index = 1
if M.state.timer then
M.state.timer:stop()
M.state.timer:close()
M.state.timer = nil
-- ensure this gets called *after* last chedule from update_spinner
vim.schedule(function()
vim.api.nvim_exec_autocmds("User", {
group = constants.AUTOGROUP,
pattern = constants.LOAD_EVENT,
})
end)
end

return active_instance.message
return ""
end

return M
1 change: 1 addition & 0 deletions lua/package-info/utils/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ M.COMMANDS = {
}

M.AUTOGROUP = "PackageInfoAutogroup"
M.LOAD_EVENT = "PackageInfoStatusUpdate"

return M

0 comments on commit 3de4f35

Please sign in to comment.