Theme Controller Listener Cleanup

"/home/yossef/notes/git/projects/maplibra/risks/Theme Controller Listener Cleanup.md"

path: maplibra/risks/Theme Controller Listener Cleanup.md

- **fileName**: Theme Controller Listener Cleanup
- **Created on**: 2026-06-16 00:48:42

Theme Controller Listener Cleanup

Migrated from: obs_vault

Parent: ./Theme Risk Index.md

Severity: high.

Summary

ThemeController registers anonymous event listeners but destroy() removes different references. After repeated map loads, old controllers can keep handling theme events while holding references to old maps, layer managers, and route controllers.

Code Evidence

Trigger Conditions

Likely Symptoms

Why It Matters

This is a lifecycle leak. It can keep large objects alive, including Mapbox map instances, old layer managers, and pathfinding controllers. It is also hard to see in a single-load happy path, so it can survive normal manual testing.

Current Tests

Missing Tests

How To Make Better

Debug Steps

  1. Load a map, toggle theme once, then load another map.
  2. Add instrumentation around handleThemeToggle() and applyTheme().
  3. Toggle theme again and confirm whether old controller instances still fire.
  4. Dispatch a synthetic storage event and verify only the active controller reacts.

See Also