From ab46edbd47fa9f380db65dbf0a7c35d18d810b19 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Fri, 28 Jun 2024 17:44:21 +0200 Subject: [PATCH] perf: async render --- lua/lazy/manage/task/init.lua | 21 +++++++++++++++------ lua/lazy/util.lua | 34 +++++++++++----------------------- 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/lua/lazy/manage/task/init.lua b/lua/lazy/manage/task/init.lua index 1470379..2b21001 100644 --- a/lua/lazy/manage/task/init.lua +++ b/lua/lazy/manage/task/init.lua @@ -49,6 +49,7 @@ function Task.new(plugin, name, task, opts) return other.name ~= name or other:running() end, plugin._.tasks or {}) table.insert(plugin._.tasks, self) + self:render() return self end @@ -119,12 +120,18 @@ function Task:log(msg, level) msg = type(msg) == "table" and table.concat(msg, "\n") or msg ---@cast msg string table.insert(self._log, { msg = msg, level = level }) - vim.api.nvim_exec_autocmds("User", { pattern = "LazyRender", modeline = false }) + self:render() if Config.headless() then self:headless() end end +function Task:render() + vim.schedule(function() + vim.api.nvim_exec_autocmds("User", { pattern = "LazyRender", modeline = false }) + end) +end + function Task:headless() if not Config.options.headless.log then return @@ -163,11 +170,13 @@ function Task:_done() if self._opts.on_done then self._opts.on_done(self) end - vim.api.nvim_exec_autocmds("User", { pattern = "LazyRender", modeline = false }) - vim.api.nvim_exec_autocmds("User", { - pattern = "LazyPlugin" .. self.name:sub(1, 1):upper() .. self.name:sub(2), - data = { plugin = self.plugin.name }, - }) + vim.schedule(function() + self:render() + vim.api.nvim_exec_autocmds("User", { + pattern = "LazyPlugin" .. self.name:sub(1, 1):upper() .. self.name:sub(2), + data = { plugin = self.plugin.name }, + }) + end) end function Task:time() diff --git a/lua/lazy/util.lua b/lua/lazy/util.lua index 9f1088d..d1ae7fd 100644 --- a/lua/lazy/util.lua +++ b/lua/lazy/util.lua @@ -73,36 +73,24 @@ end ---@param fn F ---@return F function M.throttle(ms, fn) - local timer = vim.uv.new_timer() - local pending = false - ---@type Async local async - - local function running() - return async and async:running() - end + local pending = false return function() - if timer:is_active() then + if async and async:running() then pending = true return end - timer:start( - 0, - ms, - vim.schedule_wrap(function() - if running() then - return - end - async = require("lazy.async").new(fn) - if pending then - pending = false - else - timer:stop() - end - end) - ) + ---@async + async = require("lazy.async").new(function() + repeat + pending = false + fn() + async:sleep(ms) + + until not pending + end) end end