MetaMask / MetaMask/metamask-extension
[`MetamaskController` B.4] Fully port `controller-subscriptions`
- Dominant language
- TypeScript
- Stars
- 13.2k
- Forks
- 5.6k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 451
Description
**Sub-epic:** [#41737 — `MetamaskController` decomposition: extension-specific modules](https://github.com/MetaMask/metamask-extension/issues/41737)
**Files:**
- `app/scripts/lib/controller-subscriptions/index.ts` (new)
- `app/scripts/lib/controller-subscriptions/controller-subscriptions.test.ts` (new)
- `app/scripts/metamask-controller.js`
**Size:** M
## Problem
Cross-controller subscription wiring lives inline on `MetamaskController` in `_registerSubscriptions`: lock/unlock → session manager, network change → `AccountTracker` + token detection, selected account change → Sentry, `TransactionController:incomingTransactionBlock` → notification dispatch, `PermissionController:stateChange` → perps origin tracking, and any others surfaced while reading the method. Extract this wiring into `controller-subscriptions/` so subscription setup is centralized, independently testable, and disposable.
## Solution
1. Create `app/scripts/lib/controller-subscriptions/` with `index.ts`.
2. Port the full subscription set from `metamask-controller.js`.
3. Export as `registerSubscriptions(deps): (() => void)[]` returning a disposer list.
4. Have `MetamaskController` call `registerSubscriptions` once at construction and store the disposer list.
5. Add unit tests that mock messenger `subscribe` / `call` and verify each subscription fires its handler.
6. Delete `_registerSubscriptions` and related wiring from `metamask-controller.js`.
## Acceptance criteria
- [ ] `app/scripts/lib/controller-subscriptions/index.ts` exists with the full subscription set
- [ ] `registerSubscriptions` returns a disposer array
- [ ] Tests verify each subscription fires its handler
- [ ] `MetamaskController._registerSubscriptions` deleted; only the disposer-list storage remains in the constructor
- [ ] `yarn test` green, `tsc` green, extension dev build boots
## Dependencies
**B.1** — needs `SessionManager:onLock` / `SessionManager:onUnlock` messenger actions registered.
Contributor guide
Research direction
Read _registerSubscriptions in app/scripts/metamask-controller.js and review the B.1 dependency for SessionManager:onLock and SessionManager:onUnlock. Use app/scripts/lib/controller-subscriptions/controller-subscriptions.test.ts to verify each messenger subscription handler and disposer, then run yarn test, tsc, and the extension dev build; done means the inline method is removed and the acceptance criteria pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- backend, testing
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100