From 4c69cf13f15f72bef457349caa799d736749da9f Mon Sep 17 00:00:00 2001 From: Rohan Gudimetla Date: Sat, 14 Jun 2025 04:07:00 +0530 Subject: [PATCH] lightbox: Index asset_map entries to use message_id. This commit indexes the `asset_map` cache entries with message_id so that it can be helpful to invalidate cache of a certain message when its content is edited. This is a preparatory commit to #21311 --- web/src/lightbox.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/web/src/lightbox.ts b/web/src/lightbox.ts index d444ee6e60..b117c702e4 100644 --- a/web/src/lightbox.ts +++ b/web/src/lightbox.ts @@ -34,9 +34,10 @@ type Media = { let is_open = false; // The asset map is a map of all retrieved images and YouTube videos that are memoized instead of -// being looked up multiple times. It is keyed by the asset's "canonical URL," which is likely the -// `src` used in the message feed, but for thumbnailed images is the full-resolution original URL. -const asset_map = new Map(); +// being looked up multiple times. It is keyed by the message id with each value being the +// message's assets map keyed by the asset's "canonical URL," which is likely the `src` used in +// the message feed, but for thumbnailed images is the full-resolution original URL. +const asset_map = new Map>(); export class PanZoomControl { // Class for both initializing and controlling the @@ -479,10 +480,11 @@ function supports_heic(): boolean { // retrieve the metadata from the DOM and store into the asset_map. export function parse_media_data(media: HTMLMediaElement | HTMLImageElement): Media { + const message_id = rows.get_message_id(media); const canonical_url = canonical_url_of_media(media); - if (asset_map.has(canonical_url)) { + if (asset_map.has(message_id) && asset_map.get(message_id)?.has(canonical_url)) { // Use the cached value - const payload = asset_map.get(canonical_url); + const payload = asset_map.get(message_id)!.get(canonical_url); assert(payload !== undefined); return payload; } @@ -561,7 +563,6 @@ export function parse_media_data(media: HTMLMediaElement | HTMLImageElement): Me if (is_compose_preview_media) { sender_full_name = people.my_full_name(); } else { - const message_id = rows.get_message_id(media); const message = message_store.get(message_id); if (message === undefined) { blueslip.error("Lightbox for unknown message", {message_id}); @@ -582,7 +583,10 @@ export function parse_media_data(media: HTMLMediaElement | HTMLImageElement): Me }; if (!is_loading_placeholder && canonical_url !== "") { - asset_map.set(canonical_url, payload); + if (!asset_map.has(message_id)) { + asset_map.set(message_id, new Map()); + } + asset_map.get(message_id)!.set(canonical_url, payload); } return payload; }