[PR #1907] [CLOSED] fix(staffml): track and clear toast auto-dismiss timers #36522

Closed
opened 2026-07-16 00:25:25 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/harvard-edge/cs249r_book/pull/1907
Author: @farhan523
Created: 6/26/2026
Status: Closed

Base: devHead: fix/staffml-toast-timer-leak


📝 Commits (1)

  • aa682c9 fix(staffml): track and clear toast auto-dismiss timers

📊 Changes

2 files changed (+101 additions, -4 deletions)

View changed files

interviews/staffml/src/__tests__/toast-timer-cleanup.test.tsx (+77 -0)
📝 interviews/staffml/src/components/Toast.tsx (+24 -4)

📄 Description

Problem

Each toast schedules a 4s setTimeout to auto-dismiss itself, but the timer handle was discarded:

const show = useCallback((msg) => {
  const id = nextId++;
  setToasts(prev => [...prev, { ...msg, id }]);
  setTimeout(() => { setToasts(prev => prev.filter(t => t.id !== id)); }, 4000); // never cleared
}, []);

So the timer was never cleared. If ToastProvider unmounts within 4s of a toast (route teardown, fast-refresh, test cleanup) the timer still fires setToasts on a dead tree, and a manual dismiss leaves the timer running to a wasted no-op tick.

Fix

  • Track each toast's timer in a useRef<Map<id, timeout>>.
  • Cancel the timer in dismiss().
  • Clear all pending timers in a provider unmount cleanup.

No user-visible behavior change.

Test

Adds toast-timer-cleanup.test.tsx (fake timers) asserting the pending timer is cleared on unmount and on manual dismiss, and that flushing timers afterward doesn't throw.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/harvard-edge/cs249r_book/pull/1907 **Author:** [@farhan523](https://github.com/farhan523) **Created:** 6/26/2026 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `fix/staffml-toast-timer-leak` --- ### 📝 Commits (1) - [`aa682c9`](https://github.com/harvard-edge/cs249r_book/commit/aa682c9363183dbf2b2e70cbba83f84a3ecc8f97) fix(staffml): track and clear toast auto-dismiss timers ### 📊 Changes **2 files changed** (+101 additions, -4 deletions) <details> <summary>View changed files</summary> ➕ `interviews/staffml/src/__tests__/toast-timer-cleanup.test.tsx` (+77 -0) 📝 `interviews/staffml/src/components/Toast.tsx` (+24 -4) </details> ### 📄 Description ## Problem Each toast schedules a 4s `setTimeout` to auto-dismiss itself, but the timer handle was discarded: ```js const show = useCallback((msg) => { const id = nextId++; setToasts(prev => [...prev, { ...msg, id }]); setTimeout(() => { setToasts(prev => prev.filter(t => t.id !== id)); }, 4000); // never cleared }, []); ``` So the timer was never cleared. If `ToastProvider` unmounts within 4s of a toast (route teardown, fast-refresh, test cleanup) the timer still fires `setToasts` on a dead tree, and a manual dismiss leaves the timer running to a wasted no-op tick. ## Fix - Track each toast's timer in a `useRef<Map<id, timeout>>`. - Cancel the timer in `dismiss()`. - Clear all pending timers in a provider unmount cleanup. No user-visible behavior change. ## Test Adds `toast-timer-cleanup.test.tsx` (fake timers) asserting the pending timer is cleared on unmount and on manual dismiss, and that flushing timers afterward doesn't throw. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-07-16 00:25:25 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/cs249r_book#36522