perf(util): improve impl of throttle

This commit is contained in:
Folke Lemaitre 2024-06-26 14:28:22 +02:00
parent 64fd346728
commit 36952153ec
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 20 additions and 19 deletions

View File

@ -74,27 +74,25 @@ end
---@return F
function M.throttle(ms, fn)
local timer = vim.uv.new_timer()
local running = false
local first = true
local pending = false
return function(...)
local args = { ... }
local wrapped = function()
fn(unpack(args))
return function()
if timer:is_active() then
pending = true
return
end
if not running then
if first then
wrapped()
first = false
timer:start(
0,
ms,
vim.schedule_wrap(function()
fn()
if pending then
pending = false
else
timer:stop()
end
timer:start(ms, 0, function()
running = false
vim.schedule(wrapped)
end)
running = true
end
)
end
end

View File

@ -67,7 +67,10 @@ function M.create()
self.state = vim.deepcopy(default_state)
self.render = Render.new(self)
self.update = Util.throttle(Config.options.ui.throttle, self.update)
local update = self.update
self.update = Util.throttle(Config.options.ui.throttle, function()
update(self)
end)
for _, pattern in ipairs({ "LazyRender", "LazyFloatResized" }) do
self:on({ "User" }, function()