-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# From https://gist.github.com/jcheng5/427de09573816c4ce3a8c6ec1839e7c0 | ||
import functools | ||
import time | ||
|
||
from shiny import reactive | ||
|
||
|
||
def debounce(delay_secs): | ||
def wrapper(f): | ||
when = reactive.Value(None) | ||
trigger = reactive.Value(0) | ||
|
||
@reactive.Calc | ||
def cached(): | ||
""" | ||
Just in case f isn't a reactive calc already, wrap it in one. This ensures | ||
that f() won't execute any more than it needs to. | ||
""" | ||
return f() | ||
|
||
@reactive.Effect(priority=102) | ||
def primer(): | ||
""" | ||
Whenever cached() is invalidated, set a new deadline for when to let | ||
downstream know--unless cached() invalidates again | ||
""" | ||
try: | ||
cached() | ||
except Exception: | ||
... | ||
finally: | ||
when.set(time.time() + delay_secs) | ||
|
||
@reactive.Effect(priority=101) | ||
def timer(): | ||
""" | ||
Watches changes to the deadline and triggers downstream if it's expired; if | ||
not, use invalidate_later to wait the necessary time and then try again. | ||
""" | ||
deadline = when() | ||
if deadline is None: | ||
return | ||
time_left = deadline - time.time() | ||
if time_left <= 0: | ||
# The timer expired | ||
with reactive.isolate(): | ||
when.set(None) | ||
trigger.set(trigger() + 1) | ||
else: | ||
reactive.invalidate_later(time_left) | ||
|
||
@reactive.Calc | ||
@reactive.event(trigger, ignore_none=False) | ||
@functools.wraps(f) | ||
def debounced(): | ||
return cached() | ||
|
||
return debounced | ||
|
||
return wrapper | ||
|
||
|
||
def throttle(delay_secs): | ||
def wrapper(f): | ||
last_signaled = reactive.Value(None) | ||
last_triggered = reactive.Value(None) | ||
trigger = reactive.Value(0) | ||
|
||
@reactive.Calc | ||
def cached(): | ||
return f() | ||
|
||
@reactive.Effect(priority=102) | ||
def primer(): | ||
try: | ||
cached() | ||
except Exception: | ||
... | ||
finally: | ||
last_signaled.set(time.time()) | ||
|
||
@reactive.Effect(priority=101) | ||
def timer(): | ||
if last_triggered() is not None and last_signaled() < last_triggered(): | ||
return | ||
|
||
now = time.time() | ||
if last_triggered() is None or (now - last_triggered()) >= delay_secs: | ||
last_triggered.set(now) | ||
with reactive.isolate(): | ||
trigger.set(trigger() + 1) | ||
else: | ||
reactive.invalidate_later(delay_secs - (now - last_triggered())) | ||
|
||
@reactive.Calc | ||
@reactive.event(trigger, ignore_none=False) | ||
@functools.wraps(f) | ||
def throttled(): | ||
return cached() | ||
|
||
return throttled | ||
|
||
return wrapper |