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:
+17
-39
@@ -4,14 +4,13 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../library/library_providers.dart';
|
||||
import '../models/home_data.dart';
|
||||
|
||||
/// Pre-warms the drift cache for entities the user is most likely to
|
||||
/// tap. The home prefetch fires when /api/home returns. Library
|
||||
/// tabs and artist detail expose `warmAlbums` / `warmArtists` so they
|
||||
/// can fan out reads from their own AsyncNotifier callbacks.
|
||||
///
|
||||
/// All reads are unawaited and idempotent — providers that already
|
||||
/// have a cached value short-circuit. Readers/writers don't conflict
|
||||
/// because each provider de-duplicates concurrent subscribers.
|
||||
/// Pre-warms the drift cache for likely-tap targets. Conservative on
|
||||
/// purpose: only warms artistProvider rows (single row, single round
|
||||
/// trip per id) and only ever fires once per id per session. Album
|
||||
/// detail is NOT prewarmed — the albumProvider auto-fetches its track
|
||||
/// list when missing, and pre-warming N albums fans out N parallel
|
||||
/// "fetch tracks" round trips that compete with the user's actual
|
||||
/// playback request for bandwidth.
|
||||
class MetadataPrefetcher {
|
||||
MetadataPrefetcher(this._ref) {
|
||||
_ref.listen<AsyncValue<HomeData>>(homeProvider, (_, next) {
|
||||
@@ -21,28 +20,21 @@ class MetadataPrefetcher {
|
||||
|
||||
final Ref _ref;
|
||||
|
||||
/// Cap per section so a 50-album recently-added row doesn't fan
|
||||
/// out 50 fetches the moment the screen appears. Top-N covers what
|
||||
/// the user can see without scrolling on a typical phone.
|
||||
/// Per-session dedupe so re-rendering a screen (every UI rebuild
|
||||
/// fires the data: callback) doesn't trigger N more fetches for
|
||||
/// ids we've already warmed.
|
||||
final Set<String> _warmedArtists = {};
|
||||
|
||||
/// Cap per section. Covers what fits on screen without scrolling.
|
||||
static const _topN = 8;
|
||||
|
||||
/// Pre-warm drift for each album id (no-op past the first dedup).
|
||||
/// Called from the library Albums tab + artist detail album grid.
|
||||
void warmAlbums(Iterable<String> ids) {
|
||||
var n = 0;
|
||||
for (final id in ids) {
|
||||
if (id.isEmpty) continue;
|
||||
if (n++ >= _topN) break;
|
||||
_swallow(_ref.read(albumProvider(id).future));
|
||||
}
|
||||
if (n > 0) debugPrint('metadataPrefetcher: warming $n albums');
|
||||
}
|
||||
|
||||
/// Pre-warm drift for each artist id.
|
||||
/// Pre-warm artists. Albums are intentionally not pre-warmed —
|
||||
/// see class comment.
|
||||
void warmArtists(Iterable<String> ids) {
|
||||
var n = 0;
|
||||
for (final id in ids) {
|
||||
if (id.isEmpty) continue;
|
||||
if (!_warmedArtists.add(id)) continue; // already warmed
|
||||
if (n++ >= _topN) break;
|
||||
_swallow(_ref.read(artistProvider(id).future));
|
||||
}
|
||||
@@ -50,15 +42,11 @@ class MetadataPrefetcher {
|
||||
}
|
||||
|
||||
void _warmHome(HomeData h) {
|
||||
final albumIds = <String>{};
|
||||
final artistIds = <String>{};
|
||||
|
||||
for (final a in h.recentlyAddedAlbums.take(_topN)) {
|
||||
if (a.id.isNotEmpty) albumIds.add(a.id);
|
||||
if (a.artistId.isNotEmpty) artistIds.add(a.artistId);
|
||||
}
|
||||
for (final a in h.rediscoverAlbums.take(_topN)) {
|
||||
if (a.id.isNotEmpty) albumIds.add(a.id);
|
||||
if (a.artistId.isNotEmpty) artistIds.add(a.artistId);
|
||||
}
|
||||
for (final ar in h.rediscoverArtists.take(_topN)) {
|
||||
@@ -68,19 +56,9 @@ class MetadataPrefetcher {
|
||||
if (ar.id.isNotEmpty) artistIds.add(ar.id);
|
||||
}
|
||||
for (final t in h.mostPlayedTracks.take(_topN)) {
|
||||
if (t.albumId.isNotEmpty) albumIds.add(t.albumId);
|
||||
if (t.artistId.isNotEmpty) artistIds.add(t.artistId);
|
||||
}
|
||||
|
||||
debugPrint(
|
||||
'metadataPrefetcher: warming ${albumIds.length} albums + ${artistIds.length} artists');
|
||||
|
||||
for (final id in albumIds) {
|
||||
_swallow(_ref.read(albumProvider(id).future));
|
||||
}
|
||||
for (final id in artistIds) {
|
||||
_swallow(_ref.read(artistProvider(id).future));
|
||||
}
|
||||
warmArtists(artistIds);
|
||||
}
|
||||
|
||||
/// Discards the return value and any error from a fire-and-forget
|
||||
|
||||
Reference in New Issue
Block a user