From 36952153ecb5b196c74e2d9a28eb0e01a9eb02fe Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Wed, 26 Jun 2024 14:28:22 +0200 Subject: [PATCH] perf(util): improve impl of throttle --- lua/lazy/util.lua | 34 ++++++++++++++++------------------ lua/lazy/view/init.lua | 5 ++++- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/lua/lazy/util.lua b/lua/lazy/util.lua index 813b4d9..09e4f54 100644 --- a/lua/lazy/util.lua +++ b/lua/lazy/util.lua @@ -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 diff --git a/lua/lazy/view/init.lua b/lua/lazy/view/init.lua index 7d349d3..5702531 100644 --- a/lua/lazy/view/init.lua +++ b/lua/lazy/view/init.lua @@ -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()