mirror of
https://github.com/GiriNeko/YesPlayMusic.git
synced 2025-12-18 06:07:48 +00:00
feat: updates
This commit is contained in:
parent
a1b0bcf4d3
commit
884f3df41a
198 changed files with 4572 additions and 5336 deletions
|
|
@ -13,20 +13,32 @@ const fetch = (params: FetchPlaylistParams) => {
|
|||
return fetchPlaylist(params)
|
||||
}
|
||||
|
||||
export const fetchFromCache = (id: number): FetchPlaylistResponse | undefined =>
|
||||
window.ipcRenderer?.sendSync(IpcChannels.GetApiCacheSync, {
|
||||
export const fetchFromCache = async (
|
||||
params: FetchPlaylistParams
|
||||
): Promise<FetchPlaylistResponse | undefined> =>
|
||||
window.ipcRenderer?.invoke(IpcChannels.GetApiCache, {
|
||||
api: APIs.Playlist,
|
||||
query: { id },
|
||||
query: params,
|
||||
})
|
||||
|
||||
export default function usePlaylist(params: FetchPlaylistParams) {
|
||||
const key = [PlaylistApiNames.FetchPlaylist, params]
|
||||
return useQuery(
|
||||
[PlaylistApiNames.FetchPlaylist, params],
|
||||
() => fetch(params),
|
||||
key,
|
||||
async () => {
|
||||
// fetch from cache as placeholder
|
||||
fetchFromCache(params).then(cache => {
|
||||
const existsQueryData = reactQueryClient.getQueryData(key)
|
||||
if (!existsQueryData && cache) {
|
||||
reactQueryClient.setQueryData(key, cache)
|
||||
}
|
||||
})
|
||||
|
||||
return fetch(params)
|
||||
},
|
||||
{
|
||||
enabled: !!(params.id && params.id > 0 && !isNaN(Number(params.id))),
|
||||
refetchOnWindowFocus: true,
|
||||
placeholderData: () => fetchFromCache(params.id),
|
||||
}
|
||||
)
|
||||
}
|
||||
|
|
@ -42,7 +54,7 @@ export function fetchPlaylistWithReactQuery(params: FetchPlaylistParams) {
|
|||
}
|
||||
|
||||
export async function prefetchPlaylist(params: FetchPlaylistParams) {
|
||||
if (fetchFromCache(params.id)) return
|
||||
if (await fetchFromCache(params)) return
|
||||
await reactQueryClient.prefetchQuery(
|
||||
[PlaylistApiNames.FetchPlaylist, params],
|
||||
() => fetch(params),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue