fix: resolve Copilot code review comments on PR #529

- Clear initialScrollTimerRef when room changes to prevent stale timer
  from firing in a new room after navigation (RoomTimeline.tsx)
- Scope URL preview cache by MatrixClient to prevent cross-account
  deduplication when multiple clients/sessions exist (UrlPreviewCard.tsx)
- Delete cache entries after promise settles (both success/rejection)
  to prevent unbounded cache growth over long sessions (UrlPreviewCard.tsx)
- Fix pagination token direction check: use appropriate timeline end
  and token direction based on pagination direction (forward/backward)
  instead of always checking first timeline's backward token
  (useTimelineSync.ts)
This commit is contained in:
Evie Gauthier committed 2026-03-27 23:17:03 -04:00
1 parent e1c8c64cda
commit 1114c047d5
3 files changed
+29 -9

No files matched your search

@@ -16,10 +16,23 @@ const linkStyles = { color: color.Success.Main };
// Module-level in-flight deduplication: prevents N+1 concurrent requests when a
// large event batch renders many UrlPreviewCard instances for the same URL.
// Keyed by URL only (not ts) — the same URL shows the same preview regardless
// of which message referenced it. Rejected promises are evicted so a later
// render can retry after network recovery.
const previewRequestCache = new Map<string, Promise<IPreviewUrlResponse>>();
// Scoped by MatrixClient to avoid cross-account dedup if multiple clients exist.
// Inner cache keyed by URL only (not ts) — the same URL shows the same preview
// regardless of which message referenced it. Promises are evicted after settling
// so a later render can retry after network recovery.
const previewRequestCache = new WeakMap<
any,
Map<string, Promise<IPreviewUrlResponse>>
>();
const getClientCache = (mx: any): Map<string, Promise<IPreviewUrlResponse>> => {
let clientCache = previewRequestCache.get(mx);
if (!clientCache) {
clientCache = new Map();
previewRequestCache.set(mx, clientCache);
}
return clientCache;
};
const openMediaInNewTab = async (url: string | undefined) => {
if (!url) {
@@ -41,11 +54,12 @@ export const UrlPreviewCard = as<'div', { url: string; ts: number; mediaType?: s
const [previewStatus, loadPreview] = useAsyncCallback(
useCallback(() => {
if (isDirect) return Promise.resolve(null);
const cached = previewRequestCache.get(url);
const clientCache = getClientCache(mx);
const cached = clientCache.get(url);
if (cached !== undefined) return cached;
const urlPreview = mx.getUrlPreview(url, ts);
previewRequestCache.set(url, urlPreview);
urlPreview.catch(() => previewRequestCache.delete(url));
clientCache.set(url, urlPreview);
urlPreview.finally(() => clientCache.delete(url));
return urlPreview;
}, [url, ts, mx, isDirect])
);
+4
View File
@@ -224,6 +224,10 @@ export function RoomTimeline({
mountScrollWindowRef.current = Date.now() + 3000;
currentRoomIdRef.current = room.roomId;
pendingReadyRef.current = false;
if (initialScrollTimerRef.current !== undefined) {
clearTimeout(initialScrollTimerRef.current);
initialScrollTimerRef.current = undefined;
}
setIsReady(false);
}
+4 -2
View File
@@ -192,9 +192,11 @@ const useTimelinePagination = (
const fetched = countAfter - countBefore;
if (fetched > 0 && fetched < 5) {
const checkTimeline = backwards ? freshLTimelines[0] : freshLTimelines[freshLTimelines.length - 1];
const checkDirection = backwards ? Direction.Backward : Direction.Forward;
const stillHasToken =
typeof getLinkedTimelines(freshLTimelines[0])[0]?.getPaginationToken(
Direction.Backward
typeof getLinkedTimelines(checkTimeline)[0]?.getPaginationToken(
checkDirection
) === 'string';
if (stillHasToken) {
// Release lock so inner paginate can claim it, then mark continuing