refactor: move getWatchingTitles to AniList Durable Object

introduces caching to that method
This commit is contained in:
2025-12-06 08:08:14 -05:00
parent c24ff62b30
commit 9b5cc7ea62
4 changed files with 71 additions and 148 deletions

View File

@@ -12,6 +12,7 @@ import {
GetUpcomingTitlesQuery,
GetUserProfileQuery,
GetUserQuery,
GetWatchingTitlesQuery,
MarkEpisodeAsWatchedMutation,
MarkTitleAsWatchedMutation,
NextSeasonPopularQuery,
@@ -260,6 +261,33 @@ export class AnilistDurableObject extends DurableObject {
return data?.SaveMediaListEntry;
}
async getTitles(
userName: string,
page: number,
statusFilters: (
| "CURRENT"
| "COMPLETED"
| "PLANNING"
| "DROPPED"
| "PAUSED"
| "REPEATING"
)[],
aniListToken: string,
) {
return await this.handleCachedRequest(
`titles:${JSON.stringify({ page, statusFilters })}`,
async () => {
const data = await this.fetchFromAnilist(
GetWatchingTitlesQuery,
{ userName, page, statusFilters },
aniListToken,
);
return data?.Page;
},
60 * 60 * 1000,
);
}
// Helper to handle caching logic
async handleCachedRequest(
key: string,

View File

@@ -266,3 +266,36 @@ export const NextSeasonPopularQuery = graphql(
`,
[HomeTitleFragment],
);
export const GetWatchingTitlesQuery = graphql(
`
query GetWatchingTitles(
$userName: String!
$page: Int!
$statusFilters: [MediaListStatus!]
) {
Page(page: $page, perPage: 50) {
mediaList(
userName: $userName
type: ANIME
sort: UPDATED_TIME_DESC
status_in: $statusFilters
) {
media {
...Media
mediaListEntry {
updatedAt
}
}
}
pageInfo {
currentPage
hasNextPage
perPage
total
}
}
}
`,
[MediaFragment],
);