Skip to content

Frontend: rate-limit one-more-time requests when page is out of focus or hidden#3828

Open
Williangalvani wants to merge 1 commit intobluerobotics:masterfrom
Williangalvani:throttling
Open

Frontend: rate-limit one-more-time requests when page is out of focus or hidden#3828
Williangalvani wants to merge 1 commit intobluerobotics:masterfrom
Williangalvani:throttling

Conversation

@Williangalvani
Copy link
Member

@Williangalvani Williangalvani commented Mar 13, 2026

Summary by Sourcery

Adjust OneMoreTime polling behavior to respect page visibility and focus, reducing background activity while keeping data fresh when the page is active.

Enhancements:

  • Introduce page visibility and focus tracking in the frontend store to derive a page_state used across the app.
  • Scale OneMoreTime delays based on page_state to throttle retries and polling when the tab is blurred or hidden, with an option to disable this behavior per instance.
  • Notify OneMoreTime instances when the page regains focus so they can cancel long timeouts and restart immediately.
  • Initialize and update page_state via document visibility and window focus/blur events.

@sourcery-ai
Copy link

sourcery-ai bot commented Mar 13, 2026

Reviewer's Guide

Implements page focus/visibility–aware throttling for recurring OneMoreTime actions and tracks global page state in the frontend store, including opt-out controls and lifecycle cleanup for listeners.

Sequence diagram for OneMoreTime resuming on page focus

sequenceDiagram
  participant Browser as Browser
  participant Document as document_window
  participant Store as FrontendStore
  participant OMT1 as OneMoreTime_1
  participant OMT2 as OneMoreTime_2

  Note over Document,Store: Initial setup registers visibility and focus listeners

  Browser->>Document: User focuses window or tab
  Document->>Store: update() calls setPageState(focused)
  Store-->>Store: page_state = focused

  Note over OMT1,OMT2: Instances previously registered onPageResume in pageResumeListeners

  Document->>Document: notify() on focus/visibilitychange
  Document->>OMT1: onPageResume()
  OMT1->>OMT1: check isDisposed, isPaused, isRunning, timeoutId
  alt OMT1 eligible to resume
    OMT1->>OMT1: killTask()
    OMT1->>OMT1: start()
  end

  Document->>OMT2: onPageResume()
  OMT2->>OMT2: check isDisposed, isPaused, isRunning, timeoutId
  alt OMT2 eligible to resume
    OMT2->>OMT2: killTask()
    OMT2->>OMT2: start()
  end
Loading

Class diagram for page-aware OneMoreTime throttling and frontend store

classDiagram
  class OneMoreTimeOptions {
    +number delay
    +number errorDelay
    +boolean immediate
    +unknown disposeWith
    +boolean disablePageThrottle
  }

  class OneMoreTime {
    -OneMoreTimeOptions options
    -OneMoreTimeAction action
    -boolean isPaused
    -boolean isDisposed
    -boolean isRunning
    -number timeoutId
    -function onPageResume()
    +constructor(options OneMoreTimeOptions, action OneMoreTimeAction)
    -function getEffectiveDelay(baseDelay number) number
    -function watchDisposeWith() void
    -function killTask() void
    +function pause() void
    +function resume() void
    +function softStart() void
    +function start() Promise~void~
    +function [Symbol.dispose]() void
  }

  class FrontendStore {
    +boolean backend_offline
    +PageState page_state
    +string frontend_id
    +function setBackendOffline(offline boolean) void
    +function setPageState(state PageState) void
  }

  class PageState {
    <<type>>
    focused
    blurred
    hidden
  }

  OneMoreTime --> OneMoreTimeOptions : uses
  OneMoreTime --> PageState : reads via frontend.page_state
  FrontendStore --> PageState : manages
Loading

File-Level Changes

Change Details Files
Add page visibility/focus–dependent delay throttling to OneMoreTime retries, with opt-out support and resume-on-focus behavior.
  • Import the frontend store and PageState type to access global page state inside OneMoreTime.
  • Define PAGE_STATE_MULTIPLIERS mapping page states to delay factors and register global pageResumeListeners notified on visibility/focus changes.
  • Extend OneMoreTimeOptions with a disablePageThrottle flag to allow bypassing page-based throttling for specific instances.
  • Add an onPageResume handler per OneMoreTime instance that cancels any pending timeout and immediately restarts when the page resumes.
  • Introduce getEffectiveDelay helper to compute delays based on base delay and current PageState, honoring disablePageThrottle.
  • Apply getEffectiveDelay to both errorDelay sleep and main delay scheduling, and ensure listeners are removed on disposal or when the disposeWith ref is destroyed.
core/frontend/src/one-more-time.ts
Track global page focus/visibility state in the frontend Vuex store and wire it to DOM visibility/focus/blur events.
  • Define a PageState union type ('focused'
'blurred'

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@Williangalvani Williangalvani marked this pull request as ready for review March 20, 2026 15:04
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • The module-level listeners in one-more-time.ts and store/frontend.ts are never torn down, which can cause leaks or duplicated handlers in HMR/multi-mount scenarios; consider scoping them to the app lifecycle (e.g., registering on app init and removing on teardown).
  • In one-more-time.ts, window.addEventListener is called inside a typeof document !== 'undefined' guard but without checking typeof window !== 'undefined', which can break in non-browser/SSR-like environments where document and window lifecycles differ; it would be safer to guard both globals.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The module-level listeners in `one-more-time.ts` and `store/frontend.ts` are never torn down, which can cause leaks or duplicated handlers in HMR/multi-mount scenarios; consider scoping them to the app lifecycle (e.g., registering on app init and removing on teardown).
- In `one-more-time.ts`, `window.addEventListener` is called inside a `typeof document !== 'undefined'` guard but without checking `typeof window !== 'undefined'`, which can break in non-browser/SSR-like environments where `document` and `window` lifecycles differ; it would be safer to guard both globals.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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

Successfully merging this pull request may close these issues.

1 participant