Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: blazingly fast #7799

Merged
merged 11 commits into from
Sep 25, 2024
2 changes: 1 addition & 1 deletion src/state/apis/nft/nftApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type GetNftCollectionInput = {
accountIds: AccountId[]
}

type NftState = {
export type NftState = {
selectedNftAvatarByWalletId: Record<WalletId, AssetId>
nfts: {
byId: PartialRecord<AssetId, NftItem>
Expand Down
10 changes: 6 additions & 4 deletions src/state/apis/snapshot/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ type FoxVotingPowerCryptoBalance = string

const SNAPSHOT_SPACE = 'shapeshiftdao.eth'

export const initialState: {
votingPowerByModel: Record<ParameterModel, string | undefined>
strategies: Strategy[] | undefined
} = {
export const initialState: SnapshotState = {
votingPowerByModel: {
SWAPPER: undefined,
THORCHAIN_LP: undefined,
},
strategies: undefined,
}

export type SnapshotState = {
votingPowerByModel: Record<ParameterModel, string | undefined>
strategies: Strategy[] | undefined
}

export const snapshot = createSlice({
name: 'snapshot',
initialState,
Expand Down
30 changes: 4 additions & 26 deletions src/state/migrations/clearAssets.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
import type { ReduxState } from 'state/reducer'
import type { PersistPartial } from 'redux-persist/es/persistReducer'
import type { AssetsState } from 'state/slices/assetsSlice/assetsSlice'
import { initialState } from 'state/slices/assetsSlice/assetsSlice'

export const clearAssets = (state: ReduxState): ReduxState => {
return {
...state,
assets: initialState,
// This is very ugly but also very correct
// Typically, to achieve this, we would dispatch nftapi.util.resetApiState as a side effect
// But we can't do this here because circular deps, so we have to do it manually
// https://redux-toolkit.js.org/rtk-query/api/created-api/api-slice-utils#resetapistate
assetApi: {
queries: {},
mutations: {},
provided: {},
subscriptions: {},
config: {
online: true,
focused: true,
middlewareRegistered: true,
refetchOnFocus: false,
refetchOnReconnect: true,
refetchOnMountOrArgChange: false,
keepUnusedDataFor: 60,
reducerPath: 'assetApi',
},
},
}
export const clearAssets = (_state: AssetsState): AssetsState & PersistPartial => {
return initialState as AssetsState & PersistPartial
}
10 changes: 4 additions & 6 deletions src/state/migrations/clearMarketData.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { ReduxState } from 'state/reducer'
import type { PersistPartial } from 'redux-persist/es/persistReducer'
import { initialState } from 'state/slices/marketDataSlice/marketDataSlice'
import type { MarketDataState } from 'state/slices/marketDataSlice/types'

export const clearMarketData = (state: ReduxState): ReduxState => {
export const clearMarketData = (_state: MarketDataState): MarketDataState & PersistPartial => {
// Migration to clear marketData state
return {
...state,
marketData: initialState,
}
return initialState as MarketDataState & PersistPartial
}
30 changes: 4 additions & 26 deletions src/state/migrations/clearNfts.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
import type { PersistPartial } from 'redux-persist/es/persistReducer'
import type { NftState } from 'state/apis/nft/nftApi'
import { initialState } from 'state/apis/nft/nftApi'
import type { ReduxState } from 'state/reducer'

export const clearNfts = (state: ReduxState): ReduxState => {
return {
...state,
nft: initialState,
// This is very ugly but also very correct
// Typically, to achieve this, we would dispatch nftapi.util.resetApiState as a side effect
// But we can't do this here because circular deps, so we have to do it manually
// https://redux-toolkit.js.org/rtk-query/api/created-api/api-slice-utils#resetapistate
nftApi: {
queries: {},
mutations: {},
provided: {},
subscriptions: {},
config: {
online: true,
focused: false,
middlewareRegistered: true,
refetchOnFocus: false,
refetchOnReconnect: true,
refetchOnMountOrArgChange: false,
keepUnusedDataFor: 60,
reducerPath: 'nftApi',
},
},
}
export const clearNfts = (_state: NftState): NftState & PersistPartial => {
return initialState as NftState & PersistPartial
}
12 changes: 6 additions & 6 deletions src/state/migrations/clearOpportunities.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { ReduxState } from 'state/reducer'
import type { PersistPartial } from 'redux-persist/es/persistReducer'
import type { OpportunitiesState } from 'state/slices/opportunitiesSlice/types'

import { initialState } from '../slices/opportunitiesSlice/opportunitiesSlice'

export const clearOpportunities = (state: ReduxState): ReduxState => {
export const clearOpportunities = (
_state: OpportunitiesState,
): OpportunitiesState & PersistPartial => {
// Migration to clear opportunitiesApi and opportunitiesApi state
return {
...state,
opportunities: initialState,
}
return initialState as OpportunitiesState & PersistPartial
}
gomesalexandre marked this conversation as resolved.
Show resolved Hide resolved
30 changes: 4 additions & 26 deletions src/state/migrations/clearPortfolio.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
import type { ReduxState } from 'state/reducer'
import type { PersistPartial } from 'redux-persist/es/persistReducer'
import type { Portfolio } from 'state/slices/portfolioSlice/portfolioSliceCommon'
import { initialState } from 'state/slices/portfolioSlice/portfolioSliceCommon'

export const clearPortfolio = (state: ReduxState): ReduxState => {
return {
...state,
portfolio: initialState,
// This is very ugly but also very correct
// Typically, to achieve this, we would dispatch nftapi.util.resetApiState as a side effect
// But we can't do this here because circular deps, so we have to do it manually
// https://redux-toolkit.js.org/rtk-query/api/created-api/api-slice-utils#resetapistate
portfolioApi: {
queries: {},
mutations: {},
provided: {},
subscriptions: {},
config: {
online: true,
focused: true,
middlewareRegistered: true,
refetchOnFocus: false,
refetchOnReconnect: true,
refetchOnMountOrArgChange: false,
keepUnusedDataFor: 60,
reducerPath: 'portfolioApi',
},
},
}
export const clearPortfolio = (_state: Portfolio): Portfolio & PersistPartial => {
return initialState as Portfolio & PersistPartial
}
10 changes: 4 additions & 6 deletions src/state/migrations/clearSnapshot.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { PersistPartial } from 'redux-persist/es/persistReducer'
import type { SnapshotState } from 'state/apis/snapshot/snapshot'
import { initialState } from 'state/apis/snapshot/snapshot'
import type { ReduxState } from 'state/reducer'

export const clearSnapshot = (state: ReduxState): ReduxState => {
export const clearSnapshot = (_state: SnapshotState): SnapshotState & PersistPartial => {
// Migration to clear snapshot API
return {
...state,
snapshot: initialState,
}
return initialState as SnapshotState & PersistPartial
}
10 changes: 4 additions & 6 deletions src/state/migrations/clearTxHistory.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { ReduxState } from 'state/reducer'
import type { PersistPartial } from 'redux-persist/es/persistReducer'
import type { TxHistory } from 'state/slices/txHistorySlice/txHistorySlice'
import { initialState } from 'state/slices/txHistorySlice/txHistorySlice'

export const clearTxHistory = (state: ReduxState): ReduxState => {
export const clearTxHistory = (_state: TxHistory): TxHistory & PersistPartial => {
// Migration to clear tx history state
return {
...state,
txHistory: initialState,
}
return initialState as TxHistory & PersistPartial
}
22 changes: 10 additions & 12 deletions src/state/migrations/clearWalletConnectWalletsMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import type { ReduxState } from 'state/reducer'
import type { PersistPartial } from 'redux-persist/es/persistReducer'
import type { Portfolio } from 'state/slices/portfolioSlice/portfolioSliceCommon'

export const clearWalletConnectWalletsMetadata = (state: ReduxState): ReduxState => {
const updatedWalletIds = state.portfolio.wallet.ids.filter(id => !id.startsWith('wc:'))
export const clearWalletConnectWalletsMetadata = (state: Portfolio): Portfolio & PersistPartial => {
const updatedWalletIds = state.wallet.ids.filter(id => !id.startsWith('wc:'))
const updatedWalletById = Object.fromEntries(
Object.entries(state.portfolio.wallet.byId).filter(([walletId]) => !walletId.startsWith('wc:')),
Object.entries(state.wallet.byId).filter(([walletId]) => !walletId.startsWith('wc:')),
)

return {
...state,
portfolio: {
...state.portfolio,
wallet: {
...state.portfolio.wallet,
ids: updatedWalletIds,
byId: updatedWalletById,
},
wallet: {
...state.wallet,
ids: updatedWalletIds,
byId: updatedWalletById,
},
gomesalexandre marked this conversation as resolved.
Show resolved Hide resolved
}
} as Portfolio & PersistPartial
}
159 changes: 35 additions & 124 deletions src/state/migrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,126 +1,37 @@
import type { MigrationManifest } from 'redux-persist'

import { clearAssets } from './clearAssets'
import { clearMarketData } from './clearMarketData'
import { clearNfts } from './clearNfts'
import { clearOpportunities } from './clearOpportunities'
import { clearPortfolio } from './clearPortfolio'
import { clearSnapshot } from './clearSnapshot'
import { clearTxHistory } from './clearTxHistory'
import { clearWalletConnectWalletsMetadata } from './clearWalletConnectWalletsMetadata'

export const migrations = {
0: clearOpportunities,
1: clearOpportunities,
2: clearPortfolio,
3: clearOpportunities,
4: clearOpportunities,
5: clearNfts,
6: clearAssets,
7: clearPortfolio,
8: clearOpportunities,
9: clearAssets,
10: clearTxHistory,
11: clearAssets,
12: clearAssets,
13: clearPortfolio,
14: clearTxHistory,
15: clearAssets,
16: clearOpportunities,
17: clearTxHistory,
18: clearTxHistory,
19: clearMarketData,
20: clearTxHistory,
21: clearAssets,
22: clearTxHistory,
23: clearPortfolio,
24: clearTxHistory,
25: clearAssets,
26: clearAssets,
27: clearPortfolio,
28: clearAssets,
29: clearAssets,
30: clearTxHistory,
31: clearAssets,
32: clearSnapshot,
33: clearAssets,
34: clearTxHistory,
35: clearAssets,
36: clearTxHistory,
37: clearPortfolio,
38: clearOpportunities,
39: clearAssets,
40: clearAssets,
41: clearAssets,
42: clearAssets,
43: clearAssets,
44: clearAssets,
45: clearAssets,
46: clearAssets,
47: clearAssets,
48: clearAssets,
49: clearAssets,
50: clearPortfolio,
51: clearAssets,
52: clearPortfolio,
53: clearAssets,
54: clearAssets,
55: clearAssets,
56: clearPortfolio,
57: clearAssets,
58: clearAssets,
59: clearAssets,
60: clearAssets,
61: clearOpportunities,
62: clearAssets,
63: clearAssets,
64: clearPortfolio,
65: clearAssets,
66: clearAssets,
67: clearAssets,
68: clearAssets,
69: clearAssets,
70: clearAssets,
71: clearAssets,
72: clearAssets,
73: clearAssets,
74: clearAssets,
75: clearAssets,
76: clearAssets,
77: clearAssets,
78: clearAssets,
79: clearAssets,
80: clearAssets,
81: clearAssets,
82: clearAssets,
83: clearAssets,
84: clearAssets,
85: clearAssets,
86: clearAssets,
87: clearWalletConnectWalletsMetadata,
gomesalexandre marked this conversation as resolved.
Show resolved Hide resolved
88: clearAssets,
89: clearAssets,
90: clearAssets,
91: clearAssets,
92: clearTxHistory,
93: clearAssets,
94: clearAssets,
95: clearTxHistory,
96: clearTxHistory,
97: clearAssets,
98: clearAssets,
99: clearAssets,
100: clearAssets,
101: clearAssets,
102: clearAssets,
103: clearAssets,
104: clearTxHistory,
105: clearPortfolio,
106: clearOpportunities,
107: clearAssets,
108: clearAssets,
109: clearAssets,
110: clearAssets,
111: clearAssets,
112: clearAssets,
113: clearAssets,
114: clearAssets,
}
export const clearTxHistoryMigrations = {
// Uncomment me when introducing the first migration for this slice
// 0: clearTxHistory,
} as unknown as Omit<MigrationManifest, '_persist'>

export const clearOpportunitiesMigrations = {
// Uncomment me when introducing the first migration for this slice
// 0: clearOpportunities,
} as unknown as Omit<MigrationManifest, '_persist'>

export const clearPortfolioMigrations = {
// Uncomment me when introducing the first migration for this slice
// 0: clearPortfolio,
} as unknown as Omit<MigrationManifest, '_persist'>

export const clearNftsMigrations = {
// Uncomment me when introducing the first migration for this slice
// 0: clearNfts,
} as unknown as Omit<MigrationManifest, '_persist'>

export const clearAssetsMigrations = {
0: clearAssets,
} as unknown as Omit<MigrationManifest, '_persist'>

export const clearMarketDataMigrations = {
// Uncomment me when introducing the first migration for this slice
// 0: clearMarketData,
} as unknown as Omit<MigrationManifest, '_persist'>

export const clearSnapshotMigrations = {
// Uncomment me when introducing the first migration for this slice
// 0: clearSnapshot,
} as unknown as Omit<MigrationManifest, '_persist'>
gomesalexandre marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion src/state/migrations/transformClearAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const transform = (fileInfo: FileInfo, api: API) => {

root.find(j.ExportNamedDeclaration).forEach(path => {
const objectExpression = j(path)
.find(j.VariableDeclarator, { id: { name: 'migrations' } })
.find(j.VariableDeclarator, { id: { name: 'clearAssetsMigrations' } })
.find(j.ObjectExpression)
.at(0)

Expand Down
Loading
Loading