Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Refactor: split background worker by concern (tracker / router / lifecycle / badge)#242

Merged
twschiller merged 3 commits into
mainfrom
refactor/background-by-concern
Jun 10, 2026
Merged

Refactor: split background worker by concern (tracker / router / lifecycle / badge)#242
twschiller merged 3 commits into
mainfrom
refactor/background-by-concern

Conversation

@twschiller

Copy link
Copy Markdown
Contributor

Summary

extension/src/background.ts had grown to 672 lines mixing five concerns. This splits it into focused modules under lib/background/, leaving the entry point as 43 lines of pure wiring. No behavior change.

Module Concern
background.ts (672 → 43) Pure wiring: construct the tracker, start each subsystem
lib/background/badge.ts Toolbar badge/icon paintingchrome.action side effects only
lib/background/tab-tracker.ts createTabTracker() owns all per-tab in-memory state + the badge refresh that reads across it
lib/background/message-handlers.ts registerBackgroundMethods(tracker) — the typed content/popup→background router
lib/background/lifecycle.ts startBackgroundLifecycle(tracker) — tab listeners, storage seeds/subscriptions, the recovery-pause session bridge

Design notes

  • The tracker is the seam. refreshBadge reads 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.
  • Side effects stay in lifecycle. The tracker owns only in-memory cache. Session-store writes (tabPauseMap.remove) and content broadcasts (notifyTabPause) live in lifecycle.ts; removeTab() returns a hadPause boolean so the caller decides whether to mirror the removal to session storage.
  • 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).
  • Purity holds. New modules import only RuleId from rules/rule-metadata; the build's background-purity guard reports ok (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, the 999+ cap, frame decrement, the detection badge + color precedence, the sorted popup snapshot, and the removeTab / global-off paths.

Verification

  • bun run check (biome + eslint) — clean
  • bun run typecheck — clean
  • bun run buildbackground.js purity: ok (39 canaries, no leaks)
  • bun run test — 109 suites / 2052 tests pass

🤖 Generated with Claude Code

twschiller and others added 2 commits June 9, 2026 21:22
…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]>
@vercel

vercel Bot commented Jun 10, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
agent-browser-shield-demo-site Ready Ready Preview, Comment Jun 10, 2026 1:32am

Request Review

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]>
@twschiller twschiller merged commit 0c957cb into main Jun 10, 2026
7 checks passed
@twschiller twschiller deleted the refactor/background-by-concern branch June 10, 2026 01:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant