mirror of
https://github.com/folke/snacks.nvim
synced 2025-08-04 02:38:46 +00:00
fix(util): throttle now autonatically schedules when in fast event
This commit is contained in:
parent
98df370703
commit
98403313c7
1 changed files with 12 additions and 7 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue