Skip to content

Commit

Permalink
Define #registerEventListeners constructor helper private method
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorLift committed Jan 24, 2024
1 parent e217169 commit c6ba49a
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions packages/assets-controllers/src/TokenDetectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ export class TokenDetectionController extends StaticIntervalPollingController<

#isDetectionEnabledForNetwork: boolean;

readonly #onPreferencesStateChange: (
listener: (preferencesState: PreferencesState) => Promise<void>,
) => void;

readonly #addDetectedTokens: TokensController['addDetectedTokens'];

readonly #getBalancesInSingleCall: AssetsContractController['getBalancesInSingleCall'];
Expand All @@ -158,11 +162,11 @@ export class TokenDetectionController extends StaticIntervalPollingController<
* @param options.interval - Polling interval used to fetch new token rates
* @param options.networkClientId - The selected network client ID of the current network
* @param options.selectedAddress - Vault selected address
* @param options.getPreferencesState - Gets the state of the preferences controller.
* @param options.onPreferencesStateChange - Allows subscribing to preferences controller state changes.
* @param options.addDetectedTokens - Add a list of detected tokens.
* @param options.getBalancesInSingleCall - Gets the balances of a list of tokens for the given address.
* @param options.getTokensState - Gets the current state of the Tokens controller.
* @param options.getPreferencesState - Gets the state of the preferences controller.
* @param options.trackMetaMetricsEvent - Sets options for MetaMetrics event tracking.
*/
constructor({
Expand All @@ -182,13 +186,13 @@ export class TokenDetectionController extends StaticIntervalPollingController<
selectedAddress?: string;
interval?: number;
disabled?: boolean;
getPreferencesState: () => PreferencesState;
onPreferencesStateChange: (
listener: (preferencesState: PreferencesState) => void,
listener: (preferencesState: PreferencesState) => Promise<void>,
) => void;
addDetectedTokens: TokensController['addDetectedTokens'];
getBalancesInSingleCall: AssetsContractController['getBalancesInSingleCall'];
getTokensState: () => TokensState;
getPreferencesState: () => PreferencesState;
trackMetaMetricsEvent: (options: {
event: string;
category: string;
Expand Down Expand Up @@ -222,6 +226,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<
this.#chainId,
);

this.#onPreferencesStateChange = onPreferencesStateChange;
this.#addDetectedTokens = addDetectedTokens;
this.#getBalancesInSingleCall = getBalancesInSingleCall;
this.#getTokensState = getTokensState;
Expand All @@ -233,6 +238,22 @@ export class TokenDetectionController extends StaticIntervalPollingController<
);
this.#isUnlocked = isUnlocked;

this.#registerEventListeners();
}

/**
* Constructor helper for registering this controller's messaging system subscriptions to controller events.
*/
#registerEventListeners() {
this.messagingSystem.subscribe('KeyringController:unlock', async () => {
this.#isUnlocked = true;
await this.#restartTokenDetection();
});

this.messagingSystem.subscribe('KeyringController:lock', () => {
this.#isUnlocked = false;
});

this.messagingSystem.subscribe(
'TokenListController:stateChange',
async ({ tokenList }) => {
Expand All @@ -244,7 +265,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<
},
);

onPreferencesStateChange(
this.#onPreferencesStateChange(
async ({ selectedAddress: newSelectedAddress, useTokenDetection }) => {
const isSelectedAddressChanged =
this.#selectedAddress !== newSelectedAddress;
Expand Down Expand Up @@ -298,8 +319,6 @@ export class TokenDetectionController extends StaticIntervalPollingController<
}
},
);

this.#registerKeyringListeners();
}

/**
Expand All @@ -325,21 +344,6 @@ export class TokenDetectionController extends StaticIntervalPollingController<
return !this.#disabled && this.#isUnlocked;
}

/**
* Constructor helper for subscribing listeners
* to the keyring locked state changes
*/
async #registerKeyringListeners() {
this.messagingSystem.subscribe('KeyringController:unlock', async () => {
this.#isUnlocked = true;
await this.#restartTokenDetection();
});

this.messagingSystem.subscribe('KeyringController:lock', () => {
this.#isUnlocked = false;
});
}

/**
* Start polling for detected tokens.
*/
Expand Down

0 comments on commit c6ba49a

Please sign in to comment.