added genre cache

This commit is contained in:
2026-02-25 10:16:43 -05:00
parent 8f1ee25053
commit 5689c1be42
8 changed files with 93 additions and 31 deletions
@@ -55,6 +55,10 @@ export class LibraryController implements ReactiveController {
return libraryStore.getArtists();
}
async getGenres(): Promise<library.GenreWithCount[]> {
return libraryStore.getGenres();
}
async getAlbumsByArtist(
artistID: number,
): Promise<library.Album[]> {
@@ -81,6 +85,10 @@ export class LibraryController implements ReactiveController {
return libraryStore.getCachedArtists();
}
get cachedGenres(): library.GenreWithCount[] | null {
return libraryStore.getCachedGenres();
}
get tracksLoading(): boolean {
return libraryStore.isTracksLoading();
}
@@ -93,6 +101,10 @@ export class LibraryController implements ReactiveController {
return libraryStore.isArtistsLoading();
}
get genresLoading(): boolean {
return libraryStore.isGenresLoading();
}
// ===================================================================
// SCROLL POSITION
// ===================================================================
+52 -3
View File
@@ -3,6 +3,7 @@ import {
GetAllTracks,
GetAllAlbums,
GetAllArtists,
GetAllGenresWithCounts,
GetAlbumsByArtist,
} from '@go/library/Library';
import type { library } from '@go/models';
@@ -28,10 +29,12 @@ class LibraryStore {
private tracks: library.Track[] | null = null;
private albums: library.Album[] | null = null;
private artists: library.Artist[] | null = null;
private genres: library.GenreWithCount[] | null = null;
private tracksLoading = false;
private albumsLoading = false;
private artistsLoading = false;
private genresLoading = false;
private coverSizeValue: number = COVER_SIZE_DEFAULT;
@@ -50,6 +53,7 @@ class LibraryStore {
});
this.loadCoverSize();
this.eagerFetch();
}
// ===================================================================
@@ -126,6 +130,29 @@ class LibraryStore {
}
}
async getGenres(): Promise<library.GenreWithCount[]> {
if (this.genres !== null) {
return this.genres;
}
if (this.genresLoading) {
return this.waitForGenres();
}
this.genresLoading = true;
this.notify();
try {
const genres = await GetAllGenresWithCounts();
this.genres = genres;
return genres;
} finally {
this.genresLoading = false;
this.notify();
}
}
async getAlbumsByArtist(
artistID: number,
): Promise<library.Album[]> {
@@ -179,6 +206,14 @@ class LibraryStore {
return this.artistsLoading;
}
getCachedGenres(): library.GenreWithCount[] | null {
return this.genres;
}
isGenresLoading(): boolean {
return this.genresLoading;
}
// ===================================================================
// SCROLL POSITION
// ===================================================================
@@ -249,21 +284,24 @@ class LibraryStore {
this.tracks = null;
this.albums = null;
this.artists = null;
this.genres = null;
this.scrollPositions = { tracks: 0, albums: 0, artists: 0, genres: 0 };
this.notify();
this.eagerRefetch();
this.eagerFetch();
}
/**
* Re-fetches all data after cache invalidation so that
* Fetches all library data. Called from the constructor
* (initial load) and after cache invalidation so that
* controller subscribers receive fresh data on the next
* requestUpdate() cycle without needing their own
* LibraryScanComplete listener.
*/
private eagerRefetch(): void {
private eagerFetch(): void {
void this.getTracks();
void this.getAlbums();
void this.getArtists();
void this.getGenres();
}
// ===================================================================
@@ -317,6 +355,17 @@ class LibraryStore {
});
});
}
private waitForGenres(): Promise<library.GenreWithCount[]> {
return new Promise((resolve) => {
const unsub = this.subscribe(() => {
if (!this.genresLoading && this.genres !== null) {
unsub();
resolve(this.genres);
}
});
});
}
}
// Singleton instance.
+2
View File
@@ -35,6 +35,8 @@ class PlaylistStore {
EventsOn(Events.PlaylistsRestored, () => {
this.invalidate();
});
void this.getPlaylists();
}
// ===================================================================