refactor(util): reuse timer handles in Debouncer
This commit is contained in:
+57
-20
@@ -295,10 +295,16 @@ 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 _timers table<any, uv.uv_timer_t>
|
||||
---@field private _slots table<any, ow.Util.Debouncer.Slot>
|
||||
local Debouncer = {}
|
||||
Debouncer.__index = Debouncer
|
||||
|
||||
@@ -306,42 +312,73 @@ Debouncer.__index = Debouncer
|
||||
---@param delay integer
|
||||
---@return ow.Util.Debouncer
|
||||
function Debouncer.new(fn, delay)
|
||||
return setmetatable({ _fn = fn, _delay = delay, _timers = {} }, Debouncer)
|
||||
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()
|
||||
end
|
||||
end
|
||||
|
||||
---@param id? any
|
||||
---@param ... any
|
||||
function Debouncer:call(id, ...)
|
||||
local key = id == nil and NIL_KEY or id
|
||||
local slot = self._slots[key]
|
||||
local args = vim.F.pack_len(...)
|
||||
self:cancel(id)
|
||||
self._timers[key] = vim.defer_fn(function()
|
||||
self._timers[key] = nil
|
||||
self._fn(id, vim.F.unpack_len(args))
|
||||
end, self._delay)
|
||||
end
|
||||
|
||||
---@param timer uv.uv_timer_t
|
||||
local function dispose(timer)
|
||||
timer:stop()
|
||||
timer:close()
|
||||
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
|
||||
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))
|
||||
end
|
||||
|
||||
---@param id? any
|
||||
function Debouncer:cancel(id)
|
||||
local key = id == nil and NIL_KEY or id
|
||||
local timer = self._timers[key]
|
||||
if timer then
|
||||
dispose(timer)
|
||||
self._timers[key] = nil
|
||||
local slot = self._slots[key]
|
||||
if slot then
|
||||
self._slots[key] = nil
|
||||
dispose(slot)
|
||||
end
|
||||
end
|
||||
|
||||
function Debouncer:cancel_all()
|
||||
for _, t in pairs(self._timers) do
|
||||
dispose(t)
|
||||
for _, slot in pairs(self._slots) do
|
||||
dispose(slot)
|
||||
end
|
||||
self._timers = {}
|
||||
self._slots = {}
|
||||
end
|
||||
|
||||
Util.Debouncer = Debouncer
|
||||
|
||||
Reference in New Issue
Block a user