Skip to content

Commit

Permalink
chore: Drop duplicate util
Browse files Browse the repository at this point in the history
  • Loading branch information
gavindsouza committed Mar 22, 2024
1 parent 4763c0d commit 2a745cc
Showing 1 changed file with 0 additions and 53 deletions.
53 changes: 0 additions & 53 deletions insights/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,56 +117,3 @@ def wrapper(*args, **kwargs):
return wrapper

return decorator


def debounce(wait):
"""Debounce decorator to be used on methods.
- This decorator will ensure that the method is called only after
`wait` seconds have passed since the last call.
- The method will be called if the arguments are different from the
last call.
- Returns the result of the last call if the method is called again
Parameters
----------
wait : int
Number of seconds to wait before calling the method again.
Returns
-------
function
The decorated function.
"""

def decorator(function):
@wraps(function)
def wrapper(*args, **kwargs):
# check if the method is called for the first time
if not hasattr(function, "last_call"):
function.last_call = threading.Event()

# check if the arguments are different from the last call
if (
function.last_call.is_set()
and function.last_args == args
and function.last_kwargs == kwargs
and hasattr(function, "last_result")
):
return function.last_result

# set the arguments and call the method
function.last_args = args
function.last_kwargs = kwargs
function.last_call.set()
try:
function.last_result = function(*args, **kwargs)
return function.last_result
finally:
# reset the event after `wait` seconds
threading.Timer(wait, function.last_call.clear).start()

return wrapper

return decorator

0 comments on commit 2a745cc

Please sign in to comment.