perf(media): optimize YouTube metadata loading

This commit is contained in:
NimBold
2026-06-18 14:35:34 +03:30
parent c548cb4fe0
commit c90ae8bcc3
4 changed files with 232 additions and 21 deletions
+3 -2
View File
@@ -6,6 +6,7 @@ import { open } from '@tauri-apps/plugin-dialog';
import { invokeCommand as invoke } from '../ipc';
import { DuplicateResolutionModal, DuplicateConflict } from './DuplicateResolutionModal';
import { categoryForFileName, fileNameFromUrl, isMediaUrl } from '../utils/downloads';
import { fetchMediaMetadataDeduped } from '../utils/mediaMetadata';
interface RawMediaFormat {
format_id?: string;
@@ -371,8 +372,8 @@ export const AddDownloadsModal = () => {
}
}
const mediaData = await invoke('fetch_media_metadata', {
url,
const mediaData = await fetchMediaMetadataDeduped({
url,
cookieBrowser: browserArg,
username: login?.username || null,
password: keychainPassword
+2 -1
View File
@@ -12,6 +12,7 @@ import type { Queue } from '../bindings/Queue';
import type { MediaMetadata } from '../bindings/MediaMetadata';
import { useSettingsStore } from './useSettingsStore';
import { isActiveDownloadStatus, redactDownloadForPersistence } from '../utils/downloads';
import { fetchMediaMetadataDeduped } from '../utils/mediaMetadata';
export type { DownloadCategory } from '../utils/downloads';
@@ -190,7 +191,7 @@ export const useDownloadStore = create<DownloadState>((set, get) => ({
set({ isParsing: true, parsingError: null, activeMetadata: null, activeMetadataUrl: url });
try {
const settings = useSettingsStore.getState();
const metadata = await invoke('fetch_media_metadata', {
const metadata = await fetchMediaMetadataDeduped({
url,
cookieBrowser: settings.mediaCookieSource === 'none' ? null : settings.mediaCookieSource,
username: null,
+27
View File
@@ -0,0 +1,27 @@
import { invokeCommand as invoke } from '../ipc';
import type { MediaMetadata } from '../bindings/MediaMetadata';
type FetchMediaMetadataArgs = {
url: string;
cookieBrowser: string | null;
username: string | null;
password: string | null;
};
const inFlightMediaMetadata = new Map<string, Promise<MediaMetadata>>();
const metadataKey = (args: FetchMediaMetadataArgs) =>
JSON.stringify([args.url, args.cookieBrowser, args.username, args.password]);
export const fetchMediaMetadataDeduped = (args: FetchMediaMetadataArgs): Promise<MediaMetadata> => {
const key = metadataKey(args);
const existing = inFlightMediaMetadata.get(key);
if (existing) return existing;
const request = invoke('fetch_media_metadata', args)
.finally(() => {
inFlightMediaMetadata.delete(key);
});
inFlightMediaMetadata.set(key, request);
return request;
};