Skip to content

Commit 3eb9a64

Browse files
committed
Define #registerKeyringListeners, isActive methods
1 parent feb1ff9 commit 3eb9a64

1 file changed

Lines changed: 47 additions & 5 deletions

File tree

packages/assets-controllers/src/TokenDetectionController.ts

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import {
88
safelyExecute,
99
toChecksumHexAddress,
1010
} from '@metamask/controller-utils';
11+
import type {
12+
KeyringControllerGetStateAction,
13+
KeyringControllerLockEvent,
14+
KeyringControllerUnlockEvent,
15+
} from '@metamask/keyring-controller';
1116
import type {
1217
NetworkClientId,
1318
NetworkControllerNetworkDidChangeEvent,
@@ -43,7 +48,8 @@ export type TokenDetectionControllerActions =
4348

4449
export type AllowedActions =
4550
| NetworkControllerGetNetworkConfigurationByNetworkClientId
46-
| GetTokenListState;
51+
| GetTokenListState
52+
| KeyringControllerGetStateAction;
4753

4854
export type TokenDetectionControllerStateChangeEvent =
4955
ControllerStateChangeEvent<typeof controllerName, TokenDetectionState>;
@@ -55,7 +61,9 @@ export type AllowedEvents =
5561
| AccountsControllerSelectedAccountChangeEvent
5662
| NetworkControllerStateChangeEvent
5763
| NetworkControllerNetworkDidChangeEvent
58-
| TokenListStateChange;
64+
| TokenListStateChange
65+
| KeyringControllerLockEvent
66+
| KeyringControllerUnlockEvent;
5967

6068
export type TokenDetectionControllerMessenger = RestrictedControllerMessenger<
6169
typeof controllerName,
@@ -72,6 +80,7 @@ export type TokenDetectionControllerMessenger = RestrictedControllerMessenger<
7280
* @property selectedAddress - Vault selected address
7381
* @property networkClientId - The network client ID of the current selected network
7482
* @property disabled - Boolean to track if network requests are blocked
83+
* @property isUnlocked - Boolean to track if the keyring state is unlocked
7584
* @property isDetectionEnabledFromPreferences - Boolean to track if detection is enabled from PreferencesController
7685
* @property isDetectionEnabledForNetwork - Boolean to track if detected is enabled for current network
7786
*/
@@ -90,6 +99,8 @@ export class TokenDetectionController extends StaticIntervalPollingController<
9099

91100
#disabled: boolean;
92101

102+
#isUnlocked?: boolean;
103+
93104
#isDetectionEnabledFromPreferences: boolean;
94105

95106
#isDetectionEnabledForNetwork: boolean;
@@ -231,6 +242,8 @@ export class TokenDetectionController extends StaticIntervalPollingController<
231242
}
232243
},
233244
);
245+
246+
this.#registerKeyringListeners();
234247
}
235248

236249
/**
@@ -247,6 +260,35 @@ export class TokenDetectionController extends StaticIntervalPollingController<
247260
this.#disabled = true;
248261
}
249262

263+
/**
264+
* Internal isActive state
265+
*
266+
* @type {object}
267+
*/
268+
get isActive() {
269+
return !this.#disabled && this.#isUnlocked;
270+
}
271+
272+
/**
273+
* Constructor helper for subscribing listeners
274+
* to the keyring locked state changes
275+
*/
276+
async #registerKeyringListeners() {
277+
const { isUnlocked } = this.messagingSystem.call(
278+
'KeyringController:getState',
279+
);
280+
this.#isUnlocked = isUnlocked;
281+
282+
this.messagingSystem.subscribe('KeyringController:unlock', async () => {
283+
this.#isUnlocked = true;
284+
await this.#restartTokenDetection();
285+
});
286+
287+
this.messagingSystem.subscribe('KeyringController:lock', () => {
288+
this.#isUnlocked = false;
289+
});
290+
}
291+
250292
/**
251293
* Start polling for detected tokens.
252294
*/
@@ -273,7 +315,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<
273315
* Starts a new polling interval.
274316
*/
275317
async #startPolling(): Promise<void> {
276-
if (this.#disabled) {
318+
if (!this.isActive) {
277319
return;
278320
}
279321
this.#stopPolling();
@@ -296,7 +338,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<
296338
networkClientId: string,
297339
options: { address: string },
298340
): Promise<void> {
299-
if (this.#disabled) {
341+
if (!this.isActive) {
300342
return;
301343
}
302344
await this.detectTokens({
@@ -339,7 +381,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<
339381
accountAddress?: string;
340382
} = {}): Promise<void> {
341383
if (
342-
this.#disabled ||
384+
!this.isActive ||
343385
!this.#isDetectionEnabledForNetwork ||
344386
!this.#isDetectionEnabledFromPreferences
345387
) {

0 commit comments

Comments
 (0)