perf: async render

This commit is contained in:
Folke Lemaitre 2024-06-28 17:44:21 +02:00
commit ab46edbd47
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
2 changed files with 26 additions and 29 deletions

View file

@ -73,36 +73,24 @@ end
---@param fn F
---@return F
function M.throttle(ms, fn)
local timer = vim.uv.new_timer()
local pending = false
---@type Async
local async
local function running()
return async and async:running()
end
local pending = false
return function()
if timer:is_active() then
if async and async:running() then
pending = true
return
end
timer:start(
0,
ms,
vim.schedule_wrap(function()
if running() then
return
end
async = require("lazy.async").new(fn)
if pending then
pending = false
else
timer:stop()
end
end)
)
---@async
async = require("lazy.async").new(function()
repeat
pending = false
fn()
async:sleep(ms)
until not pending
end)
end
end