refactor: consolidate debounce into utils/debounce.py

Move method-specific debounce from functions.py to debounce.py,
keeping both debounce (method) and sync_debounce (general) together.
This commit is contained in:
sankalp committed 2026-07-12 19:04:33 +05:45
1 parent 30c02f207d
commit 15e64df428
3 files changed
+959 -8

No files matched your search

+27
View File
@@ -1,9 +1,36 @@
import functools
import typing as t
from collections.abc import Callable
from fabric.utils import GLib
def debounce(ms: int):
"""Debounce a class method using GLib timers.
Stores timer state on the instance (`self`), so this only works
as a method decorator.
"""
def decorator(func: Callable):
timer_id_attr = f"_debounce_timer_{func.__name__}"
def wrapper(self, *args, **kwargs):
if existing_timer := getattr(self, timer_id_attr, None):
GLib.source_remove(existing_timer)
def timeout_cb():
setattr(self, timer_id_attr, 0)
func(self, *args, **kwargs)
return False
setattr(self, timer_id_attr, GLib.timeout_add(ms, timeout_cb))
return wrapper
return decorator
def sync_debounce(
delay: int, min_n_times: int = 0, immediate: bool = False
) -> t.Callable[..., t.Any]: