mirror of
https://github.com/austinried/subtracks.git
synced 2026-02-10 15:02:42 +01:00
move album to cover art mapping to music
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { CacheFile, CacheFileTypeKey, CacheRequest, Song } from '@app/models/music'
|
||||
import { CacheFile, CacheItemTypeKey, CacheRequest } from '@app/models/music'
|
||||
import PromiseQueue from '@app/util/PromiseQueue'
|
||||
import produce from 'immer'
|
||||
import RNFS from 'react-native-fs'
|
||||
@@ -9,9 +9,9 @@ const imageDownloadQueue = new PromiseQueue(10)
|
||||
|
||||
export type CacheDownload = CacheFile & CacheRequest
|
||||
|
||||
export type CacheDirsByServer = Record<string, Record<CacheFileTypeKey, string>>
|
||||
export type CacheFilesByServer = Record<string, Record<CacheFileTypeKey, Record<string, CacheFile>>>
|
||||
export type CacheRequestsByServer = Record<string, Record<CacheFileTypeKey, Record<string, CacheRequest>>>
|
||||
export type CacheDirsByServer = Record<string, Record<CacheItemTypeKey, string>>
|
||||
export type CacheFilesByServer = Record<string, Record<CacheItemTypeKey, Record<string, CacheFile>>>
|
||||
export type CacheRequestsByServer = Record<string, Record<CacheItemTypeKey, Record<string, CacheRequest>>>
|
||||
|
||||
// export type CacheItemsDb = Record<
|
||||
// string,
|
||||
@@ -24,7 +24,7 @@ export type CacheRequestsByServer = Record<string, Record<CacheFileTypeKey, Reco
|
||||
// >
|
||||
|
||||
export type CacheSlice = {
|
||||
cacheItem: (key: CacheFileTypeKey, itemId: string, url: string | (() => string | Promise<string>)) => Promise<void>
|
||||
cacheItem: (key: CacheItemTypeKey, itemId: string, url: string | (() => string | Promise<string>)) => Promise<void>
|
||||
|
||||
// cache: CacheItemsDb
|
||||
cacheDirs: CacheDirsByServer
|
||||
@@ -32,19 +32,11 @@ export type CacheSlice = {
|
||||
cacheRequests: CacheRequestsByServer
|
||||
|
||||
getCoverArtPath: (coverArt: string) => Promise<string | undefined>
|
||||
|
||||
albumCoverArt: { [id: string]: string | undefined }
|
||||
albumCoverArtRequests: { [id: string]: Promise<void> }
|
||||
fetchAlbumCoverArt: (id: string) => Promise<void>
|
||||
getAlbumCoverArt: (id: string | undefined) => Promise<string | undefined>
|
||||
mapSongCoverArtFromAlbum: (songs: Song[]) => Promise<Song[]>
|
||||
}
|
||||
|
||||
export const selectCache = {
|
||||
getCoverArtPath: (store: CacheSlice) => store.getCoverArtPath,
|
||||
cacheItem: (store: CacheSlice) => store.cacheItem,
|
||||
|
||||
fetchAlbumCoverArt: (store: CacheSlice) => store.fetchAlbumCoverArt,
|
||||
getCoverArtPath: (store: CacheSlice) => store.getCoverArtPath,
|
||||
}
|
||||
|
||||
export const createCacheSlice = (set: SetState<Store>, get: GetState<Store>): CacheSlice => ({
|
||||
@@ -138,70 +130,4 @@ export const createCacheSlice = (set: SetState<Store>, get: GetState<Store>): Ca
|
||||
await get().cacheItem('coverArt', coverArt, () => client.getCoverArtUri({ id: coverArt }))
|
||||
return get().cacheFiles[activeServerId].coverArt[coverArt].path
|
||||
},
|
||||
|
||||
albumCoverArt: {},
|
||||
albumCoverArtRequests: {},
|
||||
|
||||
fetchAlbumCoverArt: async id => {
|
||||
const client = get().client
|
||||
if (!client) {
|
||||
return
|
||||
}
|
||||
|
||||
const inProgress = get().albumCoverArtRequests[id]
|
||||
if (inProgress !== undefined) {
|
||||
return await inProgress
|
||||
}
|
||||
|
||||
const promise = new Promise<void>(async resolve => {
|
||||
try {
|
||||
const response = await client.getAlbum({ id })
|
||||
set(
|
||||
produce<CacheSlice>(state => {
|
||||
state.albumCoverArt[id] = response.data.album.coverArt
|
||||
}),
|
||||
)
|
||||
} finally {
|
||||
resolve()
|
||||
}
|
||||
}).then(() => {
|
||||
set(
|
||||
produce<CacheSlice>(state => {
|
||||
delete state.albumCoverArtRequests[id]
|
||||
}),
|
||||
)
|
||||
})
|
||||
set(
|
||||
produce<CacheSlice>(state => {
|
||||
state.albumCoverArtRequests[id] = promise
|
||||
}),
|
||||
)
|
||||
|
||||
return await promise
|
||||
},
|
||||
|
||||
getAlbumCoverArt: async id => {
|
||||
if (!id) {
|
||||
return
|
||||
}
|
||||
|
||||
const existing = get().albumCoverArt[id]
|
||||
if (existing) {
|
||||
return existing
|
||||
}
|
||||
|
||||
await get().fetchAlbumCoverArt(id)
|
||||
return get().albumCoverArt[id]
|
||||
},
|
||||
|
||||
mapSongCoverArtFromAlbum: async songs => {
|
||||
const mapped: Song[] = []
|
||||
for (const s of songs) {
|
||||
mapped.push({
|
||||
...s,
|
||||
coverArt: await get().getAlbumCoverArt(s.albumId),
|
||||
})
|
||||
}
|
||||
return mapped
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user