Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
[C-2095] Improve download cancel (#2833)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanjeffers authored Feb 14, 2023
1 parent 9198447 commit 52df798
Show file tree
Hide file tree
Showing 27 changed files with 308 additions and 310 deletions.
42 changes: 32 additions & 10 deletions packages/common/src/services/audius-api-client/AudiusAPIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type GetTrackArgs = {
urlTitle: string
handle: string
}
abortOnUnreachable?: boolean
}

type GetTracksArgs = {
Expand Down Expand Up @@ -228,6 +229,7 @@ type GetPlaylistFavoriteUsersArgs = {
type GetUserArgs = {
userId: ID
currentUserId: Nullable<ID>
abortOnUnreachable?: boolean
}

type GetUserByHandleArgs = {
Expand Down Expand Up @@ -281,11 +283,13 @@ type GetUserRepostsByHandleArgs = {
type GetCollectionMetadataArgs = {
collectionId: ID
currentUserId: ID
abortOnUnreachable?: boolean
}

type GetPlaylistArgs = {
playlistId: ID
currentUserId: Nullable<ID>
abortOnUnreachable?: boolean
}

type GetPlaylistByPermalinkArgs = {
Expand Down Expand Up @@ -844,7 +848,7 @@ export class AudiusAPIClient {
}

async getTrack(
{ id, currentUserId, unlistedArgs }: GetTrackArgs,
{ id, currentUserId, unlistedArgs, abortOnUnreachable }: GetTrackArgs,
retry = true
) {
const encodedTrackId = this._encodeOrThrow(id)
Expand All @@ -863,7 +867,10 @@ export class AudiusAPIClient {
await this._getResponse(
FULL_ENDPOINT_MAP.getTrack(encodedTrackId),
args,
retry
retry,
undefined,
undefined,
abortOnUnreachable
)

if (!trackResponse) return null
Expand Down Expand Up @@ -983,7 +990,7 @@ export class AudiusAPIClient {
return tracks
}

async getUser({ userId, currentUserId }: GetUserArgs) {
async getUser({ userId, currentUserId, abortOnUnreachable }: GetUserArgs) {
const encodedUserId = this._encodeOrThrow(userId)
const encodedCurrentUserId = encodeHashId(currentUserId)
this._assertInitialized()
Expand All @@ -993,7 +1000,11 @@ export class AudiusAPIClient {

const response: Nullable<APIResponse<APIUser[]>> = await this._getResponse(
FULL_ENDPOINT_MAP.getUser(encodedUserId),
params
params,
undefined,
undefined,
undefined,
abortOnUnreachable
)

if (!response) return []
Expand Down Expand Up @@ -1205,7 +1216,8 @@ export class AudiusAPIClient {

async getCollectionMetadata({
collectionId,
currentUserId
currentUserId,
abortOnUnreachable
}: GetCollectionMetadataArgs) {
this._assertInitialized()

Expand All @@ -1216,12 +1228,17 @@ export class AudiusAPIClient {
params,
false,
PathType.RootPath,
headers
headers,
abortOnUnreachable
)
return response?.data?.[0]
}

async getPlaylist({ playlistId, currentUserId }: GetPlaylistArgs) {
async getPlaylist({
playlistId,
currentUserId,
abortOnUnreachable
}: GetPlaylistArgs) {
this._assertInitialized()
const encodedCurrentUserId = encodeHashId(currentUserId)
const encodedPlaylistId = this._encodeOrThrow(playlistId)
Expand All @@ -1232,7 +1249,11 @@ export class AudiusAPIClient {
const response: Nullable<APIResponse<APIPlaylist[]>> =
await this._getResponse(
FULL_ENDPOINT_MAP.getPlaylist(encodedPlaylistId),
params
params,
undefined,
undefined,
undefined,
abortOnUnreachable
)

if (!response) return []
Expand Down Expand Up @@ -1770,13 +1791,14 @@ export class AudiusAPIClient {
retry = true,
pathType: PathType = PathType.VersionFullPath,
headers?: { [key: string]: string },
splitArrayParams = false
splitArrayParams = false,
abortOnUnreachable = true
): Promise<Nullable<T>> {
if (this.initializationState.state !== 'initialized')
throw new Error('_getResponse called uninitialized')

// If not reachable, abort
if (!this.isReachable) {
if (!this.isReachable && abortOnUnreachable) {
console.debug(`APIClient: Not reachable, aborting request`)
return null
}
Expand Down
4 changes: 2 additions & 2 deletions packages/mobile/src/components/audio/Audio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ import { useFeatureFlag } from 'app/hooks/useRemoteConfig'
import { apiClient } from 'app/services/audius-api-client'
import { audiusBackendInstance } from 'app/services/audius-backend-instance'
import {
DOWNLOAD_REASON_FAVORITES,
getLocalAudioPath,
getLocalTrackCoverArtPath
} from 'app/services/offline-downloader'
import { DOWNLOAD_REASON_FAVORITES } from 'app/store/offline-downloads/constants'
import {
getOfflineTrackStatus,
getIsCollectionMarkedForDownload
Expand Down Expand Up @@ -380,7 +380,7 @@ export const Audio = () => {
const newTrackData = await Promise.all(
newQueueTracks.map(async (track) => {
const trackOwner = queueTrackOwnersMap[track.owner_id]
const trackId = track.track_id.toString()
const trackId = track.track_id
const offlineTrackAvailable =
trackId &&
isOfflineModeEnabled &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import { DownloadStatusIndicator } from 'app/components/offline-downloads/Downlo
import { useIsOfflineModeEnabled } from 'app/hooks/useIsOfflineModeEnabled'
import { useProxySelector } from 'app/hooks/useProxySelector'
import { useThrottledCallback } from 'app/hooks/useThrottledCallback'
import { DOWNLOAD_REASON_FAVORITES } from 'app/services/offline-downloader'
import { setVisibility } from 'app/store/drawers/slice'
import { DOWNLOAD_REASON_FAVORITES } from 'app/store/offline-downloads/constants'
import { getIsCollectionMarkedForDownload } from 'app/store/offline-downloads/selectors'
import {
OfflineDownloadStatus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { useDispatch, useSelector } from 'react-redux'

import { Switch } from 'app/components/core'
import { useThrottledCallback } from 'app/hooks/useThrottledCallback'
import { DOWNLOAD_REASON_FAVORITES } from 'app/services/offline-downloader'
import { setVisibility } from 'app/store/drawers/slice'
import { DOWNLOAD_REASON_FAVORITES } from 'app/store/offline-downloads/constants'
import { getCollectionDownloadStatus } from 'app/store/offline-downloads/selectors'
import { requestDownloadAllFavorites } from 'app/store/offline-downloads/slice'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useSelector } from 'react-redux'

import { Text } from 'app/components/core'
import { ProgressBar } from 'app/components/progress-bar'
import { DOWNLOAD_REASON_FAVORITES } from 'app/services/offline-downloader'
import { DOWNLOAD_REASON_FAVORITES } from 'app/store/offline-downloads/constants'
import {
getIsCollectionMarkedForDownload,
getOfflineTrackStatus
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DownloadStatusIndicator } from 'app/components/offline-downloads'
import { useProxySelector } from 'app/hooks/useProxySelector'
import { DOWNLOAD_REASON_FAVORITES } from 'app/services/offline-downloader'
import type { AppState } from 'app/store'
import { DOWNLOAD_REASON_FAVORITES } from 'app/store/offline-downloads/constants'
import { getOfflineTrackStatus } from 'app/store/offline-downloads/selectors'
import { OfflineDownloadStatus } from 'app/store/offline-downloads/slice'
import { makeStyles } from 'app/styles'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { useDispatch, useSelector } from 'react-redux'

import { useIsOfflineModeEnabled } from 'app/hooks/useIsOfflineModeEnabled'
import { useReachabilityEffect } from 'app/hooks/useReachabilityEffect'
import { DOWNLOAD_REASON_FAVORITES } from 'app/services/offline-downloader'
import { store } from 'app/store'
import { DOWNLOAD_REASON_FAVORITES } from 'app/store/offline-downloads/constants'
import { getOfflineTracks } from 'app/store/offline-downloads/selectors'

const { getSavedTracksLineup } = savedPageSelectors
Expand Down
1 change: 0 additions & 1 deletion packages/mobile/src/services/offline-downloader/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './offline-downloader'
export * from './offline-storage'
124 changes: 0 additions & 124 deletions packages/mobile/src/services/offline-downloader/offline-downloader.ts

This file was deleted.

Loading

0 comments on commit 52df798

Please sign in to comment.