Skip to content

Commit

Permalink
use nulls and different break logic
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos committed Jan 16, 2025
1 parent d53d3bd commit f80a0fb
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions app/lib/service/youtube/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,17 @@ class _PkgOfWeekVideoFetcher {

try {
final pageTokensVisited = <String>{};
String nextPageToken = '';
String? nextPageToken;

final videos = <PkgOfWeekVideo>[];
final videoIds = <String>{};
while (videos.length < 50) {
// check visited status
if (pageTokensVisited.contains(nextPageToken)) {
break;
}
pageTokensVisited.add(nextPageToken);

// get page from cache or from Youtube API
final rs = await cache.youtubePlaylistItems(nextPageToken).get(
final rs = await cache.youtubePlaylistItems(nextPageToken ?? '').get(
() async => await youtube.playlistItems.list(
['snippet', 'contentDetails'],
playlistId: powPlaylistId,
pageToken: nextPageToken.isEmpty ? null : nextPageToken,
pageToken: nextPageToken,
),
);

Expand Down Expand Up @@ -177,8 +171,13 @@ class _PkgOfWeekVideoFetcher {
}
}

pageTokensVisited.add(nextPageToken ?? '');
// advance to next page token
nextPageToken = rs.nextPageToken ?? '';
nextPageToken = rs.nextPageToken;
if (nextPageToken == null ||
pageTokensVisited.contains(nextPageToken)) {
break;
}
}
return videos;
} finally {
Expand Down

0 comments on commit f80a0fb

Please sign in to comment.