refactor(util): unify debounce helpers into Debouncer class

This commit is contained in:
2026-04-13 21:09:07 +02:00
parent 8c3f7b8d77
commit fa01343730
3 changed files with 103 additions and 110 deletions
+44 -70
View File
@@ -1,4 +1,5 @@
local log = require("log")
local util = require("util")
local config_dir = vim.fn.stdpath("config")
@@ -82,10 +83,9 @@ local function run_build(plugin)
plugin.build(plugin)
return
elseif type(plugin.build) == "table" then
local ret = vim.system(
plugin.build --[[@as table]],
{ cwd = plugin.path }
):wait()
local ret =
vim.system(plugin.build --[[@as table]], { cwd = plugin.path })
:wait()
if ret.code ~= 0 then
log.error("Build failed for %s: %s", plugin.name, ret.stderr or "")
end
@@ -120,7 +120,8 @@ local function process_events(plugin, events)
end
for _, ev in ipairs(events) do
if ev.data.spec.name == plugin.name
if
ev.data.spec.name == plugin.name
and ev.event == "PackChanged"
and (ev.data.kind == "install" or ev.data.kind == "update")
then
@@ -136,8 +137,8 @@ end
---@type uv.uv_fs_event_t?
local watcher = nil
---@type table<string, uv.uv_timer_t>
local timers = {}
---@type ow.Util.Debouncer?
local reload = nil
---@class ow.Pack
---@field plugins ow.Pack.Plugin[]
@@ -147,12 +148,16 @@ local M = {
---@return string[]
function M.get_names()
return vim.tbl_map(function(p) return p.name end, M.plugins)
return vim.tbl_map(function(p)
return p.name
end, M.plugins)
end
---@return string[]
function M.get_paths()
return vim.tbl_map(function(p) return p.path end, M.plugins)
return vim.tbl_map(function(p)
return p.path
end, M.plugins)
end
---@param name string
@@ -196,61 +201,29 @@ function M.watch()
return
end
watcher:start(
plugins_dir,
{},
vim.schedule_wrap(function(err, filename)
if err then
log.error("Watch error: %s", err)
return
end
reload = util.debounce(function(filename)
local path = vim.fs.joinpath(plugins_dir, filename)
if not vim.uv.fs_stat(path) then
return
end
local ok, load_err = exec(path)
if ok then
log.info("Reloaded %s", filename)
else
log.error("Failed to reload %s: %s", filename, load_err)
end
end, 100)
if not filename or not filename:match("%.lua$") then
return
end
---@type uv.uv_timer_t?
local timer = timers[filename]
if not timer then
timer, err_msg, err_name = vim.uv.new_timer()
if not timer then
log.error(
"Failed to create new timer: %s (%s)", err_msg, err_name
)
return
end
timers[filename] = timer
else
timer:stop()
end
timer:start(
100,
0,
vim.schedule_wrap(function()
timer:stop()
timer:close()
timers[filename] = nil
local path = plugins_dir .. "/" .. filename
if not vim.uv.fs_stat(path) then
return
end
local ok, load_err = exec(path)
if ok then
log.info("Reloaded %s", filename)
else
log.error(
"Failed to reload %s: %s",
filename,
load_err
)
end
end)
)
end)
)
watcher:start(plugins_dir, {}, function(err, filename)
if err then
log.error("Watch error: %s", err)
return
end
if not filename or not filename:match("%.lua$") then
return
end
reload:call(filename)
end)
end
function M.unwatch()
@@ -258,10 +231,9 @@ function M.unwatch()
return
end
for key, timer in pairs(timers) do
timer:stop()
timer:close()
timers[key] = nil
if reload then
reload:cancel()
reload = nil
end
watcher:stop()
@@ -281,12 +253,14 @@ function M.setup(specs)
---@param ev ow.Pack.Event
callback = function(ev)
local name = ev.data.spec.name
if not name then return end
if not name then
return
end
if not events[name] then
events[name] = {}
end
table.insert(events[name], ev)
end
end,
}
)
vim.pack.add(vim.tbl_map(to_pack_spec, specs), {
@@ -309,7 +283,7 @@ function M.setup(specs)
if not d.ts_parser then
vim.cmd.packadd(plugin.name)
end
end
end,
})
vim.api.nvim_del_autocmd(id)