Skip to content

[pull] main from MetaMask:main#585

Merged
pull[bot] merged 1 commit into
Reality2byte:mainfrom
MetaMask:main
Mar 9, 2026
Merged

[pull] main from MetaMask:main#585
pull[bot] merged 1 commit into
Reality2byte:mainfrom
MetaMask:main

Conversation

@pull
Copy link
Copy Markdown

@pull pull Bot commented Mar 9, 2026

See Commits and Changes for more details.


Created by pull[bot] (v2.0.0-alpha.4)

Can you help keep this open source service alive? 💖 Please sponsor : )

## **Description**

Replaces per-section \`PerpsConnectionProvider\` connect/disconnect
calls with a single top-level \`PerpsAlwaysOnProvider\` mounted at the
wallet root.

**Problem**: Multiple \`PerpsConnectionProvider\` instances (Homepage,
PerpsTabView, ActivityView, TrendingView, ExploreSearchScreen,
UrlAutocomplete) each called \`connect()\`/\`disconnect()\`, creating
reference-count races in \`PerpsConnectionManager\`. This caused
intermittent bugs — positions not showing, 24h values missing — after
long app backgrounding sessions.

**Solution**: \`PerpsAlwaysOnProvider\` is the sole caller of
\`PerpsConnectionManager.connect()\` and \`disconnect()\`.
\`PerpsConnectionProvider\` is now a pure React context source — it
polls singleton state and exposes it via \`PerpsConnectionContext\`, but
never calls connect/disconnect itself.

Key changes:
- New \`PerpsAlwaysOnProvider\` — mounts once **inside**
\`ErrorBoundary\` at \`Wallet/index.tsx\`, owns \`AppState\` listener,
sole connect/disconnect owner
- \`PerpsConnectionProvider\` stripped of all lifecycle management:
removed \`manageLifecycle\` prop, \`isVisible\` prop, and the
\`usePerpsConnectionLifecycle\` hook call
- Deleted \`usePerpsConnectionLifecycle\` hook (its responsibilities
moved to \`PerpsAlwaysOnProvider\`)
- All section-level \`PerpsConnectionProvider\` instances kept as
context sources (required by \`useContext(PerpsConnectionContext)\`
consumers in \`sections.config.tsx\` et al.)
- Restored \`PerpsConnectionProvider\` (context-only) in
\`ExploreSearchScreen\`, \`UrlAutocomplete\`, and \`TrendingView\`
\`SectionWrapper\` — these render \`useSectionData\` which reads
\`PerpsConnectionContext\`
- Removed visibility callback plumbing (\`onVisibilityChange\`,
\`perpsVisibilityCallback\`) from \`WalletTokensTabView\` and
\`PerpsTabViewWithProvider\`
- Updated architecture docs

## **Changelog**

CHANGELOG entry: null

## **Related issues**

Fixes:

## **Manual testing steps**

\`\`\`gherkin
Feature: Perps always-on connection

  Scenario: user backgrounds and foregrounds the app
    Given perps feature flag is enabled
    And user is on the Wallet screen

    When user opens the Perps tab
    Then positions and market data load correctly

    When user backgrounds the app for 30+ seconds
    And user foregrounds the app
    Then Perps data reconnects and loads correctly

  Scenario: user switches between tabs
    Given perps feature flag is enabled

    When user navigates between Wallet / Activity / Trending tabs
    Then perps data remains connected without errors
    And connectionRefCount stays at 1

  Scenario: user switches accounts
    Given perps feature flag is enabled

    When user switches to a different account
    Then Perps data refreshes for the new account
\`\`\`

## **Screenshots/Recordings**

### **Before**

Multiple providers each managing lifecycle → reference-count races on
background/foreground.

### **After**

Single \`PerpsAlwaysOnProvider\` owns the lifecycle —
\`connectionRefCount\` stays exactly 1. \`PerpsConnectionProvider\` is a
context-only provider with no lifecycle responsibilities.

## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I've included tests if applicable
- [ ] I've documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I've applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **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.

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Medium Risk**
> Changes Perps WebSocket connection ownership from many screen-scoped
providers to a single wallet-root lifecycle manager, which can impact
reconnect behavior, resource usage, and perps data availability across
the app.
> 
> **Overview**
> **Centralizes Perps WebSocket lifecycle management** by introducing
`PerpsAlwaysOnProvider` (mounted at the `Wallet` root) as the *only*
component that calls `PerpsConnectionManager.connect()`/`disconnect()`,
driven by `AppState` background/foreground transitions with a delayed
reconnect and error logging.
> 
> `PerpsConnectionProvider` is simplified to a context/state wrapper
only: it no longer takes `isVisible`, no longer unmounts children when
hidden, and removes the `usePerpsConnectionLifecycle` hook entirely
(hook + tests deleted, barrel export removed). Call sites (Perps tab,
Activity perps transactions tab, homepage Perps section, Explore search,
UrlAutocomplete, Trending sections) are updated to stop passing
visibility props and to use `suppressErrorView` where needed.
> 
> Tests and docs are updated to match the new always-on architecture,
including new coverage for `PerpsAlwaysOnProvider` and adjusted
`PerpsConnectionProvider` retry/error behavior.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
1644485. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
@pull pull Bot locked and limited conversation to collaborators Mar 9, 2026
@pull pull Bot added the ⤵️ pull label Mar 9, 2026
@pull pull Bot merged commit 52520eb into Reality2byte:main Mar 9, 2026
0 of 9 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant