diff --git a/src/services/api.service.ts b/src/services/api.service.ts index 21ae29788..1c7b7353d 100644 --- a/src/services/api.service.ts +++ b/src/services/api.service.ts @@ -57,11 +57,11 @@ export const getMediaById = (id: string): Promise => { */ export const getMediaByIds = async (ids: string[]): Promise => { // @todo this should be updated when it will become possible to request multiple media items in a single request - const responses = await Promise.all(ids.map((id) => getMediaById(id))); + const responses = await Promise.allSettled(ids.map((id) => getMediaById(id))); function notEmpty(value: Value | null | undefined): value is Value { return value !== null && value !== undefined; } - return responses.filter(notEmpty); + return responses.map((result) => (result.status === 'fulfilled' ? result.value : null)).filter(notEmpty); }; diff --git a/src/stores/WatchHistoryController.ts b/src/stores/WatchHistoryController.ts index 0f6331984..0e8cedd28 100644 --- a/src/stores/WatchHistoryController.ts +++ b/src/stores/WatchHistoryController.ts @@ -1,6 +1,6 @@ import { useAccountStore } from '#src/stores/AccountStore'; import * as persist from '#src/utils/persist'; -import { getMediaById } from '#src/services/api.service'; +import { getMediaByIds } from '#src/services/api.service'; import { updatePersonalShelves } from '#src/stores/AccountController'; import { useWatchHistoryStore } from '#src/stores/WatchHistoryStore'; import type { PlaylistItem } from '#types/playlist'; @@ -18,13 +18,17 @@ export const restoreWatchHistory = async () => { // Store savedItems immediately, so we can show the watch history while we fetch the mediaItems useWatchHistoryStore.setState({ watchHistory: savedItems }); - const watchHistory = await Promise.all( - savedItems.map(async (item) => { - const mediaItem = await getMediaById(item.mediaid); + const watchHistoryItems = await getMediaByIds(savedItems.map((item) => item.mediaid)); + const watchHistoryItemsDict = Object.fromEntries(watchHistoryItems.map((item) => [item.mediaid, item])); - if (mediaItem) return createWatchHistoryItem(mediaItem, { progress: item.progress, duration: item.duration }); - }), - ); + const watchHistory = savedItems.map((item) => { + if (watchHistoryItemsDict[item.mediaid]) { + return createWatchHistoryItem(watchHistoryItemsDict[item.mediaid], { + progress: item.progress, + duration: item.duration, + }); + } + }); useWatchHistoryStore.setState({ watchHistory: watchHistory.filter((item): item is WatchHistoryItem => !!item?.mediaid),