Skip to content

New Feature Idea: Debounce expensive events by providing EventBus.get_or_dispatch(SomeEvent, include=..., exclude=...) #10

@pirate

Description

@pirate

Helpful for debouncing slow expensive events, before firing a new one you can check if any recent event is good enough and use that instead.

async def debounced_screenshot(target_id: str) -> bytes:
	"""gets the most recent screenshot within last 5 seconds or takes a new one"""

	is_fresh = lambda e: e.event_completed_at < (datetime.now() - timedelta(seconds=5))
    is_irrelevant = lambda e: e.target_id != target_id or e.event_status != 'completed'

    recent_or_new_screenshot = event_bus.get_or_dispatch(
		ScreenshotEvent(target_id=target_id, full_page=True),
		include=is_fresh,
        exclude=is_irrelevant,
    )

    # will check for any ScreenshotEvent in `event_bus.event_history` (up to history_max_length most recent events)
    # that matches the include & exclude criteria and return most recent one if any are found
    # otherwise will dispatch a new event and return that

    return (await recent_or_new_screenshot.event_result())

This should allow you to call debounced_screenshot(...) many times in a short timeframe while ratelimiting the actual pace of screenshots to 1x/5s.

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