Skip to curriculum
Jump to the editor

25.4. Find and fix the leak in a view that closes

Medium
Topics

This question is in

25. Memory and cleanup
20–30 min

A notifications panel opens and closes many times in a long session. It looks fine: it shows the notifications, updates its "Updated 3 min ago" label, closes on Escape and fits the window. But each time it closes, something it set up stays behind, and every leftover keeps a reference to the panel's closure and through it to the removed <section>, its list and its store. After an hour, the page holds hundreds of dead panels, and some of them still run code.

panel.js contains the working, leaky panel. Find every leak and fix it, without changing what the panel does while it is open.

Files

  • panel.js (edit this): createPanel(root, store) builds the panel and returns { element, close() }; openPanelCount() returns how many panels are open.
  • The store has getState(), which returns an array of strings, and subscribe(listener), which returns an unsubscribe function.

Requirements

While a panel is open, it keeps working as it does now:

  1. It shows the store's notifications in a section.panel inside root, and re-renders when the store changes.
  2. Its .updated label shows the minutes since the last change and updates every minute.
  3. Escape (a keydown on document) closes it, and a window resize sets its max-height to the window height minus 40 px (at least 120 px).
  4. openPanelCount() counts it.

When it closes, by close() or by Escape, nothing it set up is left:

  1. It is unsubscribed from the store, and later store changes do not render it.
  2. Its timer is cleared: no timer is left waiting.
  3. Its keydown listener on document and its resize listener on window are removed, and Escape no longer reaches it.
  4. It is no longer counted by openPanelCount().
  5. Closing twice is safe, and closing one panel leaves other open panels fully working.

Examples

JAVASCRIPT
const panel = createPanel(document.querySelector("#root"), store);
openPanelCount();   // 1
panel.close();
openPanelCount();   // 1 before the fix, 0 after it
store.set([...]);   // before the fix, the closed panel still re-renders
JAVASCRIPT
// Before the fix, after 100 opens and closes:
//   the store has 100 subscribers, 100 intervals tick every minute,
//   document has 100 keydown listeners and window 100 resize listeners.
// After the fix: none of them.

Environment

  • Plain JavaScript in a browser page; the tests call the globals createPanel and openPanelCount with a fake store that counts its subscribers.
  • Timers run on a virtual clock that counts waiting timers. The tests also watch addEventListener and removeEventListener on window and document to see which listeners are still attached after close.
  • The first six tests already pass; they make sure the fix keeps the panel working. Out of scope: listeners on the panel's own elements (they go with the element) and animations.

Hints

  1. Read createPanel from top to bottom and list every call that registers something somewhere outside the panel. What does each one need to be undone?
  2. One of the listeners can never be removed as it is written. Why?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…