feat(performance): Add promise deduplication for ImmichClient.getAlbum (#69)

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
Ralksta
2026-04-01 12:46:12 +02:00
committed by GitHub
parent 258c36899c
commit dbc525e8d8

View File

@@ -73,6 +73,7 @@ export interface SubpageSummary {
class ImmichClient { class ImmichClient {
private hasLoggedAlbums = false; private hasLoggedAlbums = false;
private pendingAlbumsPromise: Promise<ImmichAlbum[]> | null = null; private pendingAlbumsPromise: Promise<ImmichAlbum[]> | null = null;
private pendingAlbumPromises = new Map<string, Promise<ImmichAlbum | null>>();
private get config() { private get config() {
return getConfig(); return getConfig();
@@ -281,18 +282,31 @@ class ImmichClient {
const cached = cache.get<ImmichAlbum>(cacheKey); const cached = cache.get<ImmichAlbum>(cacheKey);
if (cached) return cached; if (cached) return cached;
const album = await this.request<ImmichAlbum>(`/albums/${encodeURIComponent(albumId)}`); if (this.pendingAlbumPromises.has(albumId)) {
if (!album) return null; return this.pendingAlbumPromises.get(albumId)!;
}
// Filter out trashed assets const promise = (async () => {
album.assets = (album.assets || []).filter((a) => !a.isTrashed); try {
const album = await this.request<ImmichAlbum>(`/albums/${encodeURIComponent(albumId)}`);
if (!album) return null;
const name = this.config.albumOverrides[album.id] ?? album.albumName; // Filter out trashed assets
album.albumName = name; album.assets = (album.assets || []).filter((a) => !a.isTrashed);
album.slug = slugify(name);
cache.set(cacheKey, album, this.config.cacheTtl); const name = this.config.albumOverrides[album.id] ?? album.albumName;
return album; album.albumName = name;
album.slug = slugify(name);
cache.set(cacheKey, album, this.config.cacheTtl);
return album;
} finally {
this.pendingAlbumPromises.delete(albumId);
}
})();
this.pendingAlbumPromises.set(albumId, promise);
return promise;
} }
/** /**