修复首页/api/favorites接口重复加载

This commit is contained in:
mtvpls
2025-12-04 23:26:52 +08:00
parent 179fc73c8f
commit d34c314636
2 changed files with 256 additions and 309 deletions
+96 -180
View File
@@ -4,7 +4,7 @@
import { ChevronRight } from 'lucide-react'; import { ChevronRight } from 'lucide-react';
import Link from 'next/link'; import Link from 'next/link';
import { Suspense, useEffect, useState } from 'react'; import { Suspense, useCallback, useEffect, useRef, useState } from 'react';
import { import {
BangumiCalendarData, BangumiCalendarData,
@@ -66,6 +66,7 @@ function HomeClient() {
}; };
const [favoriteItems, setFavoriteItems] = useState<FavoriteItem[]>([]); const [favoriteItems, setFavoriteItems] = useState<FavoriteItem[]>([]);
const favoritesFetchedRef = useRef(false);
useEffect(() => { useEffect(() => {
const fetchRecommendData = async () => { const fetchRecommendData = async () => {
@@ -109,7 +110,7 @@ function HomeClient() {
}, []); }, []);
// 处理收藏数据更新的函数 // 处理收藏数据更新的函数
const updateFavoriteItems = async (allFavorites: Record<string, any>) => { const updateFavoriteItems = useCallback(async (allFavorites: Record<string, any>) => {
const allPlayRecords = await getAllPlayRecords(); const allPlayRecords = await getAllPlayRecords();
// 根据保存时间排序(从近到远) // 根据保存时间排序(从近到远)
@@ -138,11 +139,19 @@ function HomeClient() {
} as FavoriteItem; } as FavoriteItem;
}); });
setFavoriteItems(sorted); setFavoriteItems(sorted);
}; }, []);
// 当切换到收藏夹时加载收藏数据 // 当切换到收藏夹时加载收藏数据(使用 ref 防止重复加载)
useEffect(() => { useEffect(() => {
if (activeTab !== 'favorites') return; if (activeTab !== 'favorites') {
favoritesFetchedRef.current = false;
return;
}
// 已经加载过就不再加载
if (favoritesFetchedRef.current) return;
favoritesFetchedRef.current = true;
const loadFavorites = async () => { const loadFavorites = async () => {
const allFavorites = await getAllFavorites(); const allFavorites = await getAllFavorites();
@@ -150,8 +159,12 @@ function HomeClient() {
}; };
loadFavorites(); loadFavorites();
}, [activeTab, updateFavoriteItems]);
// 监听收藏更新事件(独立的 useEffect)
useEffect(() => {
if (activeTab !== 'favorites') return;
// 监听收藏更新事件
const unsubscribe = subscribeToDataUpdates( const unsubscribe = subscribeToDataUpdates(
'favoritesUpdated', 'favoritesUpdated',
(newFavorites: Record<string, any>) => { (newFavorites: Record<string, any>) => {
@@ -160,7 +173,7 @@ function HomeClient() {
); );
return unsubscribe; return unsubscribe;
}, [activeTab]); }, [activeTab, updateFavoriteItems]);
const handleCloseAnnouncement = (announcement: string) => { const handleCloseAnnouncement = (announcement: string) => {
setShowAnnouncement(false); setShowAnnouncement(false);
@@ -248,26 +261,22 @@ function HomeClient() {
key={index} key={index}
className='min-w-[96px] w-24 sm:min-w-[180px] sm:w-44' className='min-w-[96px] w-24 sm:min-w-[180px] sm:w-44'
> >
<div className='relative aspect-[2/3] w-full overflow-hidden rounded-lg bg-gray-200 animate-pulse dark:bg-gray-800'> <div className='aspect-[2/3] bg-gray-200 dark:bg-gray-700 rounded-lg animate-pulse mb-2' />
<div className='absolute inset-0 bg-gray-300 dark:bg-gray-700'></div> <div className='h-4 bg-gray-200 dark:bg-gray-700 rounded animate-pulse w-3/4' />
</div>
<div className='mt-2 h-4 bg-gray-200 rounded animate-pulse dark:bg-gray-800'></div>
</div> </div>
)) ))
: // 显示真实数据 : hotMovies.map((movie) => (
hotMovies.map((movie, index) => (
<div <div
key={index} key={movie.id}
className='min-w-[96px] w-24 sm:min-w-[180px] sm:w-44' className='min-w-[96px] w-24 sm:min-w-[180px] sm:w-44'
> >
<VideoCard <VideoCard
from='douban' id={movie.id}
title={movie.title}
poster={movie.poster} poster={movie.poster}
douban_id={Number(movie.id)} title={movie.title}
rate={movie.rate}
year={movie.year} year={movie.year}
type='movie' type='movie'
from='douban'
/> />
</div> </div>
))} ))}
@@ -290,113 +299,33 @@ function HomeClient() {
</div> </div>
<ScrollableRow> <ScrollableRow>
{loading {loading
? // 加载状态显示灰色占位数据 ? Array.from({ length: 8 }).map((_, index) => (
Array.from({ length: 8 }).map((_, index) => (
<div <div
key={index} key={index}
className='min-w-[96px] w-24 sm:min-w-[180px] sm:w-44' className='min-w-[96px] w-24 sm:min-w-[180px] sm:w-44'
> >
<div className='relative aspect-[2/3] w-full overflow-hidden rounded-lg bg-gray-200 animate-pulse dark:bg-gray-800'> <div className='aspect-[2/3] bg-gray-200 dark:bg-gray-700 rounded-lg animate-pulse mb-2' />
<div className='absolute inset-0 bg-gray-300 dark:bg-gray-700'></div> <div className='h-4 bg-gray-200 dark:bg-gray-700 rounded animate-pulse w-3/4' />
</div>
<div className='mt-2 h-4 bg-gray-200 rounded animate-pulse dark:bg-gray-800'></div>
</div> </div>
)) ))
: // 显示真实数据 : hotTvShows.map((tvShow) => (
hotTvShows.map((show, index) => (
<div <div
key={index} key={tvShow.id}
className='min-w-[96px] w-24 sm:min-w-[180px] sm:w-44' className='min-w-[96px] w-24 sm:min-w-[180px] sm:w-44'
> >
<VideoCard <VideoCard
id={tvShow.id}
poster={tvShow.poster}
title={tvShow.title}
year={tvShow.year}
type='tv'
from='douban' from='douban'
title={show.title}
poster={show.poster}
douban_id={Number(show.id)}
rate={show.rate}
year={show.year}
/> />
</div> </div>
))} ))}
</ScrollableRow> </ScrollableRow>
</section> </section>
{/* 每日新番放送 */}
<section className='mb-8'>
<div className='mb-4 flex items-center justify-between'>
<h2 className='text-xl font-bold text-gray-800 dark:text-gray-200'>
</h2>
<Link
href='/douban?type=anime'
className='flex items-center text-sm text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200'
>
<ChevronRight className='w-4 h-4 ml-1' />
</Link>
</div>
<ScrollableRow>
{loading
? // 加载状态显示灰色占位数据
Array.from({ length: 8 }).map((_, index) => (
<div
key={index}
className='min-w-[96px] w-24 sm:min-w-[180px] sm:w-44'
>
<div className='relative aspect-[2/3] w-full overflow-hidden rounded-lg bg-gray-200 animate-pulse dark:bg-gray-800'>
<div className='absolute inset-0 bg-gray-300 dark:bg-gray-700'></div>
</div>
<div className='mt-2 h-4 bg-gray-200 rounded animate-pulse dark:bg-gray-800'></div>
</div>
))
: // 展示当前日期的番剧
(() => {
// 获取当前日期对应的星期
const today = new Date();
const weekdays = [
'Sun',
'Mon',
'Tue',
'Wed',
'Thu',
'Fri',
'Sat',
];
const currentWeekday = weekdays[today.getDay()];
// 找到当前星期对应的番剧数据
const todayAnimes =
bangumiCalendarData.find(
(item) => item.weekday.en === currentWeekday
)?.items || [];
return todayAnimes.map((anime, index) => (
<div
key={`${anime.id}-${index}`}
className='min-w-[96px] w-24 sm:min-w-[180px] sm:w-44'
>
<VideoCard
from='douban'
title={anime.name_cn || anime.name}
poster={
anime.images?.large ||
anime.images?.common ||
anime.images?.medium ||
anime.images?.small ||
anime.images?.grid ||
''
}
douban_id={anime.id}
rate={anime.rating?.score?.toFixed(1) || ''}
year={anime.air_date?.split('-')?.[0] || ''}
isBangumi={true}
/>
</div>
));
})()}
</ScrollableRow>
</section>
{/* 热门综艺 */} {/* 热门综艺 */}
<section className='mb-8'> <section className='mb-8'>
<div className='mb-4 flex items-center justify-between'> <div className='mb-4 flex items-center justify-between'>
@@ -404,7 +333,7 @@ function HomeClient() {
</h2> </h2>
<Link <Link
href='/douban?type=show' href='/douban?type=tv&category=show'
className='flex items-center text-sm text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200' className='flex items-center text-sm text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200'
> >
@@ -413,100 +342,87 @@ function HomeClient() {
</div> </div>
<ScrollableRow> <ScrollableRow>
{loading {loading
? // 加载状态显示灰色占位数据 ? Array.from({ length: 8 }).map((_, index) => (
Array.from({ length: 8 }).map((_, index) => (
<div <div
key={index} key={index}
className='min-w-[96px] w-24 sm:min-w-[180px] sm:w-44' className='min-w-[96px] w-24 sm:min-w-[180px] sm:w-44'
> >
<div className='relative aspect-[2/3] w-full overflow-hidden rounded-lg bg-gray-200 animate-pulse dark:bg-gray-800'> <div className='aspect-[2/3] bg-gray-200 dark:bg-gray-700 rounded-lg animate-pulse mb-2' />
<div className='absolute inset-0 bg-gray-300 dark:bg-gray-700'></div> <div className='h-4 bg-gray-200 dark:bg-gray-700 rounded animate-pulse w-3/4' />
</div>
<div className='mt-2 h-4 bg-gray-200 rounded animate-pulse dark:bg-gray-800'></div>
</div> </div>
)) ))
: // 显示真实数据 : hotVarietyShows.map((varietyShow) => (
hotVarietyShows.map((show, index) => (
<div <div
key={index} key={varietyShow.id}
className='min-w-[96px] w-24 sm:min-w-[180px] sm:w-44' className='min-w-[96px] w-24 sm:min-w-[180px] sm:w-44'
> >
<VideoCard <VideoCard
id={varietyShow.id}
poster={varietyShow.poster}
title={varietyShow.title}
year={varietyShow.year}
type='tv'
from='douban' from='douban'
title={show.title}
poster={show.poster}
douban_id={Number(show.id)}
rate={show.rate}
year={show.year}
/> />
</div> </div>
))} ))}
</ScrollableRow> </ScrollableRow>
</section> </section>
{/* 番剧时间表 */}
{bangumiCalendarData.length > 0 && (
<section className='mb-8'>
<div className='mb-4'>
<h2 className='text-xl font-bold text-gray-800 dark:text-gray-200'>
</h2>
</div>
{bangumiCalendarData.map((day) => (
<div key={day.weekday.en} className='mb-6'>
<h3 className='text-sm text-gray-600 dark:text-gray-400 mb-3'>
{day.weekday.en}
</h3>
<ScrollableRow>
{day.items.map((item) => (
<div
key={item.id}
className='min-w-[96px] w-24 sm:min-w-[180px] sm:w-44'
>
<VideoCard
id={String(item.id)}
poster={item.images.large}
title={item.name}
year={item.air_date || ''}
type='tv'
from='douban'
/>
</div>
))}
</ScrollableRow>
</div>
))}
</section>
)}
</> </>
)} )}
</div> </div>
</div> </div>
{announcement && showAnnouncement && (
<div {/* 公告弹窗 */}
className={`fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm dark:bg-black/70 p-4 transition-opacity duration-300 ${showAnnouncement ? '' : 'opacity-0 pointer-events-none' {showAnnouncement && (
}`} <div className='fixed inset-0 bg-black bg-opacity-50 z-50 flex items-center justify-center p-4'>
onTouchStart={(e) => { <div className='bg-white dark:bg-gray-800 rounded-lg shadow-xl max-w-md w-full p-6'>
// 如果点击的是背景区域,阻止触摸事件冒泡,防止背景滚动 <h3 className='text-lg font-semibold text-gray-900 dark:text-gray-100 mb-3'>
if (e.target === e.currentTarget) {
e.preventDefault(); </h3>
} <div className='text-gray-700 dark:text-gray-300 mb-4 whitespace-pre-wrap'>
}} {announcement}
onTouchMove={(e) => {
// 如果触摸的是背景区域,阻止触摸移动,防止背景滚动
if (e.target === e.currentTarget) {
e.preventDefault();
e.stopPropagation();
}
}}
onTouchEnd={(e) => {
// 如果触摸的是背景区域,阻止触摸结束事件,防止背景滚动
if (e.target === e.currentTarget) {
e.preventDefault();
}
}}
style={{
touchAction: 'none', // 禁用所有触摸操作
}}
>
<div
className='w-full max-w-md rounded-xl bg-white p-6 shadow-xl dark:bg-gray-900 transform transition-all duration-300 hover:shadow-2xl'
onTouchMove={(e) => {
// 允许公告内容区域正常滚动,阻止事件冒泡到外层
e.stopPropagation();
}}
style={{
touchAction: 'auto', // 允许内容区域的正常触摸操作
}}
>
<div className='flex justify-between items-start mb-4'>
<h3 className='text-2xl font-bold tracking-tight text-gray-800 dark:text-white border-b border-green-500 pb-1'>
</h3>
<button
onClick={() => handleCloseAnnouncement(announcement)}
className='text-gray-400 hover:text-gray-500 dark:text-gray-500 dark:hover:text-white transition-colors'
aria-label='关闭'
></button>
</div>
<div className='mb-6'>
<div className='relative overflow-hidden rounded-lg mb-4 bg-green-50 dark:bg-green-900/20'>
<div className='absolute inset-y-0 left-0 w-1.5 bg-green-500 dark:bg-green-400'></div>
<p className='ml-4 text-gray-600 dark:text-gray-300 leading-relaxed'>
{announcement}
</p>
</div>
</div> </div>
<button <button
onClick={() => handleCloseAnnouncement(announcement)} onClick={() => handleCloseAnnouncement(announcement || '')}
className='w-full rounded-lg bg-gradient-to-r from-green-600 to-green-700 px-4 py-3 text-white font-medium shadow-md hover:shadow-lg hover:from-green-700 hover:to-green-800 dark:from-green-600 dark:to-green-700 dark:hover:from-green-700 dark:hover:to-green-800 transition-all duration-300 transform hover:-translate-y-0.5' className='w-full px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg transition-colors'
> >
</button> </button>
</div> </div>
</div> </div>
+160 -129
View File
@@ -101,6 +101,9 @@ const SEARCH_HISTORY_LIMIT = 20;
class HybridCacheManager { class HybridCacheManager {
private static instance: HybridCacheManager; private static instance: HybridCacheManager;
// 正在进行的请求 Promise 缓存(彻底防止并发重复请求)
private pendingRequests: Map<string, Promise<any>> = new Map();
static getInstance(): HybridCacheManager { static getInstance(): HybridCacheManager {
if (!HybridCacheManager.instance) { if (!HybridCacheManager.instance) {
HybridCacheManager.instance = new HybridCacheManager(); HybridCacheManager.instance = new HybridCacheManager();
@@ -108,6 +111,31 @@ class HybridCacheManager {
return HybridCacheManager.instance; return HybridCacheManager.instance;
} }
/**
* 获取或创建请求 Promise(防止并发重复请求)
*/
getOrCreateRequest<T>(
key: string,
fetcher: () => Promise<T>
): Promise<T> {
// 如果已有正在进行的请求,直接返回
if (this.pendingRequests.has(key)) {
console.log(`[${key}] 复用进行中的请求`);
return this.pendingRequests.get(key)!;
}
console.log(`[${key}] 创建新请求`);
// 创建新请求
const promise = fetcher()
.finally(() => {
// 请求完成后清除缓存
this.pendingRequests.delete(key);
});
this.pendingRequests.set(key, promise);
return promise;
}
/** /**
* 获取当前用户名 * 获取当前用户名
*/ */
@@ -437,37 +465,40 @@ async function handleDatabaseOperationFailure(
triggerGlobalError(`数据库操作失败`); triggerGlobalError(`数据库操作失败`);
try { try {
let freshData: any; // 使用 Promise 缓存防止并发重复请求
let eventName: string; await cacheManager.getOrCreateRequest(`recovery-${dataType}`, async () => {
let freshData: any;
let eventName: string;
switch (dataType) { switch (dataType) {
case 'playRecords': case 'playRecords':
freshData = await fetchFromApi<Record<string, PlayRecord>>( freshData = await fetchFromApi<Record<string, PlayRecord>>(
`/api/playrecords` `/api/playrecords`
); );
cacheManager.cachePlayRecords(freshData); cacheManager.cachePlayRecords(freshData);
eventName = 'playRecordsUpdated'; eventName = 'playRecordsUpdated';
break; break;
case 'favorites': case 'favorites':
freshData = await fetchFromApi<Record<string, Favorite>>( freshData = await fetchFromApi<Record<string, Favorite>>(
`/api/favorites` `/api/favorites`
); );
cacheManager.cacheFavorites(freshData); cacheManager.cacheFavorites(freshData);
eventName = 'favoritesUpdated'; eventName = 'favoritesUpdated';
break; break;
case 'searchHistory': case 'searchHistory':
freshData = await fetchFromApi<string[]>(`/api/searchhistory`); freshData = await fetchFromApi<string[]>(`/api/searchhistory`);
cacheManager.cacheSearchHistory(freshData); cacheManager.cacheSearchHistory(freshData);
eventName = 'searchHistoryUpdated'; eventName = 'searchHistoryUpdated';
break; break;
} }
// 触发更新事件通知组件 // 触发更新事件通知组件
window.dispatchEvent( window.dispatchEvent(
new CustomEvent(eventName, { new CustomEvent(eventName, {
detail: freshData, detail: freshData,
}) })
); );
});
} catch (refreshErr) { } catch (refreshErr) {
console.error(`刷新${dataType}缓存失败:`, refreshErr); console.error(`刷新${dataType}缓存失败:`, refreshErr);
triggerGlobalError(`刷新${dataType}缓存失败`); triggerGlobalError(`刷新${dataType}缓存失败`);
@@ -935,6 +966,12 @@ export async function deleteSearchHistory(keyword: string): Promise<void> {
// ---------------- 收藏相关 API ---------------- // ---------------- 收藏相关 API ----------------
// 模块级别的防重复请求机制
let pendingFavoritesBackgroundRequest: Promise<void> | null = null;
let pendingFavoritesFetchRequest: Promise<Record<string, Favorite>> | null = null;
let lastFavoritesBackgroundFetchTime = 0;
const MIN_BACKGROUND_FETCH_INTERVAL = 3000; // 3秒内不重复后台请求
/** /**
* 获取全部收藏。 * 获取全部收藏。
* 数据库存储模式下使用混合缓存策略:优先返回缓存数据,后台异步同步最新数据。 * 数据库存储模式下使用混合缓存策略:优先返回缓存数据,后台异步同步最新数据。
@@ -951,39 +988,55 @@ export async function getAllFavorites(): Promise<Record<string, Favorite>> {
const cachedData = cacheManager.getCachedFavorites(); const cachedData = cacheManager.getCachedFavorites();
if (cachedData) { if (cachedData) {
// 返回缓存数据,同时后台异步更新 // 有缓存:返回缓存,后台异步刷新(带防抖和防重复)
fetchFromApi<Record<string, Favorite>>(`/api/favorites`) const now = Date.now();
.then((freshData) => { if (now - lastFavoritesBackgroundFetchTime > MIN_BACKGROUND_FETCH_INTERVAL && !pendingFavoritesBackgroundRequest) {
// 只有数据真正不同时才更新缓存 lastFavoritesBackgroundFetchTime = now;
if (JSON.stringify(cachedData) !== JSON.stringify(freshData)) {
cacheManager.cacheFavorites(freshData); pendingFavoritesBackgroundRequest = (async () => {
// 触发数据更新事件 try {
window.dispatchEvent( const freshData = await fetchFromApi<Record<string, Favorite>>(`/api/favorites`);
new CustomEvent('favoritesUpdated', { // 只有数据真正不同时才更新缓存
detail: freshData, if (JSON.stringify(cachedData) !== JSON.stringify(freshData)) {
}) cacheManager.cacheFavorites(freshData);
); // 触发数据更新事件
window.dispatchEvent(
new CustomEvent('favoritesUpdated', {
detail: freshData,
})
);
}
} catch (err) {
console.warn('后台同步收藏失败:', err);
triggerGlobalError('后台同步收藏失败');
} finally {
pendingFavoritesBackgroundRequest = null;
} }
}) })();
.catch((err) => { }
console.warn('后台同步收藏失败:', err);
triggerGlobalError('后台同步收藏失败');
});
return cachedData; return cachedData;
} else { } else {
// 缓存为空,直接从 API 获取并缓存 // 缓存:直接获取(防重复请求)
try { if (pendingFavoritesFetchRequest) {
const freshData = await fetchFromApi<Record<string, Favorite>>( return pendingFavoritesFetchRequest;
`/api/favorites`
);
cacheManager.cacheFavorites(freshData);
return freshData;
} catch (err) {
console.error('获取收藏失败:', err);
triggerGlobalError('获取收藏失败');
return {};
} }
pendingFavoritesFetchRequest = (async () => {
try {
const freshData = await fetchFromApi<Record<string, Favorite>>(`/api/favorites`);
cacheManager.cacheFavorites(freshData);
return freshData;
} catch (err) {
console.error('获取收藏失败:', err);
triggerGlobalError('获取收藏失败');
return {};
} finally {
pendingFavoritesFetchRequest = null;
}
})();
return pendingFavoritesFetchRequest;
} }
} }
@@ -1132,44 +1185,19 @@ export async function isFavorited(
): Promise<boolean> { ): Promise<boolean> {
const key = generateStorageKey(source, id); const key = generateStorageKey(source, id);
// 数据库存储模式:使用混合缓存策略(包括 redis 和 upstash // 数据库存储模式:直接从缓存读取,不触发后台刷新
// 后台刷新由 getAllFavorites() 统一管理,避免重复请求
if (STORAGE_TYPE !== 'localstorage') { if (STORAGE_TYPE !== 'localstorage') {
const cachedFavorites = cacheManager.getCachedFavorites(); const cachedFavorites = cacheManager.getCachedFavorites();
if (cachedFavorites) { if (cachedFavorites) {
// 返回缓存数据,同时后台异步更 // 直接返回缓存结果,不触发后台刷
fetchFromApi<Record<string, Favorite>>(`/api/favorites`)
.then((freshData) => {
// 只有数据真正不同时才更新缓存
if (JSON.stringify(cachedFavorites) !== JSON.stringify(freshData)) {
cacheManager.cacheFavorites(freshData);
// 触发数据更新事件
window.dispatchEvent(
new CustomEvent('favoritesUpdated', {
detail: freshData,
})
);
}
})
.catch((err) => {
console.warn('后台同步收藏失败:', err);
triggerGlobalError('后台同步收藏失败');
});
return !!cachedFavorites[key]; return !!cachedFavorites[key];
} else { } else {
// 缓存为空,直接从 API 获取并缓存 // 缓存为空时,调用 getAllFavorites() 来获取并缓存数据
try { // 这样可以复用 getAllFavorites() 中的防重复请求机制
const freshData = await fetchFromApi<Record<string, Favorite>>( const allFavorites = await getAllFavorites();
`/api/favorites` return !!allFavorites[key];
);
cacheManager.cacheFavorites(freshData);
return !!freshData[key];
} catch (err) {
console.error('检查收藏状态失败:', err);
triggerGlobalError('检查收藏状态失败');
return false;
}
} }
} }
@@ -1280,50 +1308,53 @@ export async function refreshAllCache(): Promise<void> {
if (STORAGE_TYPE === 'localstorage') return; if (STORAGE_TYPE === 'localstorage') return;
try { try {
// 并行刷新所有数据 // 使用 Promise 缓存防止并发重复刷新
const [playRecords, favorites, searchHistory, skipConfigs] = await cacheManager.getOrCreateRequest('refresh-all-cache', async () => {
await Promise.allSettled([ // 并行刷新所有数据
fetchFromApi<Record<string, PlayRecord>>(`/api/playrecords`), const [playRecords, favorites, searchHistory, skipConfigs] =
fetchFromApi<Record<string, Favorite>>(`/api/favorites`), await Promise.allSettled([
fetchFromApi<string[]>(`/api/searchhistory`), fetchFromApi<Record<string, PlayRecord>>(`/api/playrecords`),
fetchFromApi<Record<string, SkipConfig>>(`/api/skipconfigs`), fetchFromApi<Record<string, Favorite>>(`/api/favorites`),
]); fetchFromApi<string[]>(`/api/searchhistory`),
fetchFromApi<Record<string, SkipConfig>>(`/api/skipconfigs`),
]);
if (playRecords.status === 'fulfilled') { if (playRecords.status === 'fulfilled') {
cacheManager.cachePlayRecords(playRecords.value); cacheManager.cachePlayRecords(playRecords.value);
window.dispatchEvent( window.dispatchEvent(
new CustomEvent('playRecordsUpdated', { new CustomEvent('playRecordsUpdated', {
detail: playRecords.value, detail: playRecords.value,
}) })
); );
} }
if (favorites.status === 'fulfilled') { if (favorites.status === 'fulfilled') {
cacheManager.cacheFavorites(favorites.value); cacheManager.cacheFavorites(favorites.value);
window.dispatchEvent( window.dispatchEvent(
new CustomEvent('favoritesUpdated', { new CustomEvent('favoritesUpdated', {
detail: favorites.value, detail: favorites.value,
}) })
); );
} }
if (searchHistory.status === 'fulfilled') { if (searchHistory.status === 'fulfilled') {
cacheManager.cacheSearchHistory(searchHistory.value); cacheManager.cacheSearchHistory(searchHistory.value);
window.dispatchEvent( window.dispatchEvent(
new CustomEvent('searchHistoryUpdated', { new CustomEvent('searchHistoryUpdated', {
detail: searchHistory.value, detail: searchHistory.value,
}) })
); );
} }
if (skipConfigs.status === 'fulfilled') { if (skipConfigs.status === 'fulfilled') {
cacheManager.cacheSkipConfigs(skipConfigs.value); cacheManager.cacheSkipConfigs(skipConfigs.value);
window.dispatchEvent( window.dispatchEvent(
new CustomEvent('skipConfigsUpdated', { new CustomEvent('skipConfigsUpdated', {
detail: skipConfigs.value, detail: skipConfigs.value,
}) })
); );
} }
});
} catch (err) { } catch (err) {
console.error('刷新缓存失败:', err); console.error('刷新缓存失败:', err);
triggerGlobalError('刷新缓存失败'); triggerGlobalError('刷新缓存失败');