This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
retrieveTrending.ts
64 lines (55 loc) · 1.94 KB
/
retrieveTrending.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { setLastFetchedTrendingGenre } from 'containers/discover-page/store/actions'
import { getTrendingEntries } from 'containers/discover-page/store/lineups/trending/selectors'
import {
getLastFetchedTrendingGenre,
getTrendingGenre
} from 'containers/discover-page/store/selectors'
import { ID } from 'models/common/Identifiers'
import TimeRange from 'models/TimeRange'
import Track, { UserTrackMetadata } from 'models/Track'
import { put, select } from 'redux-saga/effects'
import apiClient from 'services/audius-api-client/AudiusAPIClient'
import { getTracks } from 'store/cache/tracks/selectors'
import { processAndCacheTracks } from 'store/cache/tracks/utils'
import { AppState } from 'store/types'
type RetrieveTrendingArgs = {
timeRange: TimeRange
genre?: string
offset: number
limit: number
currentUserId: ID | null
}
export function* retrieveTrending({
timeRange,
genre,
offset,
limit,
currentUserId
}: RetrieveTrendingArgs): Generator<any, Track[], any> {
const cachedTracks: ReturnType<ReturnType<
typeof getTrendingEntries
>> = yield select(getTrendingEntries(timeRange))
const lastGenre = yield select(getLastFetchedTrendingGenre)
yield put(setLastFetchedTrendingGenre(genre))
const useCached = lastGenre === genre && cachedTracks.length > offset + limit
if (useCached) {
const trackIds = cachedTracks.slice(offset, limit + offset).map(t => t.id)
const tracksMap: ReturnType<typeof getTracks> = yield select(
(state: AppState) => getTracks(state, { ids: trackIds })
)
const tracks = trackIds.map(id => tracksMap[id])
return tracks
}
const apiTracks: UserTrackMetadata[] = yield apiClient.getTrending({
genre,
offset,
limit,
currentUserId,
timeRange
})
const currentGenre = yield select(getTrendingGenre)
// If we changed genres, do nothing
if (currentGenre !== genre) return []
const processed: Track[] = yield processAndCacheTracks(apiTracks)
return processed
}