diff --git a/lib/immich.ts b/lib/immich.ts index 0766919..4484d4b 100644 --- a/lib/immich.ts +++ b/lib/immich.ts @@ -74,6 +74,7 @@ class ImmichClient { private hasLoggedAlbums = false; private pendingAlbumsPromise: Promise | null = null; private pendingAlbumPromises = new Map>(); + private pendingAssetPromises = new Map>(); private get config() { return getConfig(); @@ -344,11 +345,27 @@ class ImmichClient { const cached = cache.get(cacheKey); if (cached) return cached; - const asset = await this.request(`/assets/${encodeURIComponent(assetId)}`); - if (!asset) return null; + // ⚡ Bolt: Deduplicate concurrent requests for the same asset ID. + // If a request for this asset is already in flight (e.g. from Promise.all in a grid), + // return the pending promise instead of triggering a redundant API call. + if (this.pendingAssetPromises.has(assetId)) { + return this.pendingAssetPromises.get(assetId)!; + } - cache.set(cacheKey, asset, this.config.cacheTtl); - return asset; + const promise = (async () => { + try { + const asset = await this.request(`/assets/${encodeURIComponent(assetId)}`); + if (!asset) return null; + + cache.set(cacheKey, asset, this.config.cacheTtl); + return asset; + } finally { + this.pendingAssetPromises.delete(assetId); + } + })(); + + this.pendingAssetPromises.set(assetId, promise); + return promise; } /**