Skip to content

Conversation

@matallui
Copy link
Contributor

@matallui matallui commented Nov 11, 2025

Description

This PR implements automatic eligibility refresh for the Predict feature when the app comes to foreground. Previously, eligibility was only checked on initial load or manual refresh. Now, when a user backgrounds the app and then returns to it, the eligibility status is automatically refreshed.

What is the reason for the change?
Users may change their network configuration (e.g., switching between different network connections) while the app is backgrounded. When they return to the app, the Predict feature should automatically check eligibility again to reflect any potential changes in their connection status.

What is the improvement/solution?

  • Added an AppState listener to the usePredictEligibility hook that detects when the app transitions from background/inactive to active state
  • Implemented 1-minute debouncing to prevent excessive API calls if the user switches in and out of the app multiple times
  • Manual refresh functionality remains unchanged and bypasses the debounce
  • Added comprehensive error handling and logging

Changelog

CHANGELOG entry: null

Related issues

Fixes: N/A

Manual testing steps

Feature: Auto-refresh Predict eligibility on app focus

  Scenario: user returns to app and eligibility is refreshed
    Given user has opened the Predict feature
    And user is eligible to use Predict

    When user backgrounds the app (switches to another app)
    And user returns to the app
    Then eligibility is automatically refreshed
    And user sees updated eligibility status if it changed

  Scenario: user rapidly switches in and out of app
    Given user has opened the Predict feature
    And eligibility was just refreshed

    When user backgrounds the app
    And user returns to the app within 1 minute
    Then eligibility refresh is skipped due to debouncing
    And user still has access to Predict feature

  Scenario: user returns after debounce period
    Given user has opened the Predict feature
    And eligibility was refreshed more than 1 minute ago

    When user backgrounds the app
    And user returns to the app
    Then eligibility is automatically refreshed

Screenshots/Recordings

N/A - This is an internal behavior change with no visual UI impact.

Pre-merge author checklist

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

Note

Introduce a singleton-managed AppState listener to auto-refresh Predict eligibility on app foreground with 1-minute debounce, logging, and race-condition prevention, plus comprehensive tests.

  • Predict Hook (usePredictEligibility.ts):
    • Singleton manager (EligibilityRefreshManager):
      • Manages a single AppState change listener across hook instances.
      • Refreshes Engine.context.PredictController.refreshEligibility() on transition to active.
      • Debounces auto-refreshes by 60s; manual refreshEligibility bypasses debounce.
      • Prevents concurrent calls by reusing in-flight promise; tracks last refresh time.
      • Registers/unregisters per hook mount/unmount; adds detailed DevLogger logs and error handling.
  • Tests (usePredictEligibility.test.ts):
    • Cover eligibility state selection, singleton registration/unregistration, app focus auto-refresh, debounce behavior, manual refresh bypass, error logging/continuation, and concurrency/race-condition scenarios.

Written by Cursor Bugbot for commit 8c94340. This will update automatically on new commits. Configure here.

@github-actions
Copy link
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@metamaskbot metamaskbot added the team-predict Predict team label Nov 11, 2025
@matallui matallui requested review from caieu and kevinbluer November 11, 2025 19:58
Copy link
Contributor

@caieu caieu left a comment

Choose a reason for hiding this comment

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

LGTM

@matallui matallui enabled auto-merge November 11, 2025 20:47
@sonarqubecloud
Copy link

@matallui matallui added this pull request to the merge queue Nov 11, 2025
Merged via the queue into main with commit fb910db Nov 11, 2025
133 of 134 checks passed
@matallui matallui deleted the predict/refresh-eligibility-on-app-focus branch November 11, 2025 21:25
@github-actions github-actions bot locked and limited conversation to collaborators Nov 11, 2025
@metamaskbot metamaskbot added the release-7.60.0 Issue or pull request that will be included in release 7.60.0 label Nov 11, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-7.60.0 Issue or pull request that will be included in release 7.60.0 size-L team-predict Predict team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants