fix(util): throttle now autonatically schedules when in fast event

This commit is contained in:
Folke Lemaitre 2024-12-31 06:00:48 +01:00
parent 98df370703
commit 98403313c7
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040

View file

@ -242,19 +242,24 @@ end
---@return T
function M.throttle(fn, opts)
local timer, trailing, ms = assert(uv.new_timer()), false, opts and opts.ms or 20
local running = false
local function run()
running = true
if vim.in_fast_event() then
return vim.schedule(run)
end
fn()
running = false
end
return function()
if timer:is_active() then
if running or timer:is_active() then
trailing = true
return
end
trailing = false
if vim.in_fast_event() then
vim.schedule(fn)
else
fn()
end
run()
timer:start(ms, 0, function()
return trailing and vim.schedule(fn)
return trailing and run()
end)
end
end