Skip to content

Commit

Permalink
common/store/cache sagas to typescript (#7373)
Browse files Browse the repository at this point in the history
  • Loading branch information
amendelsohn authored Jan 30, 2024
1 parent d69458f commit 72c0af4
Show file tree
Hide file tree
Showing 22 changed files with 308 additions and 235 deletions.
4 changes: 2 additions & 2 deletions packages/common/src/models/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export type Cache<T> = {
entries: { [id: number]: Cacheable<T> }
statuses: { [id: number]: Status }
uids: { [uid: string]: ID }
subscribers: { [id: number]: Set<UID> }
subscriptions: { [id: number]: Set<{ uid: UID; kind: Kind }> }
subscribers: { [id: number | string]: Set<UID> }
subscriptions: { [id: number | string]: Set<{ uid: UID; kind: Kind }> }
idsToPrune: Set<ID>
entryTTL: number
}
2 changes: 1 addition & 1 deletion packages/common/src/services/remote-config/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IntKeys, StringKeys, DoubleKeys, BooleanKeys } from './types'

const ETH_PROVIDER_URLS = process.env.REACT_APP_ETH_PROVIDER_URL || ''
const DEFAULT_ENTRY_TTL = 1 /* min */ * 60 /* seconds */ * 1000 /* ms */
export const DEFAULT_ENTRY_TTL = 1 /* min */ * 60 /* seconds */ * 1000 /* ms */
const DEFAULT_HANDLE_VERIFICATION_TIMEOUT_MILLIS = 5_000

export const MIN_USDC_PURCHASE_AMOUNT_CENTS = 100
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/store/buy-usdc/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Name } from 'models/Analytics'
import { ErrorLevel } from 'models/ErrorReporting'
import { PurchaseVendor } from 'models/PurchaseContent'
import { Status } from 'models/Status'
import { StringUSDC } from 'models/Wallet'
import {
createPaymentRouterRouteTransaction,
createRootWalletRecoveryTransaction,
Expand All @@ -31,6 +32,7 @@ import {
import { coinflowOnrampModalActions } from 'store/ui/modals/coinflow-onramp-modal'
import { setVisibility } from 'store/ui/modals/parentSlice'
import { initializeStripeModal } from 'store/ui/stripe-modal/slice'
import { setUSDCBalance } from 'store/wallet/slice'
import { waitForValue } from 'utils'

import {
Expand All @@ -46,8 +48,6 @@ import {
} from './slice'
import { BuyUSDCError, BuyUSDCErrorCode } from './types'
import { getBuyUSDCRemoteConfig, getUSDCUserBank } from './utils'
import { setUSDCBalance } from 'store/wallet/slice'
import { StringUSDC } from 'models/Wallet'

type PurchaseStepParams = {
desiredAmount: number
Expand Down
35 changes: 10 additions & 25 deletions packages/common/src/store/cache/actions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { ID, UID } from 'models/Identifiers'
import { ID } from 'models/Identifiers'
import { Kind } from 'models/Kind'
import { Status } from 'models/Status'

import { Metadata } from './types'
import {
CacheType,
EntriesByKind,
Entry,
Metadata,
SubscriberInfo,
SubscriptionInfo
} from './types'

export const ADD = 'CACHE/ADD'
export const ADD_SUCCEEDED = 'CACHE/ADD_SUCCEEDED'
Expand All @@ -18,13 +25,6 @@ export const REMOVE_SUCCEEDED = 'CACHE/REMOVE_SUCCEEDED'
export const SET_EXPIRED = 'CACHE/SET_EXPIRED'
export const SET_CACHE_CONFIG = 'CACHE/SET_CONFIG'

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>[]
Expand Down Expand Up @@ -76,10 +76,6 @@ export const addSucceeded = ({
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>
Expand All @@ -103,10 +99,6 @@ export const addEntries = (
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
Expand Down Expand Up @@ -139,7 +131,7 @@ export const increment = (kind: Kind, entries: Entry[]) => ({
* Sets the status of an entry from the cache as to be removed. The
* entries actually get removed by the removeSucceeded action
*/
export const remove = (kind: Kind, ids: ID[]) => ({
export const remove = (kind: Kind, ids: (ID | string)[]) => ({
type: REMOVE,
kind,
ids
Expand All @@ -166,11 +158,6 @@ export const setStatus = (
statuses
})

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

/**
* Subscribes uids to ids in the cache.
*/
Expand Down Expand Up @@ -212,8 +199,6 @@ export const setExpired = (kind: Kind, id: ID) => ({
id
})

export type CacheType = 'normal' | 'fast' | 'safe-fast'

export type SetCacheConfigAction = {
cacheType: CacheType
entryTTL: number
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/store/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export * as cacheConfig from './config'
export * as cacheReducer from './reducer'
export * as cacheSelectors from './selectors'
export { processAndCacheUsers, reformatUser } from './users/utils'
export * from './types'
3 changes: 1 addition & 2 deletions packages/common/src/store/cache/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ import {
SET_EXPIRED,
INCREMENT,
AddSuccededAction,
CacheType,
ADD_ENTRIES,
AddEntriesAction,
SetCacheConfigAction,
SET_CACHE_CONFIG
} from './actions'
import { Metadata } from './types'
import { CacheType, Metadata } from './types'

type CacheState = {
entries: Record<ID, { _timestamp: number; metadata: Metadata }>
Expand Down
25 changes: 25 additions & 0 deletions packages/common/src/store/cache/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
import { ID, UID } from 'models/Identifiers'
import { Kind } from 'models/Kind'

export type CacheType = 'normal' | 'fast' | 'safe-fast'

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

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

export type Metadata = {
blocknumber?: number
local?: boolean
} & Record<string, any>

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

export type SubscriptionInfo = SubscriberInfo & {
kind: Kind
}
2 changes: 1 addition & 1 deletion packages/common/src/store/ui/modals/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Action, combineReducers, Reducer } from '@reduxjs/toolkit'

import { addFundsModalReducer } from './add-funds-modal'
import { coinflowOnrampModalReducer } from './coinflow-onramp-modal'
import { coinflowWithdrawModalReducer } from './coinflow-withdraw-modal'
import { createChatModalReducer } from './create-chat-modal'
import { BaseModalState } from './createModal'
import { editPlaylistModalReducer } from './edit-playlist-modal'
Expand All @@ -15,7 +16,6 @@ import { usdcManualTransferModalReducer } from './usdc-manual-transfer-modal'
import { usdcPurchaseDetailsModalReducer } from './usdc-purchase-details-modal'
import { usdcTransactionDetailsModalReducer } from './usdc-transaction-details-modal'
import { withdrawUSDCModalReducer } from './withdraw-usdc-modal'
import { coinflowWithdrawModalReducer } from './coinflow-withdraw-modal'

/**
* Create a bunch of reducers that do nothing, so that the state is maintained and not lost through the child reducers
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/store/ui/modals/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ModalSource } from 'models/Analytics'

import { AddFundsModalState } from './add-funds-modal'
import { CoinflowOnrampModalState } from './coinflow-onramp-modal'
import { CoinflowWithdrawModalState } from './coinflow-withdraw-modal'
import { CreateChatModalState } from './create-chat-modal'
import { BaseModalState } from './createModal'
import { EditPlaylistModalState } from './edit-playlist-modal'
Expand All @@ -13,7 +14,6 @@ import { USDCManualTransferModalState } from './usdc-manual-transfer-modal'
import { USDCPurchaseDetailsModalState } from './usdc-purchase-details-modal'
import { USDCTransactionDetailsModalState } from './usdc-transaction-details-modal'
import { WithdrawUSDCModalState } from './withdraw-usdc-modal'
import { CoinflowWithdrawModalState } from './coinflow-withdraw-modal'

export type Modals =
| 'TiersExplainer'
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/utils/uid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class Uid {
}
}

export function makeUids(kinds: string[], ids: number[], source: string) {
export function makeUids(kinds: string[], ids: number[], source?: string) {
if (Array.isArray(kinds)) {
// Multiple kinds in the ids array.
const totals: Record<string, number> = {}
Expand Down
Loading

0 comments on commit 72c0af4

Please sign in to comment.