@@ -133,6 +133,10 @@ export class TokenDetectionController extends StaticIntervalPollingController<
133133
134134 #isDetectionEnabledForNetwork: boolean ;
135135
136+ readonly #onPreferencesStateChange: (
137+ listener : ( preferencesState : PreferencesState ) => Promise < void > ,
138+ ) => void ;
139+
136140 readonly #addDetectedTokens: TokensController [ 'addDetectedTokens' ] ;
137141
138142 readonly #getBalancesInSingleCall: AssetsContractController [ 'getBalancesInSingleCall' ] ;
@@ -158,11 +162,11 @@ export class TokenDetectionController extends StaticIntervalPollingController<
158162 * @param options.interval - Polling interval used to fetch new token rates
159163 * @param options.networkClientId - The selected network client ID of the current network
160164 * @param options.selectedAddress - Vault selected address
165+ * @param options.getPreferencesState - Gets the state of the preferences controller.
161166 * @param options.onPreferencesStateChange - Allows subscribing to preferences controller state changes.
162167 * @param options.addDetectedTokens - Add a list of detected tokens.
163168 * @param options.getBalancesInSingleCall - Gets the balances of a list of tokens for the given address.
164169 * @param options.getTokensState - Gets the current state of the Tokens controller.
165- * @param options.getPreferencesState - Gets the state of the preferences controller.
166170 * @param options.trackMetaMetricsEvent - Sets options for MetaMetrics event tracking.
167171 */
168172 constructor ( {
@@ -182,13 +186,13 @@ export class TokenDetectionController extends StaticIntervalPollingController<
182186 selectedAddress ?: string ;
183187 interval ?: number ;
184188 disabled ?: boolean ;
189+ getPreferencesState : ( ) => PreferencesState ;
185190 onPreferencesStateChange : (
186- listener : ( preferencesState : PreferencesState ) => void ,
191+ listener : ( preferencesState : PreferencesState ) => Promise < void > ,
187192 ) => void ;
188193 addDetectedTokens : TokensController [ 'addDetectedTokens' ] ;
189194 getBalancesInSingleCall : AssetsContractController [ 'getBalancesInSingleCall' ] ;
190195 getTokensState : ( ) => TokensState ;
191- getPreferencesState : ( ) => PreferencesState ;
192196 trackMetaMetricsEvent : ( options : {
193197 event : string ;
194198 category : string ;
@@ -222,6 +226,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<
222226 this . #chainId,
223227 ) ;
224228
229+ this . #onPreferencesStateChange = onPreferencesStateChange ;
225230 this . #addDetectedTokens = addDetectedTokens ;
226231 this . #getBalancesInSingleCall = getBalancesInSingleCall ;
227232 this . #getTokensState = getTokensState ;
@@ -233,6 +238,22 @@ export class TokenDetectionController extends StaticIntervalPollingController<
233238 ) ;
234239 this . #isUnlocked = isUnlocked ;
235240
241+ this . #registerEventListeners( ) ;
242+ }
243+
244+ /**
245+ * Constructor helper for registering this controller's messaging system subscriptions to controller events.
246+ */
247+ #registerEventListeners( ) {
248+ this . messagingSystem . subscribe ( 'KeyringController:unlock' , async ( ) => {
249+ this . #isUnlocked = true ;
250+ await this . #restartTokenDetection( ) ;
251+ } ) ;
252+
253+ this . messagingSystem . subscribe ( 'KeyringController:lock' , ( ) => {
254+ this . #isUnlocked = false ;
255+ } ) ;
256+
236257 this . messagingSystem . subscribe (
237258 'TokenListController:stateChange' ,
238259 async ( { tokenList } ) => {
@@ -244,7 +265,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<
244265 } ,
245266 ) ;
246267
247- onPreferencesStateChange (
268+ this . # onPreferencesStateChange(
248269 async ( { selectedAddress : newSelectedAddress , useTokenDetection } ) => {
249270 const isSelectedAddressChanged =
250271 this . #selectedAddress !== newSelectedAddress ;
@@ -298,8 +319,6 @@ export class TokenDetectionController extends StaticIntervalPollingController<
298319 }
299320 } ,
300321 ) ;
301-
302- this . #registerKeyringListeners( ) ;
303322 }
304323
305324 /**
@@ -325,21 +344,6 @@ export class TokenDetectionController extends StaticIntervalPollingController<
325344 return ! this . #disabled && this . #isUnlocked;
326345 }
327346
328- /**
329- * Constructor helper for subscribing listeners
330- * to the keyring locked state changes
331- */
332- async #registerKeyringListeners( ) {
333- this . messagingSystem . subscribe ( 'KeyringController:unlock' , async ( ) => {
334- this . #isUnlocked = true ;
335- await this . #restartTokenDetection( ) ;
336- } ) ;
337-
338- this . messagingSystem . subscribe ( 'KeyringController:lock' , ( ) => {
339- this . #isUnlocked = false ;
340- } ) ;
341- }
342-
343347 /**
344348 * Start polling for detected tokens.
345349 */
0 commit comments