|
379 | 379 | " def __init__(self, timeout, callback):\n",
|
380 | 380 | " self._timeout = timeout\n",
|
381 | 381 | " self._callback = callback\n",
|
382 |
| - " self._task = asyncio.ensure_future(self._job())\n", |
383 | 382 | "\n",
|
384 | 383 | " async def _job(self):\n",
|
385 | 384 | " await asyncio.sleep(self._timeout)\n",
|
386 | 385 | " self._callback()\n",
|
387 | 386 | "\n",
|
| 387 | + " def start(self):\n", |
| 388 | + " self._task = asyncio.ensure_future(self._job())\n", |
| 389 | + "\n", |
388 | 390 | " def cancel(self):\n",
|
389 | 391 | " self._task.cancel()\n",
|
390 | 392 | "\n",
|
|
401 | 403 | " if timer is not None:\n",
|
402 | 404 | " timer.cancel()\n",
|
403 | 405 | " timer = Timer(wait, call_it)\n",
|
| 406 | + " timer.start()\n", |
404 | 407 | " return debounced\n",
|
405 | 408 | " return decorator"
|
406 | 409 | ]
|
|
454 | 457 | " more than once every wait period. \"\"\"\n",
|
455 | 458 | " def decorator(fn):\n",
|
456 | 459 | " time_of_last_call = 0\n",
|
457 |
| - " scheduled = False\n", |
| 460 | + " scheduled, timer = False, None\n", |
458 | 461 | " new_args, new_kwargs = None, None\n",
|
459 | 462 | " def throttled(*args, **kwargs):\n",
|
460 |
| - " nonlocal new_args, new_kwargs, time_of_last_call, scheduled\n", |
| 463 | + " nonlocal new_args, new_kwargs, time_of_last_call, scheduled, timer\n", |
461 | 464 | " def call_it():\n",
|
462 |
| - " nonlocal new_args, new_kwargs, time_of_last_call, scheduled\n", |
| 465 | + " nonlocal new_args, new_kwargs, time_of_last_call, scheduled, timer\n", |
463 | 466 | " time_of_last_call = time()\n",
|
464 | 467 | " fn(*new_args, **new_kwargs)\n",
|
465 | 468 | " scheduled = False\n",
|
466 | 469 | " time_since_last_call = time() - time_of_last_call\n",
|
467 |
| - " new_args = args\n", |
468 |
| - " new_kwargs = kwargs\n", |
| 470 | + " new_args, new_kwargs = args, kwargs\n", |
469 | 471 | " if not scheduled:\n",
|
470 |
| - " new_wait = max(0, wait - time_since_last_call)\n", |
471 |
| - " Timer(new_wait, call_it)\n", |
472 | 472 | " scheduled = True\n",
|
| 473 | + " new_wait = max(0, wait - time_since_last_call)\n", |
| 474 | + " timer = Timer(new_wait, call_it)\n", |
| 475 | + " timer.start()\n", |
473 | 476 | " return throttled\n",
|
474 | 477 | " return decorator"
|
475 | 478 | ]
|
|
0 commit comments