Skip to content

Fix debouncing and throttling code #3060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions docs/source/examples/Widget Events.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,14 @@
" def __init__(self, timeout, callback):\n",
" self._timeout = timeout\n",
" self._callback = callback\n",
" self._task = asyncio.ensure_future(self._job())\n",
"\n",
" async def _job(self):\n",
" await asyncio.sleep(self._timeout)\n",
" self._callback()\n",
"\n",
" def start(self):\n",
" self._task = asyncio.ensure_future(self._job())\n",
"\n",
" def cancel(self):\n",
" self._task.cancel()\n",
"\n",
Expand All @@ -401,6 +403,7 @@
" if timer is not None:\n",
" timer.cancel()\n",
" timer = Timer(wait, call_it)\n",
" timer.start()\n",
" return debounced\n",
" return decorator"
]
Expand Down Expand Up @@ -454,22 +457,22 @@
" more than once every wait period. \"\"\"\n",
" def decorator(fn):\n",
" time_of_last_call = 0\n",
" scheduled = False\n",
" scheduled, timer = False, None\n",
" new_args, new_kwargs = None, None\n",
" def throttled(*args, **kwargs):\n",
" nonlocal new_args, new_kwargs, time_of_last_call, scheduled\n",
" nonlocal new_args, new_kwargs, time_of_last_call, scheduled, timer\n",
" def call_it():\n",
" nonlocal new_args, new_kwargs, time_of_last_call, scheduled\n",
" nonlocal new_args, new_kwargs, time_of_last_call, scheduled, timer\n",
" time_of_last_call = time()\n",
" fn(*new_args, **new_kwargs)\n",
" scheduled = False\n",
" time_since_last_call = time() - time_of_last_call\n",
" new_args = args\n",
" new_kwargs = kwargs\n",
" new_args, new_kwargs = args, kwargs\n",
" if not scheduled:\n",
" new_wait = max(0, wait - time_since_last_call)\n",
" Timer(new_wait, call_it)\n",
" scheduled = True\n",
" new_wait = max(0, wait - time_since_last_call)\n",
" timer = Timer(new_wait, call_it)\n",
" timer.start()\n",
" return throttled\n",
" return decorator"
]
Expand Down