fix(flutter): stop the cache feedback loop — playback no longer competes
The prefetcher + alwaysRefresh combination was creating a feedback loop visible in the logs as repeated `metadataPrefetcher: warming N albums` cycles, each kicking N parallel getAlbum fetches that then triggered drift writes that triggered re-emits that re-ran the prefetcher. Tap-to-play was queueing behind 14+ in-flight cache fetches. Three structural fixes: 1. Prefetcher hard-dedupes per session via _warmedArtists Set. Re-rendering a screen no longer re-fires fetches for ids we've already seen. 2. Prefetcher only warms artistProvider, not albumProvider. Albums carry track lists; pre-warming N albums fans out N parallel "fetch tracks" round trips for content the user may never visit. Artist rows are single-row lookups — cheap. Album detail loads on tap (still fast: server-side perf work makes it ~one round trip). 3. Drop alwaysRefresh from albumProvider, artistProvider, artistAlbumsProvider, artistTracksProvider. Each was kicking one silent background refresh per first cache hit. With the prefetcher creating many subscriptions in parallel, that meant every prewarmed id triggered an extra fetch even when drift was already populated. playlistsListProvider keeps alwaysRefresh — system playlists genuinely rotate UUIDs and need the catch-up. Pull-to- refresh remains the explicit invalidation path everywhere else. Removed the warmAlbums calls from the library Albums tab and artist detail album grid (the storm sources). Net effect: cold app boot warms ~12-15 artist rows once, period. Tapping a tile still fetches its detail on demand (one round trip, fast). User-initiated playback isn't queued behind cache work.
This commit is contained in:
@@ -135,9 +135,6 @@ class ArtistDetailScreen extends ConsumerWidget {
|
||||
error: (e, _) => Center(child: Text('$e', style: TextStyle(color: fs.error))),
|
||||
loading: () => const Padding(padding: EdgeInsets.all(16), child: Center(child: CircularProgressIndicator())),
|
||||
data: (list) {
|
||||
ref
|
||||
.read(metadataPrefetcherProvider)
|
||||
.warmAlbums(list.map((a) => a.id));
|
||||
return LayoutBuilder(builder: (ctx, constraints) {
|
||||
const cols = 3;
|
||||
const sidePad = 8.0;
|
||||
|
||||
@@ -66,9 +66,10 @@ final artistProvider =
|
||||
isOnline: () async => (await ref
|
||||
.read(connectivityProvider.future)
|
||||
.timeout(const Duration(seconds: 3), onTimeout: () => true)),
|
||||
// SWR: yield cache instantly on hit, refresh in the background so
|
||||
// the row catches up to server state without making the user wait.
|
||||
alwaysRefresh: true,
|
||||
// No alwaysRefresh: artist rows don't change frequently, and the
|
||||
// metadata prefetcher creates many subscriptions in parallel —
|
||||
// each silently re-fetching once would saturate the request
|
||||
// pipeline behind the user's actual playback request.
|
||||
tag: 'artist($id)',
|
||||
);
|
||||
});
|
||||
@@ -100,7 +101,6 @@ final artistAlbumsProvider =
|
||||
isOnline: () async => (await ref
|
||||
.read(connectivityProvider.future)
|
||||
.timeout(const Duration(seconds: 3), onTimeout: () => true)),
|
||||
alwaysRefresh: true,
|
||||
tag: 'artistAlbums($artistId)',
|
||||
);
|
||||
});
|
||||
@@ -137,7 +137,6 @@ final artistTracksProvider =
|
||||
isOnline: () async => (await ref
|
||||
.read(connectivityProvider.future)
|
||||
.timeout(const Duration(seconds: 3), onTimeout: () => true)),
|
||||
alwaysRefresh: true,
|
||||
tag: 'artistTracks($artistId)',
|
||||
);
|
||||
});
|
||||
@@ -169,10 +168,7 @@ final albumProvider = StreamProvider.family<
|
||||
// Once-per-subscription guard so we don't re-fetch in a loop if the
|
||||
// server genuinely returns zero tracks (or if the fetch fails).
|
||||
var fetchAttempted = false;
|
||||
// SWR revalidation guard — fire one background refresh per
|
||||
// subscription on the first complete cache hit, so the displayed
|
||||
// data catches up to server state without making the user wait.
|
||||
var revalidated = false;
|
||||
// (revalidated flag removed; see SWR note below the yield.)
|
||||
|
||||
Future<bool> isOnline() async {
|
||||
try {
|
||||
@@ -303,15 +299,13 @@ final albumProvider = StreamProvider.family<
|
||||
);
|
||||
}).toList();
|
||||
|
||||
// SWR: first complete cache hit triggers one background refresh
|
||||
// so we don't show stale data indefinitely. Drift watch re-emits
|
||||
// on success and the next iteration yields the fresh data.
|
||||
if (!revalidated && trackRows.isNotEmpty) {
|
||||
revalidated = true;
|
||||
if (await isOnline()) {
|
||||
unawaited(fetchAndPopulate());
|
||||
}
|
||||
}
|
||||
// Note: NO SWR here on purpose. Prior code kicked a background
|
||||
// refresh on every first cache hit, which combined with the
|
||||
// metadata prefetcher meant every prewarmed album id triggered
|
||||
// an extra fetch even when drift was already populated. Tracks
|
||||
// and album metadata don't change on the same timescale as
|
||||
// playlists; a stale read is fine until the user invalidates
|
||||
// (pull-to-refresh) or the album is genuinely re-fetched.
|
||||
|
||||
yield (album: album, tracks: tracks);
|
||||
}
|
||||
|
||||
@@ -269,9 +269,6 @@ class _AlbumsTab extends ConsumerWidget {
|
||||
loading: () => const Center(child: CircularProgressIndicator()),
|
||||
error: (e, _) => Center(child: Text('$e', style: TextStyle(color: fs.error))),
|
||||
data: (page) {
|
||||
ref
|
||||
.read(metadataPrefetcherProvider)
|
||||
.warmAlbums(page.items.map((a) => a.id));
|
||||
return page.items.isEmpty
|
||||
? Center(child: Text("No albums yet — scan a library folder via the server's config.", style: TextStyle(color: fs.ash), textAlign: TextAlign.center))
|
||||
: RefreshIndicator(
|
||||
|
||||
Reference in New Issue
Block a user