Skip to content

Commit

Permalink
[PAY-1391] ensure all collections are fetched before attempting to do…
Browse files Browse the repository at this point in the history
…wnload (#3521)
  • Loading branch information
schottra committed Jun 6, 2023
1 parent 8f45d91 commit b8277b8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CommonState } from '../commonStore'
import { CollectionType } from './types'

const getAccountCollections = (state: CommonState) => state.account.collections

export const getSavedCollectionsState = (
state: CommonState,
type: CollectionType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getContext,
savedPageSelectors
} from '@audius/common'
import { fetchAllAccountCollections } from 'common/store/saved-collections/sagas'
import moment from 'moment'
import { takeEvery, select, call, put } from 'typed-redux-saga'

Expand Down Expand Up @@ -78,6 +79,9 @@ function* downloadAllFavorites() {
}

// Add favorited collections and their tracks
// AccountCollections don't include track lists, so retrieve all the collections
// first
yield* call(fetchAllAccountCollections)
const favoritedCollections = yield* select(getAccountCollections)

for (const favoritedCollection of favoritedCollections) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { savedCollectionsActions, waitForRead } from '@audius/common'
import { call, put, takeEvery } from 'typed-redux-saga'
import {
CollectionType,
ID,
savedCollectionsActions,
savedCollectionsSelectors,
waitForRead
} from '@audius/common'
import { all, call, select, put, takeEvery } from 'typed-redux-saga'

import { retrieveCollections } from '../cache/collections/utils'

const { fetchCollections, fetchCollectionsSucceeded } = savedCollectionsActions
const { getAccountAlbums, getAccountPlaylists } = savedCollectionsSelectors

function* fetchCollectionsAsync(action: ReturnType<typeof fetchCollections>) {
const { type, ids } = action.payload
type FetchCollectionsConfig = {
type: CollectionType
ids: ID[]
}

function* fetchCollectionsAsync({ ids, type }: FetchCollectionsConfig) {
yield waitForRead()

yield* call(retrieveCollections, ids)
Expand All @@ -18,8 +29,35 @@ function* fetchCollectionsAsync(action: ReturnType<typeof fetchCollections>) {
)
}

/** Will create and wait on parallel effects to fetch full details for all saved albums and
* playlists. Note: Only use this if you really need full details (such as track
* lists) for all collections, as it may potentially fetch a lot of data.
*/
export function* fetchAllAccountCollections() {
yield waitForRead()

const { data: playlists } = yield* select(getAccountPlaylists)
const { data: albums } = yield* select(getAccountAlbums)

yield* all([
call(fetchCollectionsAsync, {
ids: albums.map(({ id }) => id),
type: 'albums'
}),
call(fetchCollectionsAsync, {
ids: playlists.map(({ id }) => id),
type: 'playlists'
})
])
}

function* watchFetchCollections() {
yield takeEvery(fetchCollections.type, fetchCollectionsAsync)
yield* takeEvery(
fetchCollections.type,
function* (action: ReturnType<typeof fetchCollections>) {
yield* fetchCollectionsAsync(action.payload)
}
)
}

export default function sagas() {
Expand Down

0 comments on commit b8277b8

Please sign in to comment.