25 lines
542 B
Python
25 lines
542 B
Python
import asyncio
|
|
from functools import wraps
|
|
|
|
|
|
def async_debounce(wait):
|
|
def decorator(func):
|
|
task: asyncio.Task | None = None
|
|
|
|
@wraps(func)
|
|
async def debounced(*args, **kwargs):
|
|
nonlocal task
|
|
if task and not task.done():
|
|
return
|
|
|
|
async def call_func():
|
|
await asyncio.sleep(wait)
|
|
await func(*args, **kwargs)
|
|
|
|
task = asyncio.create_task(call_func())
|
|
return task
|
|
|
|
return debounced
|
|
|
|
return decorator
|