Skip to content

Commit

Permalink
Replace MemStoreControllersComposedState usage with `BackgroundStat…
Browse files Browse the repository at this point in the history
…eProxy`
  • Loading branch information
MajorLift committed Dec 4, 2024
1 parent 32f90c5 commit 99ebad8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
28 changes: 17 additions & 11 deletions app/scripts/controllers/metametrics-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import Analytics from '../lib/segment/analytics';
import { ENVIRONMENT } from '../../../development/build/constants';
///: END:ONLY_INCLUDE_IF

import { BackgroundStateProxy } from '../../../shared/types/metamask';
import type {
PreferencesControllerState,
PreferencesControllerGetStateAction,
Expand Down Expand Up @@ -963,7 +964,7 @@ export default class MetaMetricsController extends BaseController<
}
}

handleMetaMaskStateUpdate(newState: MetaMaskState): void {
handleMetaMaskStateUpdate(newState: BackgroundStateProxy): void {
const userTraits = this._buildUserTraitsObject(newState);
if (userTraits) {
this.identify(userTraits);
Expand Down Expand Up @@ -1128,7 +1129,7 @@ export default class MetaMetricsController extends BaseController<
* @returns traits that have changed since last update
*/
_buildUserTraitsObject(
metamaskState: MetaMaskState,
metamaskState: BackgroundStateProxy,
): Partial<MetaMetricsUserTraits> | null {
///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
const mmiAccountAddress =
Expand Down Expand Up @@ -1258,11 +1259,13 @@ export default class MetaMetricsController extends BaseController<
*
* @param allNfts
*/
#getAllNFTsFlattened = memoize((allNfts: MetaMaskState['allNfts'] = {}) => {
return Object.values(allNfts).reduce((result: Nft[], chainNFTs) => {
return result.concat(...Object.values(chainNFTs));
}, []);
});
#getAllNFTsFlattened = memoize(
(allNfts: BackgroundStateProxy['NftController']['allNfts'] = {}) => {
return Object.values(allNfts).reduce((result: Nft[], chainNFTs) => {
return result.concat(...Object.values(chainNFTs));
}, []);
},
);

/**
* Returns the number of unique NFT addresses the user
Expand All @@ -1271,7 +1274,7 @@ export default class MetaMetricsController extends BaseController<
* @param allNfts
*/
#getAllUniqueNFTAddressesLength(
allNfts: MetaMaskState['allNfts'] = {},
allNfts: BackgroundStateProxy['NftController']['allNfts'] = {},
): number {
const allNFTAddresses = this.#getAllNFTsFlattened(allNfts).map(
(nft) => nft.address,
Expand All @@ -1284,7 +1287,9 @@ export default class MetaMetricsController extends BaseController<
* @param allTokens
* @returns number of unique token addresses
*/
#getNumberOfTokens(allTokens: MetaMaskState['allTokens']): number {
#getNumberOfTokens(
allTokens: BackgroundStateProxy['TokensController']['allTokens'],
): number {
return Object.values(allTokens).reduce((result, accountsByChain) => {
return result + sum(Object.values(accountsByChain).map(size));
}, 0);
Expand Down Expand Up @@ -1508,8 +1513,9 @@ export default class MetaMetricsController extends BaseController<
*
* @param metamaskState
*/
#getPetnameAddressCount(metamaskState: MetaMaskState): number {
const addressNames = metamaskState.names?.[NameType.ETHEREUM_ADDRESS] ?? {};
#getPetnameAddressCount(metamaskState: BackgroundStateProxy): number {
const addressNames =
metamaskState.NameController.names?.[NameType.ETHEREUM_ADDRESS] ?? {};

return Object.keys(addressNames).reduce((totalCount, address) => {
const addressEntry = addressNames[address];
Expand Down
16 changes: 8 additions & 8 deletions app/scripts/lib/PatchStore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createProjectLogger, getKnownPropertyNames } from '@metamask/utils';
import { Patch } from 'immer';
import { v4 as uuid } from 'uuid';
import { MemStoreControllersComposedState as BackgroundState } from '../../../shared/types/metamask';
import type { BackgroundStateProxy } from '../../../shared/types/metamask';
import ComposableObservableStore from './ComposableObservableStore';
import { sanitizeUIState } from './state-utils';

Expand All @@ -16,8 +16,8 @@ export class PatchStore {

private listener: (request: {
controllerKey: string;
oldState: BackgroundState;
newState: BackgroundState;
oldState: BackgroundStateProxy;
newState: BackgroundStateProxy;
}) => void;

constructor(observableStore: ComposableObservableStore) {
Expand Down Expand Up @@ -52,8 +52,8 @@ export class PatchStore {
newState,
}: {
controllerKey: string;
oldState: BackgroundState;
newState: BackgroundState;
oldState: BackgroundStateProxy;
newState: BackgroundStateProxy;
}) {
const sanitizedNewState = sanitizeUIState(newState);
const patches = this._generatePatches(oldState, sanitizedNewState);
Expand Down Expand Up @@ -81,10 +81,10 @@ export class PatchStore {
}

private _generatePatches(
oldState: BackgroundState,
newState: BackgroundState,
oldState: BackgroundStateProxy,
newState: BackgroundStateProxy,
): Patch[] {
return getKnownPropertyNames<keyof BackgroundState>(newState).reduce<
return getKnownPropertyNames<keyof BackgroundStateProxy>(newState).reduce<
Patch[]
>((patches, controllerName) => {
Object.keys(oldState[controllerName]).forEach((key) => {
Expand Down
8 changes: 5 additions & 3 deletions app/scripts/lib/state-utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { SnapControllerState } from '@metamask/snaps-controllers';
import { isSnapId, Snap } from '@metamask/snaps-utils';
import { MemStoreControllersComposedState as BackgroundState } from '../../../shared/types/metamask';
import { BackgroundStateProxy } from '../../../shared/types/metamask';

const REMOVE_KEYS = ['snapStates', 'unencryptedSnapStates', 'vault'] as const;

export function sanitizeUIState(state: BackgroundState): BackgroundState {
export function sanitizeUIState(
state: BackgroundStateProxy,
): BackgroundStateProxy {
const newState = { ...state };

for (const key of REMOVE_KEYS) {
Expand All @@ -20,7 +22,7 @@ export function sanitizeUIState(state: BackgroundState): BackgroundState {
return newState;
}

function sanitizeSnapData(state: BackgroundState) {
function sanitizeSnapData(state: BackgroundStateProxy) {
const snapsData: SnapControllerState['snaps'] | undefined =
state.SnapController.snaps;

Expand Down

0 comments on commit 99ebad8

Please sign in to comment.