You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
asyncdefdebounced_screenshot(target_id: str) ->bytes:
"""gets the most recent screenshot within last 5 seconds or takes a new one"""is_fresh=lambdae: e.event_completed_at< (datetime.now() -timedelta(seconds=5))
is_irrelevant=lambdae: e.target_id!=target_idore.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 thatreturn (awaitrecent_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.