Commit 52520eb
authored
feat(perps): always on socket connection (#27103)
## **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 -->1 parent 8099e72 commit 52520eb
17 files changed
Lines changed: 535 additions & 1129 deletions
File tree
- app/components
- UI
- Perps
- Views/PerpsTabView
- hooks
- providers
- UrlAutocomplete
- Views
- ActivityView
- Homepage/Sections/Perpetuals
- TrendingView
- Views/ExploreSearchScreen
- Wallet
- docs/perps
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
3 | 2 | | |
4 | 3 | | |
5 | | - | |
6 | 4 | | |
7 | 5 | | |
8 | 6 | | |
| |||
13 | 11 | | |
14 | 12 | | |
15 | 13 | | |
16 | | - | |
17 | 14 | | |
18 | | - | |
19 | | - | |
20 | 15 | | |
21 | 16 | | |
22 | 17 | | |
| |||
26 | 21 | | |
27 | 22 | | |
28 | 23 | | |
29 | | - | |
30 | | - | |
31 | | - | |
| 24 | + | |
| 25 | + | |
32 | 26 | | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
38 | 30 | | |
39 | 31 | | |
40 | 32 | | |
41 | 33 | | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | 34 | | |
56 | 35 | | |
57 | 36 | | |
| |||
64 | 43 | | |
65 | 44 | | |
66 | 45 | | |
67 | | - | |
| 46 | + | |
68 | 47 | | |
69 | 48 | | |
70 | 49 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
21 | 20 | | |
22 | 21 | | |
23 | 22 | | |
| |||
0 commit comments