Skip to content

Commit ab46edb

Browse files
committed
perf: async render
1 parent a36ebd2 commit ab46edb

File tree

2 files changed

+26
-29
lines changed

2 files changed

+26
-29
lines changed

lua/lazy/manage/task/init.lua

+15-6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ function Task.new(plugin, name, task, opts)
4949
return other.name ~= name or other:running()
5050
end, plugin._.tasks or {})
5151
table.insert(plugin._.tasks, self)
52+
self:render()
5253
return self
5354
end
5455

@@ -119,12 +120,18 @@ function Task:log(msg, level)
119120
msg = type(msg) == "table" and table.concat(msg, "\n") or msg
120121
---@cast msg string
121122
table.insert(self._log, { msg = msg, level = level })
122-
vim.api.nvim_exec_autocmds("User", { pattern = "LazyRender", modeline = false })
123+
self:render()
123124
if Config.headless() then
124125
self:headless()
125126
end
126127
end
127128

129+
function Task:render()
130+
vim.schedule(function()
131+
vim.api.nvim_exec_autocmds("User", { pattern = "LazyRender", modeline = false })
132+
end)
133+
end
134+
128135
function Task:headless()
129136
if not Config.options.headless.log then
130137
return
@@ -163,11 +170,13 @@ function Task:_done()
163170
if self._opts.on_done then
164171
self._opts.on_done(self)
165172
end
166-
vim.api.nvim_exec_autocmds("User", { pattern = "LazyRender", modeline = false })
167-
vim.api.nvim_exec_autocmds("User", {
168-
pattern = "LazyPlugin" .. self.name:sub(1, 1):upper() .. self.name:sub(2),
169-
data = { plugin = self.plugin.name },
170-
})
173+
vim.schedule(function()
174+
self:render()
175+
vim.api.nvim_exec_autocmds("User", {
176+
pattern = "LazyPlugin" .. self.name:sub(1, 1):upper() .. self.name:sub(2),
177+
data = { plugin = self.plugin.name },
178+
})
179+
end)
171180
end
172181

173182
function Task:time()

lua/lazy/util.lua

+11-23
Original file line numberDiff line numberDiff line change
@@ -73,36 +73,24 @@ end
7373
---@param fn F
7474
---@return F
7575
function M.throttle(ms, fn)
76-
local timer = vim.uv.new_timer()
77-
local pending = false
78-
7976
---@type Async
8077
local async
81-
82-
local function running()
83-
return async and async:running()
84-
end
78+
local pending = false
8579

8680
return function()
87-
if timer:is_active() then
81+
if async and async:running() then
8882
pending = true
8983
return
9084
end
91-
timer:start(
92-
0,
93-
ms,
94-
vim.schedule_wrap(function()
95-
if running() then
96-
return
97-
end
98-
async = require("lazy.async").new(fn)
99-
if pending then
100-
pending = false
101-
else
102-
timer:stop()
103-
end
104-
end)
105-
)
85+
---@async
86+
async = require("lazy.async").new(function()
87+
repeat
88+
pending = false
89+
fn()
90+
async:sleep(ms)
91+
92+
until not pending
93+
end)
10694
end
10795
end
10896

0 commit comments

Comments
 (0)