Skip to content

feat: Add reactive.delay #1814

@schloerke

Description

@schloerke

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions