Skip to content

Commit

Permalink
fix: allow nil arguments to debounce function
Browse files Browse the repository at this point in the history
this allows
```lua
local f = utils.debounce(function(...) print(...) end)
f(1, nil, 9, nil, nil) -- "1 nil 9 nil nil"
--- previously would've output "1" (undefined behavior)
```
  • Loading branch information
aarondill committed Mar 25, 2024
1 parent 5e813f8 commit 0a4469b
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lua/tabnine/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ local api = vim.api
local M = {}
local last_changedtick = vim.b.changedtick

---@param func fun(...: unknown): any the callback
---@param delay integer delay in milliseconds
---@return fun(...: unknown)
function M.debounce(func, delay)
local timer_id
return function(...)
if timer_id then fn.timer_stop(timer_id) end
local args = { ... }
local args = table.pack(...)
timer_id = fn.timer_start(delay, function()
func(unpack(args))
return func(unpack(args, 1, args.n))
end)
end
end
Expand Down

0 comments on commit 0a4469b

Please sign in to comment.