mirror of
https://github.com/aykhans/AzSuicideDataVisualization.git
synced 2025-04-22 10:28:02 +00:00
35 lines
694 B
Python
35 lines
694 B
Python
import asyncio
|
|
|
|
|
|
class Timer:
|
|
def __init__(self, timeout, callback):
|
|
self._timeout = timeout
|
|
self._callback = callback
|
|
self._task = asyncio.ensure_future(self._job())
|
|
|
|
async def _job(self):
|
|
await asyncio.sleep(self._timeout)
|
|
self._callback()
|
|
|
|
def cancel(self):
|
|
self._task.cancel()
|
|
|
|
|
|
def debounce(wait):
|
|
def decorator(fn):
|
|
timer = None
|
|
|
|
def debounced(*args, **kwargs):
|
|
nonlocal timer
|
|
|
|
def call_it():
|
|
fn(*args, **kwargs)
|
|
|
|
if timer is not None:
|
|
timer.cancel()
|
|
timer = Timer(wait, call_it)
|
|
|
|
return debounced
|
|
|
|
return decorator
|