helpers: Add debounce function

This can be used to solve widget signals being emitted more than
once (e.g. widget::layout_changed signal being emitted twice when the
widget children are updated).
This commit is contained in:
elenapan 2024-07-20 18:57:19 +03:00
parent 13a1229a97
commit c4b58db757

View file

@ -461,4 +461,18 @@ function helpers.this_dir()
return str:match("(.*/)")
end
function helpers.debounce(func, timeout)
local timer = nil
return function(...)
local args = { ... }
if timer then
timer:stop()
end
timer = gears.timer.start_new(timeout, function()
func(unpack(args))
return false -- Stop the timer
end)
end
end
return helpers