mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-09-29 12:52:13 +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:
@@ -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