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

optimization - zustand selectors #1513

Merged
merged 5 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Address } from 'wagmi';
import create from 'zustand';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';

export interface AppConnectionWalletSwitcherStore {
nudgeSheetEnabled: boolean;
Expand Down Expand Up @@ -78,6 +79,6 @@ export const appConnectionWalletSwitcherStore =
},
);

export const useAppConnectionWalletSwitcherStore = create(
appConnectionWalletSwitcherStore,
export const useAppConnectionWalletSwitcherStore = withSelectors(
create(appConnectionWalletSwitcherStore),
);
3 changes: 2 additions & 1 deletion src/core/state/appSessions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import create from 'zustand';
import { ChainId } from '~/core/types/chains';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';

export interface AppSession {
activeSessionAddress: Address;
Expand Down Expand Up @@ -242,4 +243,4 @@ export const appSessionsStore = createStore<AppSessionsStore<AppSession>>(
},
);

export const useAppSessionsStore = create(appSessionsStore);
export const useAppSessionsStore = withSelectors(create(appSessionsStore));
3 changes: 2 additions & 1 deletion src/core/state/contacts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Address } from 'wagmi';
import create from 'zustand';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';

export interface Contact {
name: string;
Expand Down Expand Up @@ -52,4 +53,4 @@ export const contactsStore = createStore<ContactsStore>(
},
);

export const useContactsStore = create(contactsStore);
export const useContactsStore = withSelectors(create(contactsStore));
4 changes: 3 additions & 1 deletion src/core/state/currentSettings/currentAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import create from 'zustand';

import { createStore } from '~/core/state/internal/createStore';

import { withSelectors } from '../internal/withSelectors';

interface PersistedAddressState {
currentAddress: Address;
setCurrentAddress: (address: Address) => void;
Expand Down Expand Up @@ -47,4 +49,4 @@ persistedAddressStore.subscribe((state) => {
}
});

export const useCurrentAddressStore = currentAddressStore;
export const useCurrentAddressStore = withSelectors(currentAddressStore);
4 changes: 3 additions & 1 deletion src/core/state/currentSettings/currentTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import create from 'zustand';
import { createStore } from '~/core/state/internal/createStore';
import { ThemeOption } from '~/core/types/settings';

import { withSelectors } from '../internal/withSelectors';

export interface CurrentThemeState {
currentTheme: Exclude<ThemeOption, 'system'>;
currentUserSelectedTheme: ThemeOption;
Expand Down Expand Up @@ -38,4 +40,4 @@ export const currentThemeStore = createStore<CurrentThemeState>(
},
);

export const useCurrentThemeStore = create(currentThemeStore);
export const useCurrentThemeStore = withSelectors(create(currentThemeStore));
3 changes: 2 additions & 1 deletion src/core/state/error/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import create from 'zustand';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';

export interface ErrorState {
error: Error | null;
Expand All @@ -12,4 +13,4 @@ export const errorStore = createStore<ErrorState>((set) => ({
setError: (error) => set({ error }),
}));

export const useErrorStore = create(errorStore);
export const useErrorStore = withSelectors(create(errorStore));
3 changes: 2 additions & 1 deletion src/core/state/gas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '~/core/types/gas';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';

export interface GasStore {
selectedGas: GasFeeParams | GasFeeLegacyParams;
Expand Down Expand Up @@ -65,4 +66,4 @@ export const gasStore = createStore<GasStore>(
},
);

export const useGasStore = create(gasStore);
export const useGasStore = withSelectors(create(gasStore));
3 changes: 2 additions & 1 deletion src/core/state/hiddenAssets/hiddenAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ParsedUserAsset } from '~/core/types/assets';
import { SearchAsset } from '~/core/types/search';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';

type UpdateHiddenAssetArgs = {
uniqueId: string;
Expand Down Expand Up @@ -49,4 +50,4 @@ export const hiddenAssetsStore = createStore<HiddenAssetState>(
},
);

export const useHiddenAssetStore = create(hiddenAssetsStore);
export const useHiddenAssetStore = withSelectors(create(hiddenAssetsStore));
18 changes: 18 additions & 0 deletions src/core/state/internal/withSelectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { StoreApi, UseBoundStore } from 'zustand';

type WithSelectors<S> = S extends { getState: () => infer T }
? S & { use: { [K in keyof T]: () => T[K] } }
: never;

export function withSelectors<S extends UseBoundStore<StoreApi<object>>>(
_store: S,
) {
const store = _store as WithSelectors<typeof _store>;
store.use = {};
for (const k of Object.keys(store.getState())) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(store.use as any)[k] = () => store((s) => s[k as keyof typeof s]);
}

return store;
}
9 changes: 6 additions & 3 deletions src/core/state/navRestoration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import create from 'zustand';
import { isNativePopup } from '~/core/utils/tabs';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';

export interface NavRestorationStore {
clearLastPage: () => Promise<void>;
lastPage?: string;
lastPage: string | undefined;
setLastPage: (lastPage: string) => Promise<void>;
lastState?: Record<string, string>;
lastState: Record<string, string> | undefined;
setLastState: (lastState: Record<string, string>) => Promise<void>;
setShouldRestoreNavigation: (should: boolean) => Promise<void>;
shouldRestoreNavigation: boolean;
Expand Down Expand Up @@ -52,4 +53,6 @@ export const navRestorationStore = createStore<NavRestorationStore>(
},
);

export const useNavRestorationStore = create(navRestorationStore);
export const useNavRestorationStore = withSelectors(
create(navRestorationStore),
);
3 changes: 2 additions & 1 deletion src/core/state/nfts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Address } from 'wagmi';
import create from 'zustand';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';

type NftDisplayMode = 'byCollection' | 'grouped';
export type NftSort = 'alphabetical' | 'recent';
Expand Down Expand Up @@ -69,4 +70,4 @@ export const nftsStore = createStore<NftsState>(
},
);

export const useNftsStore = create(nftsStore);
export const useNftsStore = withSelectors(create(nftsStore));
3 changes: 2 additions & 1 deletion src/core/state/nonce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import create from 'zustand';
import { ChainId } from '~/core/types/chains';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';

type NonceData = {
currentNonce?: number;
Expand Down Expand Up @@ -67,4 +68,4 @@ export const nonceStore = createStore<CurrentNonceState>(
},
);

export const useNonceStore = create(nonceStore);
export const useNonceStore = withSelectors(create(nonceStore));
3 changes: 2 additions & 1 deletion src/core/state/popupInstances/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { isNativePopup } from '~/core/utils/tabs';
import { IndependentField } from '~/entries/popup/hooks/swap/useSwapInputs';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';

type SendAddress = Address | 'eth' | '';
interface CustomNetworkDraft {
Expand Down Expand Up @@ -166,7 +167,7 @@ export const popupInstanceStore = createStore<PopupInstanceStore>(
},
);

export const usePopupInstanceStore = create(popupInstanceStore);
export const usePopupInstanceStore = withSelectors(create(popupInstanceStore));

// creates handlers that only work in popup context and passes through callback types
function popupInstanceHandlerFactory<
Expand Down
3 changes: 2 additions & 1 deletion src/core/state/rainbowChains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { persistOptions } from '~/core/utils/persistOptions';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';

export interface RainbowChain {
activeRpcUrl: string;
Expand Down Expand Up @@ -289,4 +290,4 @@ export const rainbowChainsStore = createStore<RainbowChainsState>(
},
);

export const useRainbowChainsStore = create(rainbowChainsStore);
export const useRainbowChainsStore = withSelectors(create(rainbowChainsStore));
3 changes: 2 additions & 1 deletion src/core/state/savedEnsNames/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Address } from 'wagmi';
import create from 'zustand';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';

type SavedNamesStore = {
savedNames: Record<Address, string>;
Expand All @@ -20,4 +21,4 @@ export const savedEnsNamesStore = createStore<SavedNamesStore>(
{ persist: { name: 'ensSavedNames', version: 0 } },
);

export const useSavedEnsNames = create(savedEnsNamesStore);
export const useSavedEnsNames = withSelectors(create(savedEnsNamesStore));
3 changes: 2 additions & 1 deletion src/core/state/selectedNft/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import create from 'zustand';
import { UniqueAsset } from '~/core/types/nfts';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';

export interface SelectedNftState {
setSelectedNft: (nft?: UniqueAsset) => void;
Expand All @@ -16,4 +17,4 @@ export const selectedNftStore = createStore<SelectedNftState>((set) => ({
selectedNft: null,
}));

export const useSelectedNftStore = create(selectedNftStore);
export const useSelectedNftStore = withSelectors(create(selectedNftStore));
3 changes: 2 additions & 1 deletion src/core/state/selectedToken/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import create from 'zustand';
import { ParsedUserAsset } from '~/core/types/assets';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';

export interface SelectedTokenState {
getSelectedToken: () => ParsedUserAsset | null;
Expand All @@ -20,4 +21,4 @@ export const selectedTokenStore = createStore<SelectedTokenState>(
}),
);

export const useSelectedTokenStore = create(selectedTokenStore);
export const useSelectedTokenStore = withSelectors(create(selectedTokenStore));
5 changes: 4 additions & 1 deletion src/core/state/selectedTransaction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import create from 'zustand';
import { RainbowTransaction } from '~/core/types/transactions';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';

export interface SelectedTransactionState {
getSelectedTransaction: () => RainbowTransaction | null;
Expand All @@ -20,4 +21,6 @@ export const selectedTransactionStore = createStore<SelectedTransactionState>(
}),
);

export const useSelectedTransactionStore = create(selectedTransactionStore);
export const useSelectedTransactionStore = withSelectors(
create(selectedTransactionStore),
);
5 changes: 4 additions & 1 deletion src/core/state/swapAssetsToRefresh/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import create from 'zustand';
import { ParsedSearchAsset } from '~/core/types/assets';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';

export interface SwapAssetsToRefreshState {
setSwapAssetsToRefresh: (assetsToRefresh: {
Expand Down Expand Up @@ -30,4 +31,6 @@ export const swapAssetsToRefreshStore = createStore<SwapAssetsToRefreshState>(
}),
);

export const useSwapAssetsToRefreshStore = create(swapAssetsToRefreshStore);
export const useSwapAssetsToRefreshStore = withSelectors(
create(swapAssetsToRefreshStore),
);
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import create from 'zustand';
import { RainbowTransaction } from '~/core/types/transactions';

import { createStore } from '../../internal/createStore';
import { withSelectors } from '../../internal/withSelectors';

export interface CustomNetworkTransactionsState {
customNetworkTransactions: Record<
Expand Down Expand Up @@ -85,6 +86,6 @@ export const customNetworkTransactionsStore =
},
);

export const useCustomNetworkTransactionsStore = create(
customNetworkTransactionsStore,
export const useCustomNetworkTransactionsStore = withSelectors(
create(customNetworkTransactionsStore),
);
5 changes: 4 additions & 1 deletion src/core/state/transactions/pendingTransactions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import create from 'zustand';
import { RainbowTransaction } from '~/core/types/transactions';

import { createStore } from '../../internal/createStore';
import { withSelectors } from '../../internal/withSelectors';

export interface PendingTransactionsStateV1 {
[key: Address]: {
Expand Down Expand Up @@ -145,4 +146,6 @@ export const pendingTransactionsStore = createStore<PendingTransactionsState>(
},
);

export const usePendingTransactionsStore = create(pendingTransactionsStore);
export const usePendingTransactionsStore = withSelectors(
create(pendingTransactionsStore),
);
3 changes: 2 additions & 1 deletion src/core/state/userChains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SUPPORTED_MAINNET_CHAINS } from '~/core/references';
import { ChainId } from '~/core/types/chains';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';

export interface UserChainsState {
/**
Expand Down Expand Up @@ -114,4 +115,4 @@ export const userChainsStore = createStore<UserChainsState>(
},
);

export const useUserChainsStore = create(userChainsStore);
export const useUserChainsStore = withSelectors(create(userChainsStore));
3 changes: 2 additions & 1 deletion src/core/state/walletBackups/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import create from 'zustand';
import { KeychainType, KeychainWallet } from '~/core/types/keychainTypes';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';

export interface WalletBackupReminderStore {
reminded: boolean;
Expand Down Expand Up @@ -101,4 +102,4 @@ export const walletBackupsStore = createStore<WalletBackupsStore>(
},
);

export const useWalletBackupsStore = create(walletBackupsStore);
export const useWalletBackupsStore = withSelectors(create(walletBackupsStore));
3 changes: 2 additions & 1 deletion src/core/state/walletNames/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Address } from 'wagmi';
import create from 'zustand';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';

export interface WalletNamesStore {
walletNames: { [address: Address]: string };
Expand Down Expand Up @@ -49,4 +50,4 @@ export const walletNamesStore = createStore<WalletNamesStore>(
},
);

export const useWalletNamesStore = create(walletNamesStore);
export const useWalletNamesStore = withSelectors(create(walletNamesStore));
3 changes: 2 additions & 1 deletion src/core/state/walletOrder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Address } from 'wagmi';
import create from 'zustand';

import { createStore } from '../internal/createStore';
import { withSelectors } from '../internal/withSelectors';

export interface WalletOrderStore {
walletOrder: Address[];
Expand Down Expand Up @@ -31,4 +32,4 @@ export const walletOrderStore = createStore<WalletOrderStore>(
},
);

export const useWalletOrderStore = create(walletOrderStore);
export const useWalletOrderStore = withSelectors(create(walletOrderStore));