Skip to content

Commit

Permalink
Convert common store cache js files to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Shanks committed Jan 30, 2024
1 parent 583e266 commit 9a303b5
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 129 deletions.
2 changes: 1 addition & 1 deletion packages/common/src/audius-query/createApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ const fetchData = async <Args, Data>(
omitUser: false
})
)
dispatch(addEntries(Object.keys(entities), entities))
dispatch(addEntries(entities))
} else {
data = apiData
}
Expand Down
139 changes: 69 additions & 70 deletions packages/common/src/store/cache/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @ts-nocheck

import { ID, UID } from 'models/Identifiers'
import { Kind } from 'models/Kind'
import { Status } from 'models/Status'

import { Metadata } from './types'

Expand All @@ -19,56 +18,47 @@ export const REMOVE_SUCCEEDED = 'CACHE/REMOVE_SUCCEEDED'
export const SET_EXPIRED = 'CACHE/SET_EXPIRED'
export const SET_CACHE_CONFIG = 'CACHE/SET_CONFIG'

type Entry<EntryT extends Metadata = Metadata> = {
export type Entry<EntryT extends Metadata = Metadata> = {
id: ID
uid?: UID
metadata: EntryT
timestamp?: number
}

type BaseAddAction<EntryT extends Metadata = Metadata> = {
kind: Kind
entries: Entry<EntryT>[]
// replace optionally replaces the entire entry instead of joining metadata
replace?: boolean
// persist optionally persists the cache entry to indexdb
persist?: boolean
}

export type AddAction<EntryT extends Metadata = Metadata> =
BaseAddAction<EntryT> & {
type: typeof ADD
}

/**
* Signals to add an entry to the cache.
* @param {Kind} kind
* @param {array} entries { id, uid, metadata }
* @param {boolean} replace optionally replaces the entire entry instead of joining metadata
* @param {boolean} persist optionally persists the cache entry to indexdb
*/
export const add = (kind, entries, replace = false, persist = true) => ({
export const add = (
kind: Kind,
entries: Entry[],
replace = false,
persist = true
): AddAction => ({
type: ADD,
kind,
entries,
replace,
persist
})

export type AddSuccededAction<EntryT extends Metadata = Metadata> = {
type: typeof ADD_SUCCEEDED
kind: Kind
entries: {
id: ID
uid: UID
metadata: EntryT
timestamp: number
}[]
// replace optionally replaces the entire entry instead of joining metadata
replace?: boolean
// persist optionally persists the cache entry to indexdb
persist?: boolean
}

type EntriesByKind<EntryT extends Metadata = Metadata> = {
[key: Kind]: Entry<EntryT>[]
}

export type AddEntriesAction<EntryT extends Metadata = Metadata> = {
type: typeof ADD_ENTRIES
kind: Kind[]
entriesByKind: EntriesByKind<EntryT>
// replace optionally replaces the entire entry instead of joining metadata
replace?: boolean
// persist optionally persists the cache entry to indexdb
persist?: boolean
}
export type AddSuccededAction<EntryT extends Metadata = Metadata> =
BaseAddAction<EntryT> & {
type: typeof ADD_SUCCEEDED
}

/**
* Adds entries to the cache.
Expand All @@ -78,43 +68,55 @@ export const addSucceeded = ({
entries,
replace = false,
persist = true
}: AddSuccededAction) => ({
}: BaseAddAction): AddSuccededAction => ({
type: ADD_SUCCEEDED,
kind,
entries,
replace,
persist
})

export type EntriesByKind<EntryT extends Metadata = Metadata> = {
[key in Kind]?: Entry<EntryT>[]
}

export type AddEntriesAction<EntryT extends Metadata = Metadata> = {
type: typeof ADD_ENTRIES
entriesByKind: EntriesByKind<EntryT>
// replace optionally replaces the entire entry instead of joining metadata
replace?: boolean
// persist optionally persists the cache entry to indexdb
persist?: boolean
}

/**
* Signals to add an entries of multiple kinds to the cache.
* @param {Kind} kind
* @param {array} entries { id, uid, metadata }
* @param {boolean} replace optionally replaces the entire entry instead of joining metadata
* @param {boolean} persist optionally persists the cache entry to indexdb
*/
export const addEntries = (
kind,
entriesByKind,
entriesByKind: EntriesByKind,
replace = false,
persist = true
): AddEntriesAction => ({
type: ADD_ENTRIES,
kind,
entriesByKind,
replace,
persist
})

type SubscriptionInfo = SubscriberInfo & {
kind: Kind
}

/**
* Updates an entry in the cache. Can also add transitive cache subscriptions.
* E.g. if a collection references multiple tracks, the collection should be subscribed to those
* tracks.
* @param {Kind} kind
* @param {array} entries { id, metadata }
* @param {?array} subscriptions { id, kind, uids }
*/
export const update = (kind, entries, subscriptions = []) => ({
export const update = (
kind: Kind,
entries: Entry[],
subscriptions: SubscriptionInfo[] = []
) => ({
type: UPDATE,
kind,
entries,
Expand All @@ -126,10 +128,8 @@ export const update = (kind, entries, subscriptions = []) => ({
* Only numeric fields should be part of the update if so, e.g.
* entries = [{ id: 2, metadata: { followee_count: 1 } }]
* would yield an update to the cached followee_count of id 2 by 1.
* @param {Kind} kind
* @param {array} entries { id, metadata }
*/
export const increment = (kind, entries) => ({
export const increment = (kind: Kind, entries: Entry[]) => ({
type: INCREMENT,
kind,
entries
Expand All @@ -138,65 +138,64 @@ export const increment = (kind, entries) => ({
/**
* Sets the status of an entry from the cache as to be removed. The
* entries actually get removed by the removeSucceeded action
* @param {Kind} kind
* @param {array} ids
*/
export const remove = (kind, ids) => ({
export const remove = (kind: Kind, ids: ID[]) => ({
type: REMOVE,
kind,
ids
})

/**
* Removes entries from the cache
* @param {Kind} kind
* @param {array} ids
*/
export const removeSucceeded = (kind, ids) => ({
export const removeSucceeded = (kind: Kind, ids: ID[]) => ({
type: REMOVE_SUCCEEDED,
kind,
ids
})

/**
* Sets the status of N entries.
* @param {Kind} kind
* @param {array} statuses {id, status}
*/
export const setStatus = (kind, statuses) => ({
export const setStatus = (
kind: Kind,
statuses: { id: ID; status: Status }[]
) => ({
type: SET_STATUS,
kind,
statuses
})

type SubscriberInfo = {
uid: UID
id?: string | number
}

/**
* Subscribes uids to ids in the cache.
* @param {Kind} kind
* @param {array} subscribers { uid, id }
*/
export const subscribe = (kind, subscribers) => ({
export const subscribe = (kind: Kind, subscribers: SubscriberInfo[]) => ({
type: SUBSCRIBE,
kind,
subscribers
})

/**
* Unsubscribes a uid from an id in the cache. Automatically clears transitive subscriptions.
* @param {Kind} kind
* @param {array} unsubscribers { uid, id? } if id is not provided, looks it up in the cache uids
*/
export const unsubscribe = (kind, unsubscribers) => ({
export const unsubscribe = (kind: Kind, unsubscribers: SubscriberInfo[]) => ({
type: UNSUBSCRIBE,
kind,
unsubscribers
})

/**
* Realizes an unsubscription action and potentially removes the entry from the cache.
* @param {Kind} kind
* @param {array} unsubscribers { uid, id? }
*/
export const unsubscribeSucceeded = (kind, unsubscribers) => ({
export const unsubscribeSucceeded = (
kind: Kind,
unsubscribers: SubscriberInfo[]
) => ({
type: UNSUBSCRIBE_SUCCEEDED,
kind,
unsubscribers
Expand All @@ -207,7 +206,7 @@ export const unsubscribeSucceeded = (kind, unsubscribers) => ({
* @param {Kind} kind
* @param {string} id
*/
export const setExpired = (kind, id) => ({
export const setExpired = (kind: Kind, id: ID) => ({
type: SET_EXPIRED,
kind,
id
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/store/cache/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ const actionsMap = {
},
[ADD_ENTRIES](state: CacheState, action: AddEntriesAction, kind: Kind) {
const { entriesByKind, replace } = action
const matchingEntries = entriesByKind[kind]
const matchingEntries = entriesByKind[kind] ?? {}
const cacheableEntries = Object.entries(matchingEntries).map(
([id, entry]) => ({
id,
Expand Down
12 changes: 5 additions & 7 deletions packages/common/src/store/cache/tracks/actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// @ts-nocheck
// TODO(nkang) - convert to TS
import { ID } from '../../../models'
import { ID, SquareSizes, TrackMetadata } from '../../../models'
export const EDIT_TRACK = 'CACHE/TRACKS/EDIT_TRACK'
export const EDIT_TRACK_SUCCEEDED = 'CACHE/TRACKS/EDIT_TRACK_SUCCEEDED'
export const EDIT_TRACK_FAILED = 'CACHE/TRACKS/EDIT_TRACK_FAILED'
Expand All @@ -13,7 +11,7 @@ export const SET_PERMALINK = 'CACHE/TRACKS/SET_PERMALINK'

export const FETCH_COVER_ART = 'CACHE/TRACKS/FETCH_COVER_ART'

export function editTrack(trackId, formFields) {
export function editTrack(trackId: ID, formFields: TrackMetadata) {
return { type: EDIT_TRACK, trackId, formFields }
}

Expand All @@ -25,11 +23,11 @@ export function editTrackFailed() {
return { type: EDIT_TRACK_FAILED }
}

export function deleteTrack(trackId) {
export function deleteTrack(trackId: ID) {
return { type: DELETE_TRACK, trackId }
}

export function deleteTrackSucceeded(trackId) {
export function deleteTrackSucceeded(trackId: ID) {
return { type: DELETE_TRACK_SUCCEEDED, trackId }
}

Expand All @@ -41,6 +39,6 @@ export function setPermalink(permalink: string, trackId: ID) {
return { type: SET_PERMALINK, permalink, trackId }
}

export function fetchCoverArt(trackId, size) {
export function fetchCoverArt(trackId: ID, size: SquareSizes) {
return { type: FETCH_COVER_ART, trackId, size }
}
2 changes: 1 addition & 1 deletion packages/common/src/store/cache/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type Metadata = {
blocknumber?: number
local?: boolean
}
} & Record<string, any>
8 changes: 3 additions & 5 deletions packages/common/src/store/cache/users/actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// @ts-nocheck

import { ID } from 'models'
import { ID, SquareSizes } from 'models'

export const REMOVE_BY_HANDLE = 'CACHE/USERS/REMOVE_BY_HANDLE'

Expand All @@ -14,11 +12,11 @@ export const removeByHandle = (handle: string) => ({
handle
})

export function fetchProfilePicture(userId, size) {
export function fetchProfilePicture(userId: ID, size: SquareSizes) {
return { type: FETCH_PROFILE_PICTURE, userId, size }
}

export function fetchCoverPhoto(userId, size) {
export function fetchCoverPhoto(userId: ID, size: SquareSizes) {
return { type: FETCH_COVER_PHOTO, userId, size }
}

Expand Down
14 changes: 4 additions & 10 deletions packages/common/src/store/gated-content/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,16 +352,10 @@ function* updateGatedTrackAccess(

const allTracks = {
...cachedTracks,
...newlyUpdatedTracks.reduce(
(
acc: { [id: ID]: Track },
curr: { id: number; uid: string; metadata: Track }
) => {
acc[curr.metadata.track_id] = curr.metadata
return acc
},
[]
)
...newlyUpdatedTracks.reduce((acc: { [id: ID]: Track }, curr: any) => {
acc[curr.metadata.track_id] = curr.metadata
return acc
}, [])
}

// Handle newly loading special access tracks that should have a signature but do not yet.
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/store/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ import smartCollection from './pages/smart-collection/slice'
import tokenDashboardSlice from './pages/token-dashboard/slice'
import track from './pages/track/reducer'
import TrackPageState from './pages/track/types'
import trendingPlaylists from './pages/trending-playlists/slice'
import trendingUnderground from './pages/trending-underground/slice'
import trending from './pages/trending/reducer'
import { TrendingPageState } from './pages/trending/types'
import trendingPlaylists from './pages/trending-playlists/slice'
import trendingUnderground from './pages/trending-underground/slice'
import { PlaybackPositionState } from './playback-position'
import playbackPosition from './playback-position/slice'
import player, { PlayerState } from './player/slice'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function* addWalletToUser(

const updatedCID = yield* call(getAccountMetadataCID)

if (updatedCID) {
if (updatedCID && accountUserId) {
yield* put(
cacheActions.update(Kind.USERS, [
{
Expand Down
Loading

0 comments on commit 9a303b5

Please sign in to comment.