Skip to content

Commit

Permalink
Merge pull request jupyter-widgets#3060 from davidbrochart/fix_doc
Browse files Browse the repository at this point in the history
Fix debouncing and throttling code
  • Loading branch information
SylvainCorlay authored Feb 5, 2021
2 parents 60c88a5 + e58d8d7 commit fba787f
Showing 1 changed file with 11 additions and 8 deletions.
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

0 comments on commit fba787f

Please sign in to comment.