feat(watcher): debounce FS watchers

This commit is contained in:
Alexander Courtis
2022-06-28 11:18:22 +10:00
parent 7a795d78fa
commit e401a4c957
6 changed files with 74 additions and 28 deletions

View File

@@ -3,7 +3,9 @@ local has_notify, notify = pcall(require, "notify")
local a = vim.api
local uv = vim.loop
local M = {}
local M = {
debouncers = {},
}
M.is_windows = vim.fn.has "win32" == 1 or vim.fn.has "win32unix" == 1
@@ -328,4 +330,25 @@ function M.key_by(tbl, key)
return keyed
end
---Execute callback timeout ms after the lastest invocation with context. Waiting invocations for that context will be discarded. Caller should this ensure that callback performs the same or functionally equivalent actions.
---@param context string identifies the callback to debounce
---@param timeout number ms to wait
---@param callback function to execute on completion
function M.debounce(context, timeout, callback)
if M.debouncers[context] then
pcall(uv.close, M.debouncers[context])
end
M.debouncers[context] = uv.new_timer()
M.debouncers[context]:start(
timeout,
0,
vim.schedule_wrap(function()
M.debouncers[context]:close()
M.debouncers[context] = nil
callback()
end)
)
end
return M