MetaMask / MetaMask/metamask-extension
[P1] Extract active-tab tracker from `background.js`
- Dominant language
- TypeScript
- Stars
- 13.2k
- Forks
- 5.6k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 451
Description
**Parent Epic:** [#44543 — `app/scripts/background.js` decomposition](https://github.com/MetaMask/metamask-extension/issues/44543)
**File:** `app/scripts/background.js` → `app/scripts/lib/active-tab/`
**Size:** M
---
## Problem
`refreshAppActiveTab` (~1445–1500), `initializeAppActiveTab`, and the `tabs.onActivated` / `tabs.onUpdated` / `windows.onFocusChanged` listeners (~2371–2544) keep `appStateController.appActiveTab` + subject metadata synced to the focused tab. Reads/writes `controller.{appStateController,subjectMetadataController}` only; no counters. The two large tab listeners duplicate `setAppActiveTab`+`addSubjectMetadata` logic that DRYs up on extraction. `refreshAppActiveTab` is also called by the side-panel branch of the connection wiring.
## Solution
`app/scripts/lib/active-tab/active-tab-tracker.ts` exporting `installActiveTabTracker({ getController, isInitialized })` that registers the listeners and returns `refreshAppActiveTab` for the side-panel caller.
## Acceptance Criteria
- [ ] TS module with an injected seam; originals deleted; duplicated listener logic DRY'd.
- [ ] Unit test against mock controller + stubbed `browser.tabs`/`windows`.
- [ ] Active-tab sync parity in a dev build.
Contributor guide
Research direction
Start with app/scripts/background.js, especially refreshAppActiveTab, initializeAppActiveTab, and the tabs.onActivated, tabs.onUpdated, and windows.onFocusChanged listeners. Review the side-panel connection wiring that calls refreshAppActiveTab, then add app/scripts/lib/active-tab/active-tab-tracker.ts with the injected seam and a unit test using mocked controllers and browser tabs/windows. Done means the originals are removed, duplicated listener logic is shared, and active-tab sync matches in a dev build.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- web-dev
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100