fix(util): refactor debouncer

This commit is contained in:
2026-04-19 00:40:24 +02:00
parent a4af5ce66f
commit b4721bb444
5 changed files with 156 additions and 100 deletions
+144 -81
View File
@@ -1,8 +1,8 @@
local log = require("log")
local Util = {}
local M = {}
Util.os_name = vim.uv.os_uname().sysname
M.os_name = vim.uv.os_uname().sysname
---@alias OutputStream "stdout" | "stderr" | "in_place"
@@ -26,7 +26,7 @@ Util.os_name = vim.uv.os_uname().sysname
--- Format buffer
---@param opts ow.FormatOptions
function Util.format(opts)
function M.format(opts)
opts = {
buf = opts.buf or vim.api.nvim_get_current_buf(),
cmd = opts.cmd,
@@ -241,7 +241,7 @@ end
---@param kt type
---@param vt type
---@return boolean
function Util.is_map(val, kt, vt)
function M.is_map(val, kt, vt)
if type(val) ~= "table" then
return false
end
@@ -263,7 +263,7 @@ end
---@param val any
---@param t? type
---@return boolean
function Util.is_list(val, t)
function M.is_list(val, t)
if not vim.islist(val) then
return false
end
@@ -285,115 +285,178 @@ end
---@param val? any
---@param t? type
---@return boolean
function Util.is_list_or_nil(val, t)
function M.is_list_or_nil(val, t)
if val == nil then
return true
else
return Util.is_list(val, t)
return M.is_list(val, t)
end
end
local NIL_KEY = {}
---@class ow.Util.Debouncer.Slot
---@field timer uv.uv_timer_t
---@field cb function
---@field id any
---@field args table
---@class ow.Util.Debouncer
---@field private _fn fun(id: any, ...)
---@field private _delay integer
---@field private _slots table<any, ow.Util.Debouncer.Slot>
---@field package _fn fun(...)
---@field package _delay integer
---@field package _timer uv.uv_timer_t
---@field package _gen integer
---@field package _fired_gen integer
---@field package _args? table
---@field package _cb_main fun()
---@field package _cb_uv fun()
local Debouncer = {}
Debouncer.__index = Debouncer
---@param fn fun(id: any, ...)
---@param fn fun(...)
---@param delay integer
---@return ow.Util.Debouncer
function Debouncer.new(fn, delay)
return setmetatable({ _fn = fn, _delay = delay, _slots = {} }, Debouncer)
end
---@param slot ow.Util.Debouncer.Slot
local function dispose(slot)
if not slot.timer:is_closing() then
slot.timer:stop()
slot.timer:close()
local self = setmetatable({
_fn = fn,
_delay = delay,
_timer = assert(vim.uv.new_timer()),
_gen = 0,
_fired_gen = 0,
_args = nil,
}, Debouncer)
self._cb_main = vim.schedule_wrap(function()
-- Identity check: the libuv fire may have been superseded by a
-- re-arm or a cancel between the timer firing and this scheduled
-- callback running.
if self._fired_gen ~= self._gen or self._args == nil then
return
end
local args = self._args
self._args = nil
self._fn(vim.F.unpack_len(args))
end)
self._cb_uv = function()
self._fired_gen = self._gen
self._cb_main()
end
return self
end
---@param id? any
---@param ... any
function Debouncer:call(id, ...)
local key = id == nil and NIL_KEY or id
function Debouncer:__call(...)
self._args = vim.F.pack_len(...)
self._gen = self._gen + 1
self._timer:start(self._delay, 0, self._cb_uv)
end
function Debouncer:cancel()
self._timer:stop()
self._args = nil
end
function Debouncer:flush()
if self._args == nil then
return
end
self._timer:stop()
local args = self._args
self._args = nil
self._fn(vim.F.unpack_len(args))
end
---@return boolean
function Debouncer:pending()
return self._args ~= nil
end
function Debouncer:close()
self._timer:stop()
if not self._timer:is_closing() then
self._timer:close()
end
self._args = nil
end
---@generic F: fun(...)
---@param fn F
---@param delay integer
---@return F | ow.Util.Debouncer
function M.debounce(fn, delay)
return Debouncer.new(fn, delay)
end
---@class ow.Util.KeyedDebouncer<T>
---@field package _fn fun(key: T, ...)
---@field package _delay integer
---@field package _slots table<T, ow.Util.Debouncer>
local KeyedDebouncer = {}
KeyedDebouncer.__index = KeyedDebouncer
---@generic T
---@param fn fun(key: T, ...)
---@param delay integer
---@return ow.Util.KeyedDebouncer<T>
function KeyedDebouncer.new(fn, delay)
return setmetatable({
_fn = fn,
_delay = delay,
_slots = {},
}, KeyedDebouncer)
end
---@generic T
---@param self ow.Util.KeyedDebouncer<T>
---@param key T
function KeyedDebouncer:__call(key, ...)
local slot = self._slots[key]
local args = vim.F.pack_len(...)
if not slot then
-- cb lives on the slot so restart (the else branch below) can hand
-- the same closure back to timer:start, avoiding a fresh
-- schedule_wrap allocation per call. It reads id/args from the
-- slot (not upvalues) so restarts pick up the latest args.
local timer = assert(vim.uv.new_timer())
---@type ow.Util.Debouncer.Slot
local new_slot
new_slot = {
timer = timer,
id = id,
args = args,
cb = vim.schedule_wrap(function()
-- Slot may have been cancelled between libuv firing and
-- this scheduled callback running. Identity-check before
-- firing.
if self._slots[key] ~= new_slot then
return
end
self._slots[key] = nil
dispose(new_slot)
self._fn(new_slot.id, vim.F.unpack_len(new_slot.args))
end),
}
self._slots[key] = new_slot
slot = new_slot
else
slot.id = id
slot.args = args
slot = Debouncer.new(function(...)
self._fn(key, ...)
end, self._delay)
self._slots[key] = slot
end
-- uv_timer_start on an already-active timer restarts it with the new
-- timeout, reusing the same handle (no per-call allocation).
assert(slot.timer:start(self._delay, 0, slot.cb))
slot(...)
end
---@param id? any
function Debouncer:cancel(id)
local key = id == nil and NIL_KEY or id
---@generic T
---@param self ow.Util.KeyedDebouncer<T>
---@param key T
function KeyedDebouncer:cancel(key)
local slot = self._slots[key]
if slot then
slot:close()
self._slots[key] = nil
dispose(slot)
end
end
function Debouncer:cancel_all()
---@generic T
---@param self ow.Util.KeyedDebouncer<T>
---@param key T
function KeyedDebouncer:flush(key)
local slot = self._slots[key]
if slot then
slot:flush()
end
end
---@generic T
---@param self ow.Util.KeyedDebouncer<T>
---@param key T
---@return boolean
function KeyedDebouncer:pending(key)
local slot = self._slots[key]
return slot ~= nil and slot:pending()
end
function KeyedDebouncer:close()
for _, slot in pairs(self._slots) do
dispose(slot)
slot:close()
end
self._slots = {}
end
Util.Debouncer = Debouncer
--- Creates a debounced function that delays execution of `fn` until after `delay` milliseconds have
--- elapsed since the last time it was invoked. Use `d:call(id, ...)` to debounce per-id, or
--- `d:call(...)` for a single shared slot.
---@param fn fun(id: any, ...) Function to be debounced
---@param delay integer Debounce delay in milliseconds
---@return ow.Util.Debouncer
function Util.debounce(fn, delay)
return Debouncer.new(fn, delay)
---@diagnostic disable-next-line: undefined-doc-name
---@generic T, F: fun(key: T, ...)
---@param fn F
---@param delay integer
---@return F | ow.Util.KeyedDebouncer<T>
function M.keyed_debounce(fn, delay)
return KeyedDebouncer.new(fn, delay)
end
function Util.get_hl_source(name)
function M.get_hl_source(name)
local hl = vim.api.nvim_get_hl(0, { name = name })
while hl.link do
hl = vim.api.nvim_get_hl(0, { name = hl.link })
@@ -402,4 +465,4 @@ function Util.get_hl_source(name)
return hl
end
return Util
return M