Skip to content

Commit

Permalink
fix(debounce): only await coroutines
Browse files Browse the repository at this point in the history
fixes #130
  • Loading branch information
PaulHax committed Oct 11, 2024
1 parent ca2baab commit 61e4e8d
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/nrtk_explorer/library/debounce.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
from functools import wraps
from trame.app import asynchronous


def debounce(wait, state=None):
Expand All @@ -15,17 +16,14 @@ async def wrapper(*args, **kwargs):
task.cancel()

async def debounced():
try:
await asyncio.sleep(wait)
if state:
with state:
await func(*args, **kwargs)
else:
await func(*args, **kwargs)
except asyncio.CancelledError:
pass

task = asyncio.create_task(debounced())
await asyncio.sleep(wait)
result = func(*args, **kwargs)
if asyncio.iscoroutine(result):
await result
if state:
state.flush()

task = asynchronous.create_task(debounced())

return wrapper

Expand Down

0 comments on commit 61e4e8d

Please sign in to comment.