Refactor: split background worker by concern (tracker / router / lifecycle / badge)#242
Merged
Merged
Conversation
…cycle / badge) `background.ts` had grown to 672 lines mixing five concerns: per-tab in-memory state, badge/icon rendering, state mutators, the typed message router, and the chrome.tabs / storage lifecycle wiring. Split into focused modules under `lib/background/`, leaving the entry point as 43 lines of pure wiring. - `badge.ts` — toolbar badge/icon painting (chrome.action side effects only) - `tab-tracker.ts` — `createTabTracker()` owns all per-tab state + the badge refresh that reads across it - `message-handlers.ts` — `registerBackgroundMethods(tracker)`, the typed content/popup→background router - `lifecycle.ts` — `startBackgroundLifecycle(tracker)`: tab listeners, storage seeds/subscriptions, the recovery-pause session bridge Behavior-preserving: the enforcement subscription and its startup seed are unified onto `setEnforcementEnabled` (the clear-stale-maps branch is a no-op on empty maps at seed time). Session-store writes and content broadcasts stay in the lifecycle layer; the tracker owns only in-memory cache. Background-purity check still passes (new modules import only `RuleId` from `rules/rule-metadata`). A side benefit: the badge/tracker helpers were locked in the untestable entry bundle and are now unit-testable. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Cover the behaviors the refactor had to preserve: cross-frame badge summing, the "999+" cap, frame decrement on an empty re-report, the "!" detection badge and its color precedence, the sorted popup snapshot, and the removeTab/global-off paths. These were untestable while inlined in the service-worker entry bundle. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
CI's tsconfig.test.json typecheck (noUncheckedIndexedAccess + the precise chrome.action signatures) caught what `bun run check` alone missed: the mock-call helper was typed against `setBadgeText` (rejecting `setBadgeBackgroundColor`) and indexed `mock.calls` without guarding the array access. Type the helper param as `jest.Mock` and guard the index. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
extension/src/background.tshad grown to 672 lines mixing five concerns. This splits it into focused modules underlib/background/, leaving the entry point as 43 lines of pure wiring. No behavior change.background.ts(672 → 43)lib/background/badge.tschrome.actionside effects onlylib/background/tab-tracker.tscreateTabTracker()owns all per-tab in-memory state + the badge refresh that reads across itlib/background/message-handlers.tsregisterBackgroundMethods(tracker)— the typed content/popup→background routerlib/background/lifecycle.tsstartBackgroundLifecycle(tracker)— tab listeners, storage seeds/subscriptions, the recovery-pause session bridgeDesign notes
refreshBadgereads across every per-tab map plus the protection-signal cache, so that shared state is encapsulated in one factory; the router and lifecycle are thin shells over its operations.tabPauseMap.remove) and content broadcasts (notifyTabPause) live inlifecycle.ts;removeTab()returns ahadPauseboolean so the caller decides whether to mirror the removal to session storage.setEnforcementEnabled(the clear-stale-maps branch is a no-op on empty maps at seed time).RuleIdfromrules/rule-metadata; the build's background-purity guard reportsok (39 canaries, no leaks).Tests
The badge/tracker helpers were previously locked in the untestable entry bundle. Added
lib/background/__tests__/tab-tracker.test.ts(10 cases) covering cross-frame summing, the999+cap, frame decrement, the detection badge + color precedence, the sorted popup snapshot, and theremoveTab/ global-off paths.Verification
bun run check(biome + eslint) — cleanbun run typecheck— cleanbun run build—background.js purity: ok (39 canaries, no leaks)bun run test— 109 suites / 2052 tests pass🤖 Generated with Claude Code