Skip to content
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

feat: Add reactive.delay #1814

Open
schloerke opened this issue Jan 9, 2025 · 4 comments
Open

feat: Add reactive.delay #1814

schloerke opened this issue Jan 9, 2025 · 4 comments

Comments

@schloerke
Copy link
Collaborator

Goal: I'd like to have a reactive event or calculation run after a set amount of time. I'd like for the reactive graph to not be blocked while waiting.

Related to debounce / throttle issue: #564 . It could be argued that a single use of delay is throttle and throttle would de a better job at cancelling previous jobs to deter concurrent delays at the same time.

While debounce and throttle both have cancellation policies, delay could be a third option where there is no cancellation policy... only a delay between the trigger and execution.

(Either way, this is more motivation for any of these methods to be added!)


Naive implementation:

trigger = reactive.val(False)
result = reactive.val()

# Start
@reactive.effect
def _():
	trigger.set(True)

# Update
@reactive.effect
@reactive.event(triggger)
async def _():
	await asyncio.sleep(4)
    result.set(new_value)

Extended task impl:

result = reactive.value()

# Sleep for `k` seconds
@reactive.extended_task
async def trigger(delay: int = 1) -> int:
    await asyncio.sleep(delay)
    return delay

# Start
@reactive.effect
def _():
    trigger(4)

# Update only if `trigger` has completed
@reactive.effect
async def _():
    trigger.result()

	result.set(new_value)

Goal API

@reactive.effect
@reactive.event(triggger, delay = 4)
def _():
    result.set(new_value)
@reactive.calc
@reactive.event(triggger, delay = 4)
def result():
	return new_value
@gadenbuie
Copy link
Collaborator

Could you explain a bit more about why you'd want to delay the reactive evaluation?

@schloerke
Copy link
Collaborator Author

schloerke commented Jan 9, 2025

My current use case is "on modal close, wait 1 second, update chatlas user input".

After submitting the modal close, many other reactives could be run within the 1 second wait. If the UI isn't being constructed, then the wait is unnecessary and timing is bad.

@schloerke
Copy link
Collaborator Author

For my particular use case, it could be resolved with ui.Chat() setting client data input hidden values from the JavaScript.

Since ui.Chat() is an output, this value should be exposed.

@gadenbuie
Copy link
Collaborator

Yeah, I've learned to be very suspicious of any kind of magical delays, to me it almost always indicates a missing step in the reactive graph or a missing event client-side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants