25.4. Find and fix the leak in a view that closes
Topics
This question is in
25. Memory and cleanupA 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, andsubscribe(listener), which returns an unsubscribe function.
Requirements
While a panel is open, it keeps working as it does now:
- It shows the store's notifications in a
section.panelinsideroot, and re-renders when the store changes. - Its
.updatedlabel shows the minutes since the last change and updates every minute. - Escape (a
keydownondocument) closes it, and a windowresizesets itsmax-heightto the window height minus 40 px (at least 120 px). openPanelCount()counts it.
When it closes, by close() or by Escape, nothing it set up is left:
- It is unsubscribed from the store, and later store changes do not render it.
- Its timer is cleared: no timer is left waiting.
- Its
keydownlistener ondocumentand itsresizelistener onwindoware removed, and Escape no longer reaches it. - It is no longer counted by
openPanelCount(). - Closing twice is safe, and closing one panel leaves other open panels fully working.
Examples
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// 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
createPanelandopenPanelCountwith a fake store that counts its subscribers. - Timers run on a virtual clock that counts waiting timers. The tests also watch
addEventListenerandremoveEventListeneronwindowanddocumentto see which listeners are still attached afterclose. - 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
- Read
createPanelfrom top to bottom and list every call that registers something somewhere outside the panel. What does each one need to be undone? - 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