63 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-05-12 02:45:43 +08:00
import { fetchAudioSource, fetchTracks } from '@/web/api/track'
import type {} from '@/web/api/track'
import reactQueryClient from '@/web/utils/reactQueryClient'
2022-04-16 21:14:03 +08:00
import { IpcChannels } from '@/shared/IpcChannels'
2022-04-09 00:28:37 +08:00
import {
2022-03-19 17:03:29 +08:00
FetchAudioSourceParams,
FetchTracksParams,
FetchTracksResponse,
2022-04-16 21:14:03 +08:00
TrackApiNames,
} from '@/shared/api/Track'
import { APIs } from '@/shared/CacheAPIs'
2022-05-12 02:45:43 +08:00
import { useQuery } from 'react-query'
2022-03-13 14:40:38 +08:00
export default function useTracks(params: FetchTracksParams) {
return useQuery(
[TrackApiNames.FetchTracks, params],
2022-03-13 14:40:38 +08:00
() => {
return fetchTracks(params)
},
{
enabled: params.ids.length !== 0,
refetchInterval: false,
staleTime: Infinity,
2022-03-30 00:53:05 +08:00
initialData: (): FetchTracksResponse | undefined =>
2022-04-09 00:28:37 +08:00
window.ipcRenderer?.sendSync(IpcChannels.GetApiCacheSync, {
2022-04-16 21:14:03 +08:00
api: APIs.Track,
2022-03-30 00:53:05 +08:00
query: {
ids: params.ids.join(','),
},
}),
2022-03-13 14:40:38 +08:00
}
)
}
export function fetchTracksWithReactQuery(params: FetchTracksParams) {
return reactQueryClient.fetchQuery(
[TrackApiNames.FetchTracks, params],
2022-03-13 14:40:38 +08:00
() => {
return fetchTracks(params)
},
{
2022-04-02 16:54:37 +08:00
retry: 4,
retryDelay: (retryCount: number) => {
return retryCount * 500
},
2022-03-13 14:40:38 +08:00
staleTime: 86400000,
}
)
}
export function fetchAudioSourceWithReactQuery(params: FetchAudioSourceParams) {
return reactQueryClient.fetchQuery(
[TrackApiNames.FetchAudioSource, params],
2022-03-13 14:40:38 +08:00
() => {
return fetchAudioSource(params)
},
{
retry: 3,
2022-03-19 17:03:29 +08:00
staleTime: 0, // TODO: Web版1小时缓存
2022-03-13 14:40:38 +08:00
}
)
}