mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-09-29 04:42:02 +02:00
perf(library): dispose unused playback row subscriptions
Auto-dispose media-item playback providers after their last listener leaves so scrolling does not retain every visited row subscription. Test disposal of 200 rows, active-row behavior and reattachment.
This commit is contained in:
@@ -86,22 +86,24 @@ final playbackLoadingProvider = Provider<bool>((ref) {
|
||||
/// Transport state for one media item. Non-current Library cells only watch
|
||||
/// the current media ID; the active cell additionally follows play/loading so
|
||||
/// a transport toggle does not rebuild the entire Library grid.
|
||||
final mediaItemPlaybackUiProvider =
|
||||
Provider.family<({bool isCurrent, bool isPlaying, bool isLoading}), String>(
|
||||
(ref, mediaId) {
|
||||
final currentId = ref.watch(
|
||||
currentMediaItemProvider.select((state) => state.value?.id),
|
||||
);
|
||||
if (mediaId.isEmpty || currentId != mediaId) {
|
||||
return (isCurrent: false, isPlaying: false, isLoading: false);
|
||||
}
|
||||
return (
|
||||
isCurrent: true,
|
||||
isPlaying: ref.watch(playbackPlayingProvider),
|
||||
isLoading: ref.watch(playbackLoadingProvider),
|
||||
);
|
||||
},
|
||||
);
|
||||
/// Release each cell's subscription when it scrolls out of the Library.
|
||||
final mediaItemPlaybackUiProvider = Provider.autoDispose
|
||||
.family<({bool isCurrent, bool isPlaying, bool isLoading}), String>((
|
||||
ref,
|
||||
mediaId,
|
||||
) {
|
||||
final currentId = ref.watch(
|
||||
currentMediaItemProvider.select((state) => state.value?.id),
|
||||
);
|
||||
if (mediaId.isEmpty || currentId != mediaId) {
|
||||
return (isCurrent: false, isPlaying: false, isLoading: false);
|
||||
}
|
||||
return (
|
||||
isCurrent: true,
|
||||
isPlaying: ref.watch(playbackPlayingProvider),
|
||||
isLoading: ref.watch(playbackLoadingProvider),
|
||||
);
|
||||
});
|
||||
|
||||
final playQueueProvider = StreamProvider<List<MediaItem>>((ref) {
|
||||
return musicPlayerQueueEvents();
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:audio_service/audio_service.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
@@ -35,4 +37,70 @@ void main() {
|
||||
isLoading: false,
|
||||
));
|
||||
});
|
||||
|
||||
test(
|
||||
'Library releases playback state for cells that leave the viewport',
|
||||
() async {
|
||||
final mediaItems = StreamController<MediaItem?>();
|
||||
addTearDown(mediaItems.close);
|
||||
final container = ProviderContainer(
|
||||
overrides: [
|
||||
currentMediaItemProvider.overrideWith((ref) => mediaItems.stream),
|
||||
playbackPlayingProvider.overrideWith((ref) => true),
|
||||
playbackLoadingProvider.overrideWith((ref) => false),
|
||||
],
|
||||
);
|
||||
addTearDown(container.dispose);
|
||||
final visibleProvider = mediaItemPlaybackUiProvider('visible-track');
|
||||
final visibleSubscription = container.listen(visibleProvider, (_, _) {});
|
||||
addTearDown(visibleSubscription.close);
|
||||
mediaItems.add(
|
||||
const MediaItem(id: 'visible-track', title: 'Visible track'),
|
||||
);
|
||||
await container.read(currentMediaItemProvider.future);
|
||||
|
||||
for (var index = 0; index < 200; index++) {
|
||||
final provider = mediaItemPlaybackUiProvider('scrolled-track-$index');
|
||||
final subscription = container.listen(provider, (_, _) {});
|
||||
expect(container.exists(provider), isTrue);
|
||||
subscription.close();
|
||||
}
|
||||
await container.pump();
|
||||
|
||||
for (var index = 0; index < 200; index++) {
|
||||
expect(
|
||||
container.exists(
|
||||
mediaItemPlaybackUiProvider('scrolled-track-$index'),
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
}
|
||||
expect(container.exists(visibleProvider), isTrue);
|
||||
expect(visibleSubscription.read(), (
|
||||
isCurrent: true,
|
||||
isPlaying: true,
|
||||
isLoading: false,
|
||||
));
|
||||
|
||||
mediaItems.add(
|
||||
const MediaItem(id: 'scrolled-track-0', title: 'Scrolled track'),
|
||||
);
|
||||
await container.pump();
|
||||
final returningSubscription = container.listen(
|
||||
mediaItemPlaybackUiProvider('scrolled-track-0'),
|
||||
(_, _) {},
|
||||
);
|
||||
addTearDown(returningSubscription.close);
|
||||
expect(returningSubscription.read(), (
|
||||
isCurrent: true,
|
||||
isPlaying: true,
|
||||
isLoading: false,
|
||||
));
|
||||
expect(visibleSubscription.read(), (
|
||||
isCurrent: false,
|
||||
isPlaying: false,
|
||||
isLoading: false,
|
||||
));
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user