mirror of https://github.com/folke/lazy.nvim.git
perf(util): improve impl of throttle
This commit is contained in:
parent
64fd346728
commit
36952153ec
|
@ -74,27 +74,25 @@ end
|
||||||
---@return F
|
---@return F
|
||||||
function M.throttle(ms, fn)
|
function M.throttle(ms, fn)
|
||||||
local timer = vim.uv.new_timer()
|
local timer = vim.uv.new_timer()
|
||||||
local running = false
|
local pending = false
|
||||||
local first = true
|
|
||||||
|
|
||||||
return function(...)
|
return function()
|
||||||
local args = { ... }
|
if timer:is_active() then
|
||||||
local wrapped = function()
|
pending = true
|
||||||
fn(unpack(args))
|
return
|
||||||
end
|
end
|
||||||
if not running then
|
timer:start(
|
||||||
if first then
|
0,
|
||||||
wrapped()
|
ms,
|
||||||
first = false
|
vim.schedule_wrap(function()
|
||||||
end
|
fn()
|
||||||
|
if pending then
|
||||||
timer:start(ms, 0, function()
|
pending = false
|
||||||
running = false
|
else
|
||||||
vim.schedule(wrapped)
|
timer:stop()
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|
)
|
||||||
running = true
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -67,7 +67,10 @@ function M.create()
|
||||||
self.state = vim.deepcopy(default_state)
|
self.state = vim.deepcopy(default_state)
|
||||||
|
|
||||||
self.render = Render.new(self)
|
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
|
for _, pattern in ipairs({ "LazyRender", "LazyFloatResized" }) do
|
||||||
self:on({ "User" }, function()
|
self:on({ "User" }, function()
|
||||||
|
|
Loading…
Reference in New Issue