Skip to content

Commit cc10f2e

Browse files
Clearing the widget settle fallback timer
1 parent a9b5750 commit cc10f2e

2 files changed

Lines changed: 87 additions & 2 deletions

File tree

‎components/report-view.jsx‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ export default function ReportView({
149149
if (!payload) return undefined;
150150
let cancelled = false;
151151
const container = containerRef.current;
152+
let widgetSettleTimer;
152153

153154
/** Renders the current table state and restores stateful report widgets. */
154155
async function run() {
@@ -158,8 +159,14 @@ export default function ReportView({
158159
// to arrive; everything else degrades gracefully when missing.
159160
await Promise.race([
160161
Promise.all(ENHANCE_WIDGETS.map(name => waitForWidget(name, { timeoutMs: widgetSettleMs }))),
161-
new Promise(resolve => setTimeout(resolve, widgetSettleMs))
162+
new Promise(resolve => {
163+
widgetSettleTimer = setTimeout(resolve, widgetSettleMs);
164+
})
162165
]);
166+
// The race is settled — the fallback no longer needs to fire, so
167+
// release its handle (and the closure it retains) early.
168+
clearTimeout(widgetSettleTimer);
169+
widgetSettleTimer = undefined;
163170
widgetsSettledRef.current = true;
164171
}
165172
if (cancelled) return;
@@ -216,7 +223,12 @@ export default function ReportView({
216223
}
217224

218225
run();
219-
return () => { cancelled = true; };
226+
return () => {
227+
cancelled = true;
228+
// Cancel a still-pending settle fallback so its timer cannot fire after
229+
// the effect is torn down. (No-op once the race has settled.)
230+
clearTimeout(widgetSettleTimer);
231+
};
220232
}, [payload, tableStates, widgetSettleMs, username, repository, branch,
221233
handleTableAction, handleCopy, enhanceReportImpl, enhanceTablesImpl]);
222234

‎tests/integration/report-view.test.jsx‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,79 @@ describe('ReportView', () => {
270270
});
271271
});
272272

273+
// Widget gate: the render effect races widget readiness against a fallback
274+
// setTimeout. The fallback handle must be cleared when the race settles and
275+
// again if the effect is torn down first, instead of keeping the closure
276+
// (and its references) alive until widgetSettleMs elapses.
277+
describe('widget settle fallback timer', () => {
278+
const SETTLE_MS = 4321; // distinctive delay so the fallback is identifiable
279+
280+
function trackTimers() {
281+
const realSet = globalThis.setTimeout;
282+
const realClear = globalThis.clearTimeout;
283+
const created = [];
284+
const cleared = new Set();
285+
globalThis.setTimeout = (fn, delay, ...args) => {
286+
const handle = realSet(fn, delay, ...args);
287+
created.push({ handle, delay });
288+
return handle;
289+
};
290+
globalThis.clearTimeout = handle => {
291+
cleared.add(handle);
292+
return realClear(handle);
293+
};
294+
return {
295+
handlesFor: delay => created.filter(t => t.delay === delay).map(t => t.handle),
296+
isCleared: handle => cleared.has(handle),
297+
restore() {
298+
globalThis.setTimeout = realSet;
299+
globalThis.clearTimeout = realClear;
300+
}
301+
};
302+
}
303+
304+
test('clears the fallback timer once the race settles', async () => {
305+
const timers = trackTimers();
306+
try {
307+
// Widgets already ready, so waitForWidget resolves synchronously and
308+
// creates no timers; the only SETTLE_MS timer is the race fallback.
309+
markWidgetReady('chart');
310+
markWidgetReady('vizdom');
311+
respondJsonFor(sampleJson);
312+
await renderReport({ widgetSettleMs: SETTLE_MS });
313+
314+
const handles = timers.handlesFor(SETTLE_MS);
315+
expect(handles).toHaveLength(1);
316+
expect(timers.isCleared(handles[0])).toBe(true);
317+
} finally {
318+
timers.restore();
319+
}
320+
});
321+
322+
test('clears a still-pending fallback timer when the effect is torn down', async () => {
323+
const timers = trackTimers();
324+
try {
325+
respondJsonFor(sampleJson); // widgets never become ready
326+
const utils = render(_jsx(ReportView, {
327+
username: 'junit-team',
328+
repository: 'junit4',
329+
widgetSettleMs: SETTLE_MS
330+
}));
331+
// waitForWidget timers (chart, vizdom) + the race fallback, in order.
332+
await waitFor(() => {
333+
expect(timers.handlesFor(SETTLE_MS).length).toBeGreaterThanOrEqual(3);
334+
});
335+
const fallbackTimer = timers.handlesFor(SETTLE_MS).at(-1);
336+
expect(timers.isCleared(fallbackTimer)).toBe(false);
337+
338+
utils.unmount();
339+
expect(timers.isCleared(fallbackTimer)).toBe(true);
340+
} finally {
341+
timers.restore();
342+
}
343+
});
344+
});
345+
273346
// ---------------------------------------------------------------------------
274347
// Enhanced tables (plan: implement-paginated-tables-with-sticky-headers):
275348
// pagination, sorting, search/filter, CSV export and copy-to-clipboard wired

0 commit comments

Comments
 (0)