Skip to content

Commit

Permalink
[C-2465] Improve audius-query cache selection performance (#3687)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanjeffers authored Jun 30, 2023
1 parent 05b1940 commit dcd67b9
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 27 deletions.
2 changes: 1 addition & 1 deletion packages/common/src/api/collection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createApi } from 'audius-query'
import { ID, Kind } from 'models'
import { createApi } from 'src/audius-query/createApi'
import { Nullable } from 'utils'

const collectionApi = createApi({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { z } from 'zod'

import { createApi } from 'audius-query'
import { ID } from 'models/Identifiers'
import { createApi } from 'src/audius-query'
import { encodeHashId } from 'utils/hashIds'

const descriptionMaxLength = 128
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from './relatedArtists'
export * from './track'
export * from './collection'
export * from './user'
export * from './developer-apps'
export * from './developerApps'
2 changes: 1 addition & 1 deletion packages/common/src/api/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { combineReducers } from 'redux'

import { collectionApiReducer } from './collection'
import { developerAppsApiReducer } from './developer-apps'
import { developerAppsApiReducer } from './developerApps'
import { relatedArtistsApiReducer } from './relatedArtists'
import { trackApiReducer } from './track'
import { userApiReducer } from './user'
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/api/relatedArtists.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createApi } from 'audius-query'
import { ID } from 'models/Identifiers'
import { createApi } from 'src/audius-query/createApi'

const relatedArtistsApi = createApi({
reducerPath: 'relatedArtistsApi',
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/api/track.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createApi } from 'audius-query'
import { ID, Kind } from 'models'
import { createApi } from 'src/audius-query/createApi'
import { parseTrackRouteFromPermalink } from 'utils/stringUtils'
import { Nullable } from 'utils/typeUtils'

Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/api/user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createApi } from 'audius-query'
import { ID, Kind } from 'models'
import { createApi } from 'src/audius-query/createApi'

const userApi = createApi({
reducerPath: 'userApi',
Expand Down
31 changes: 11 additions & 20 deletions packages/common/src/audius-query/createApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { denormalize, normalize } from 'normalizr'
import { useDispatch, useSelector } from 'react-redux'
import { Dispatch } from 'redux'

import { useProxySelector } from 'hooks/useProxySelector'
import { ErrorLevel } from 'models/ErrorReporting'
import { Kind } from 'models/Kind'
import { Status } from 'models/Status'
Expand Down Expand Up @@ -143,7 +142,7 @@ const useQueryState = <Args, Data>(
reducerPath: string,
endpointName: string,
endpoint: EndpointConfig<Args, Data>
) => {
): Nullable<PerKeyState<any> & { isInitialValue?: boolean }> => {
return useSelector((state: CommonState) => {
if (!state.api[reducerPath]) {
throw new Error(
Expand Down Expand Up @@ -224,7 +223,7 @@ const useQueryState = <Args, Data>(
}
}

return { ...endpointState[key] }
return endpointState[key]
}, isEqual)
}

Expand All @@ -234,23 +233,15 @@ const useCacheData = <Args, Data>(
nonNormalizedData: any,
hookOptions?: QueryHookOptions
) => {
return useProxySelector(
(state: CommonState) => {
if (hookOptions?.shallow && !endpoint.options.kind)
return nonNormalizedData
const entityMap = selectCommonEntityMap(
state,
endpoint.options.kind,
hookOptions?.shallow
)
return denormalize(
nonNormalizedData,
apiResponseSchema,
entityMap
) as Data
},
[nonNormalizedData, apiResponseSchema, endpoint.options.kind]
)
return useSelector((state: CommonState) => {
if (hookOptions?.shallow && !endpoint.options.kind) return nonNormalizedData
const entityMap = selectCommonEntityMap(
state,
endpoint.options.kind,
hookOptions?.shallow
)
return denormalize(nonNormalizedData, apiResponseSchema, entityMap) as Data
}, isEqual)
}

const fetchData = async <Args, Data>(
Expand Down
1 change: 1 addition & 0 deletions packages/common/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"outDir": "dist",
"baseUrl": ".",
"paths": {
"audius-query": ["src/audius-query/index.ts"],
"assets/*": ["src/assets/*"],
"hooks/*": ["src/hooks/*"],
"models/*": ["src/models/*"],
Expand Down

0 comments on commit dcd67b9

Please sign in to comment.