Skip to content

Commit

Permalink
fix(ui): running tasks are now less twitchy
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Oct 17, 2023
1 parent c7298a1 commit 7613ab2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
25 changes: 20 additions & 5 deletions lua/lazy/manage/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ local Util = require("lazy.util")
---@field concurrency? number

---@alias PipelineStep {task:string, opts?:TaskOptions}
---@alias LazyRunnerTask {co:thread, status: {task?:LazyTask, waiting?:boolean}}
---@alias LazyRunnerTask {co:thread, status: {task?:LazyTask, waiting?:boolean}, plugin: LazyPlugin}

---@class Runner
---@field _plugins LazyPlugin[]
Expand All @@ -29,6 +29,9 @@ function Runner.new(opts)
else
self._plugins = plugins or Config.plugins
end
table.sort(self._plugins, function(a, b)
return a.name < b.name
end)
self._running = {}
self._on_done = {}

Expand All @@ -54,12 +57,19 @@ function Runner:_resume(entry)
end

function Runner:resume(waiting)
if waiting then
for _, entry in ipairs(self._running) do
if entry.status then
if entry.status.waiting then
entry.status.waiting = false
entry.plugin._.working = true
end
end
end
end
local running = 0
for _, entry in ipairs(self._running) do
if entry.status then
if waiting and entry.status.waiting then
entry.status.waiting = false
end
if not entry.status.waiting and self:_resume(entry) then
running = running + 1
if self._opts.concurrency and running >= self._opts.concurrency then
Expand All @@ -76,7 +86,7 @@ function Runner:start()
local co = coroutine.create(self.run_pipeline)
local ok, err = coroutine.resume(co, self, plugin)
if ok then
table.insert(self._running, { co = co, status = {} })
table.insert(self._running, { co = co, status = {}, plugin = plugin })
else
Util.error("Could not start tasks for " .. plugin.name .. "\n" .. err)
end
Expand All @@ -99,21 +109,26 @@ end
---@async
---@param plugin LazyPlugin
function Runner:run_pipeline(plugin)
plugin._.working = true
coroutine.yield()
for _, step in ipairs(self._pipeline) do
if step.task == "wait" then
plugin._.working = false
coroutine.yield({ waiting = true })
plugin._.working = true
else
local task = self:queue(plugin, step.task, step.opts)
if task then
coroutine.yield({ task = task })
assert(task:is_done())
if task.error then
plugin._.working = false
return
end
end
end
end
plugin._.working = false
end

---@param plugin LazyPlugin
Expand Down
1 change: 1 addition & 0 deletions lua/lazy/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
---@field loaded? {[string]:string}|{time:number}
---@field installed? boolean
---@field tasks? LazyTask[]
---@field working? boolean
---@field dirty? boolean
---@field updated? {from:string, to:string}
---@field is_local? boolean
Expand Down
3 changes: 3 additions & 0 deletions lua/lazy/view/sections.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ return {
},
{
filter = function(plugin)
if plugin._.working then
return true
end
return has_task(plugin, function(task)
return task:is_running()
end)
Expand Down

0 comments on commit 7613ab2

Please sign in to comment.