From 592ca7881e6270a89ff9aeeed5d65d9d1fea1896 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Sat, 28 Jun 2025 00:43:41 +0200 Subject: [PATCH] Update download status query cache on external download enqueue Downloads that got enqueued from outside the webui never got added to the download status query. To be able to see them, a page refresh was required. --- src/lib/requests/RequestManager.ts | 45 +++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/lib/requests/RequestManager.ts b/src/lib/requests/RequestManager.ts index bc94632f..ff888ae7 100644 --- a/src/lib/requests/RequestManager.ts +++ b/src/lib/requests/RequestManager.ts @@ -211,6 +211,7 @@ import { UpdateLibraryMutationVariables, GetExtensionQuery, GetExtensionQueryVariables, + DownloaderState, } from '@/lib/graphql/generated/graphql.ts'; import { GET_GLOBAL_METADATAS } from '@/lib/graphql/queries/GlobalMetadataQuery.ts'; import { DELETE_GLOBAL_METADATA, SET_GLOBAL_METADATA } from '@/lib/graphql/mutations/GlobalMetadataMutation.ts'; @@ -2984,8 +2985,50 @@ export class RequestManager { }); } + const downloadStatusQueryCache = cache.readQuery({ + query: GET_DOWNLOAD_STATUS, + }); + + const downloadsToAdd = downloadChanged?.updates.filter((update) => { + const removeDownload = [DownloadUpdateType.Dequeued, DownloadUpdateType.Finished].includes( + update.type, + ); + if (removeDownload) { + return false; + } + + const isAlreadyKnown = downloadStatusQueryCache?.downloadStatus.queue.some( + (download) => download.chapter.id === update.download.chapter.id, + ); + + return !isAlreadyKnown; + }); + + if (downloadsToAdd?.length) { + cache.writeQuery({ + query: GET_DOWNLOAD_STATUS, + data: { + ...downloadStatusQueryCache, + downloadStatus: { + __typename: 'DownloadStatus', + state: + downloadChanged?.state ?? + downloadStatusQueryCache?.downloadStatus.state ?? + DownloaderState.Stopped, + queue: [ + ...(downloadStatusQueryCache?.downloadStatus?.queue ?? []), + ...(downloadsToAdd?.map((update) => update.download) ?? []), + ], + }, + }, + }); + } + downloadChanged?.updates.forEach((update) => { - if (![DownloadUpdateType.Dequeued, DownloadUpdateType.Finished].includes(update.type)) { + const removeDownload = [DownloadUpdateType.Dequeued, DownloadUpdateType.Finished].includes( + update.type, + ); + if (!removeDownload) { return; }