Compare commits
1 commit
build/nix-
...
feat/debou
| Author | SHA1 | Date | |
|---|---|---|---|
| acc4dfc850 |
1 changed files with 20 additions and 2 deletions
|
|
@ -12,6 +12,11 @@ local watching = {}
|
||||||
---@type table<integer, true>
|
---@type table<integer, true>
|
||||||
local opened = {}
|
local opened = {}
|
||||||
|
|
||||||
|
---@type table<integer, uv_timer_t>
|
||||||
|
local debounce_timers = {}
|
||||||
|
|
||||||
|
local DEBOUNCE_MS = 500
|
||||||
|
|
||||||
---@param val string[]|fun(ctx: preview.Context): string[]
|
---@param val string[]|fun(ctx: preview.Context): string[]
|
||||||
---@param ctx preview.Context
|
---@param ctx preview.Context
|
||||||
---@return string[]
|
---@return string[]
|
||||||
|
|
@ -171,8 +176,15 @@ function M.toggle(bufnr, name, provider, ctx_builder)
|
||||||
local au_id = vim.api.nvim_create_autocmd('BufWritePost', {
|
local au_id = vim.api.nvim_create_autocmd('BufWritePost', {
|
||||||
buffer = bufnr,
|
buffer = bufnr,
|
||||||
callback = function()
|
callback = function()
|
||||||
local ctx = ctx_builder(bufnr)
|
if debounce_timers[bufnr] then
|
||||||
M.compile(bufnr, name, provider, ctx)
|
debounce_timers[bufnr]:stop()
|
||||||
|
else
|
||||||
|
debounce_timers[bufnr] = vim.uv.new_timer()
|
||||||
|
end
|
||||||
|
debounce_timers[bufnr]:start(DEBOUNCE_MS, 0, vim.schedule_wrap(function()
|
||||||
|
local ctx = ctx_builder(bufnr)
|
||||||
|
M.compile(bufnr, name, provider, ctx)
|
||||||
|
end))
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -198,6 +210,11 @@ function M.unwatch(bufnr)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
vim.api.nvim_del_autocmd(au_id)
|
vim.api.nvim_del_autocmd(au_id)
|
||||||
|
if debounce_timers[bufnr] then
|
||||||
|
debounce_timers[bufnr]:stop()
|
||||||
|
debounce_timers[bufnr]:close()
|
||||||
|
debounce_timers[bufnr] = nil
|
||||||
|
end
|
||||||
watching[bufnr] = nil
|
watching[bufnr] = nil
|
||||||
log.dbg('unwatched buffer %d', bufnr)
|
log.dbg('unwatched buffer %d', bufnr)
|
||||||
end
|
end
|
||||||
|
|
@ -254,6 +271,7 @@ M._test = {
|
||||||
active = active,
|
active = active,
|
||||||
watching = watching,
|
watching = watching,
|
||||||
opened = opened,
|
opened = opened,
|
||||||
|
debounce_timers = debounce_timers,
|
||||||
}
|
}
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue