perf: async render

This commit is contained in:
Folke Lemaitre 2024-06-28 17:44:21 +02:00
parent a36ebd2a75
commit ab46edbd47
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 26 additions and 29 deletions

View File

@ -49,6 +49,7 @@ function Task.new(plugin, name, task, opts)
return other.name ~= name or other:running() return other.name ~= name or other:running()
end, plugin._.tasks or {}) end, plugin._.tasks or {})
table.insert(plugin._.tasks, self) table.insert(plugin._.tasks, self)
self:render()
return self return self
end end
@ -119,12 +120,18 @@ function Task:log(msg, level)
msg = type(msg) == "table" and table.concat(msg, "\n") or msg msg = type(msg) == "table" and table.concat(msg, "\n") or msg
---@cast msg string ---@cast msg string
table.insert(self._log, { msg = msg, level = level }) table.insert(self._log, { msg = msg, level = level })
vim.api.nvim_exec_autocmds("User", { pattern = "LazyRender", modeline = false }) self:render()
if Config.headless() then if Config.headless() then
self:headless() self:headless()
end end
end end
function Task:render()
vim.schedule(function()
vim.api.nvim_exec_autocmds("User", { pattern = "LazyRender", modeline = false })
end)
end
function Task:headless() function Task:headless()
if not Config.options.headless.log then if not Config.options.headless.log then
return return
@ -163,11 +170,13 @@ function Task:_done()
if self._opts.on_done then if self._opts.on_done then
self._opts.on_done(self) self._opts.on_done(self)
end end
vim.api.nvim_exec_autocmds("User", { pattern = "LazyRender", modeline = false }) vim.schedule(function()
self:render()
vim.api.nvim_exec_autocmds("User", { vim.api.nvim_exec_autocmds("User", {
pattern = "LazyPlugin" .. self.name:sub(1, 1):upper() .. self.name:sub(2), pattern = "LazyPlugin" .. self.name:sub(1, 1):upper() .. self.name:sub(2),
data = { plugin = self.plugin.name }, data = { plugin = self.plugin.name },
}) })
end)
end end
function Task:time() function Task:time()

View File

@ -73,36 +73,24 @@ end
---@param fn F ---@param fn F
---@return F ---@return F
function M.throttle(ms, fn) function M.throttle(ms, fn)
local timer = vim.uv.new_timer()
local pending = false
---@type Async ---@type Async
local async local async
local pending = false
local function running()
return async and async:running()
end
return function() return function()
if timer:is_active() then if async and async:running() then
pending = true pending = true
return return
end end
timer:start( ---@async
0, async = require("lazy.async").new(function()
ms, repeat
vim.schedule_wrap(function()
if running() then
return
end
async = require("lazy.async").new(fn)
if pending then
pending = false pending = false
else fn()
timer:stop() async:sleep(ms)
end
until not pending
end) end)
)
end end
end end