Skip to content

Commit

Permalink
perf(util): improve impl of throttle
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 26, 2024
1 parent 64fd346 commit 3695215
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
34 changes: 16 additions & 18 deletions lua/lazy/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
end

timer:start(ms, 0, function()
running = false
vim.schedule(wrapped)
timer:start(
0,
ms,
vim.schedule_wrap(function()
fn()
if pending then
pending = false
else
timer:stop()
end
end)

running = true
end
)
end
end

Expand Down
5 changes: 4 additions & 1 deletion lua/lazy/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 3695215

Please sign in to comment.