refactor: refactored ui code

This commit is contained in:
Folke Lemaitre 2022-12-23 10:18:19 +01:00
parent cd023dc709
commit fde5feea6d
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
4 changed files with 238 additions and 184 deletions

View file

@ -45,23 +45,29 @@ function M.write_file(file, contents)
fd:close()
end
---@generic F: fun()
---@param ms number
---@param fn fun()
---@param fn F
---@return F
function M.throttle(ms, fn)
local timer = vim.loop.new_timer()
local running = false
local first = true
return function()
return function(...)
local args = { ... }
local wrapped = function()
fn(unpack(args))
end
if not running then
if first then
fn()
wrapped()
first = false
end
timer:start(ms, 0, function()
running = false
vim.schedule(fn)
vim.schedule(wrapped)
end)
running = true