-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generate lookup using debouncer
- Loading branch information
1 parent
51ffe50
commit ce935ef
Showing
8 changed files
with
125 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
local M = {} | ||
|
||
function M.create_debouncer() | ||
local timers = {} | ||
local results = {} | ||
local funcs = {} -- Store the original functions | ||
|
||
return function(func, delay) | ||
if type(func) ~= "function" then | ||
error("First argument to debounce must be a function") | ||
end | ||
|
||
local key = tostring(func) | ||
funcs[key] = func -- Store the function | ||
|
||
return function(...) | ||
local args = { ... } | ||
local callback = nil | ||
|
||
-- Check if the last argument is a callback function | ||
if #args > 0 and type(args[#args]) == "function" then | ||
callback = table.remove(args) | ||
end | ||
|
||
if timers[key] then | ||
vim.fn.timer_stop(timers[key]) | ||
end | ||
|
||
timers[key] = vim.fn.timer_start(delay, function() | ||
if not funcs[key] then | ||
print("Error: Function not found for key: " .. key) | ||
return | ||
end | ||
|
||
local result | ||
local success, err = pcall(function() | ||
if #args > 0 then | ||
result = funcs[key](unpack(args)) | ||
else | ||
result = funcs[key]() | ||
end | ||
end) | ||
|
||
if not success then | ||
print("Error executing function: " .. tostring(err)) | ||
return | ||
end | ||
|
||
results[key] = result | ||
timers[key] = nil | ||
|
||
if callback then | ||
callback(result) | ||
end | ||
end) | ||
|
||
-- Return a function to get the result | ||
return function() | ||
return results[key] | ||
end | ||
end | ||
end | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters