Skip to content

Commit a9ea7e6

Browse files
committed
address feedback
1 parent 90eecae commit a9ea7e6

10 files changed

+72
-80
lines changed

packages/assets-controllers/src/CurrencyRateController.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import type {
1111
NetworkClientId,
1212
NetworkControllerGetNetworkClientByIdAction,
1313
} from '@metamask/network-controller';
14-
import type { IPollingController } from '@metamask/polling-controller';
1514
import { StaticIntervalPollingController } from '@metamask/polling-controller';
1615
import { Mutex } from 'async-mutex';
1716

@@ -83,14 +82,11 @@ const defaultState = {
8382
* Controller that passively polls on a set interval for an exchange rate from the current network
8483
* asset to the user's preferred currency.
8584
*/
86-
export class CurrencyRateController
87-
extends StaticIntervalPollingController<
88-
typeof name,
89-
CurrencyRateState,
90-
CurrencyRateMessenger
91-
>
92-
implements IPollingController
93-
{
85+
export class CurrencyRateController extends StaticIntervalPollingController<
86+
typeof name,
87+
CurrencyRateState,
88+
CurrencyRateMessenger
89+
> {
9490
private readonly mutex = new Mutex();
9591

9692
private readonly fetchExchangeRate;

packages/assets-controllers/src/NftDetectionController.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import type {
1111
NetworkState,
1212
NetworkClient,
1313
} from '@metamask/network-controller';
14-
import type { IPollingController } from '@metamask/polling-controller';
1514
import { StaticIntervalPollingControllerV1 } from '@metamask/polling-controller';
1615
import type { PreferencesState } from '@metamask/preferences-controller';
1716
import type { Hex } from '@metamask/utils';
@@ -148,10 +147,10 @@ export interface NftDetectionConfig extends BaseConfig {
148147
/**
149148
* Controller that passively polls on a set interval for NFT auto detection
150149
*/
151-
export class NftDetectionController
152-
extends StaticIntervalPollingControllerV1<NftDetectionConfig, BaseState>
153-
implements IPollingController
154-
{
150+
export class NftDetectionController extends StaticIntervalPollingControllerV1<
151+
NftDetectionConfig,
152+
BaseState
153+
> {
155154
private intervalId?: ReturnType<typeof setTimeout>;
156155

157156
private getOwnerNftApi({

packages/assets-controllers/src/TokenDetectionController.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import type {
88
NetworkController,
99
NetworkState,
1010
} from '@metamask/network-controller';
11-
import type { IPollingController } from '@metamask/polling-controller';
1211
import { StaticIntervalPollingControllerV1 } from '@metamask/polling-controller';
1312
import type { PreferencesState } from '@metamask/preferences-controller';
1413
import type { Hex } from '@metamask/utils';
@@ -45,10 +44,10 @@ export interface TokenDetectionConfig extends BaseConfig {
4544
/**
4645
* Controller that passively polls on a set interval for Tokens auto detection
4746
*/
48-
export class TokenDetectionController
49-
extends StaticIntervalPollingControllerV1<TokenDetectionConfig, BaseState>
50-
implements IPollingController
51-
{
47+
export class TokenDetectionController extends StaticIntervalPollingControllerV1<
48+
TokenDetectionConfig,
49+
BaseState
50+
> {
5251
private intervalId?: ReturnType<typeof setTimeout>;
5352

5453
/**

packages/assets-controllers/src/TokenListController.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import type {
1010
NetworkState,
1111
NetworkControllerGetNetworkClientByIdAction,
1212
} from '@metamask/network-controller';
13-
import type { IPollingController } from '@metamask/polling-controller';
1413
import { StaticIntervalPollingController } from '@metamask/polling-controller';
1514
import type { Hex } from '@metamask/utils';
1615
import { Mutex } from 'async-mutex';
@@ -92,14 +91,11 @@ const defaultState: TokenListState = {
9291
/**
9392
* Controller that passively polls on a set interval for the list of tokens from metaswaps api
9493
*/
95-
export class TokenListController
96-
extends StaticIntervalPollingController<
97-
typeof name,
98-
TokenListState,
99-
TokenListMessenger
100-
>
101-
implements IPollingController
102-
{
94+
export class TokenListController extends StaticIntervalPollingController<
95+
typeof name,
96+
TokenListState,
97+
TokenListMessenger
98+
> {
10399
private readonly mutex = new Mutex();
104100

105101
private intervalId?: ReturnType<typeof setTimeout>;

packages/assets-controllers/src/TokenRatesController.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import type {
1010
NetworkController,
1111
NetworkState,
1212
} from '@metamask/network-controller';
13-
import type { IPollingController } from '@metamask/polling-controller';
1413
import { StaticIntervalPollingControllerV1 } from '@metamask/polling-controller';
1514
import type { PreferencesState } from '@metamask/preferences-controller';
1615
import type { Hex } from '@metamask/utils';
@@ -137,10 +136,10 @@ async function getCurrencyConversionRate({
137136
* Controller that passively polls on a set interval for token-to-fiat exchange rates
138137
* for tokens stored in the TokensController
139138
*/
140-
export class TokenRatesController
141-
extends StaticIntervalPollingControllerV1<TokenRatesConfig, TokenRatesState>
142-
implements IPollingController
143-
{
139+
export class TokenRatesController extends StaticIntervalPollingControllerV1<
140+
TokenRatesConfig,
141+
TokenRatesState
142+
> {
144143
private handle?: ReturnType<typeof setTimeout>;
145144

146145
#pollState = PollState.Inactive;

packages/gas-fee-controller/src/GasFeeController.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import type {
1818
NetworkState,
1919
ProviderProxy,
2020
} from '@metamask/network-controller';
21-
import type { IPollingController } from '@metamask/polling-controller';
2221
import { StaticIntervalPollingController } from '@metamask/polling-controller';
2322
import type { Hex } from '@metamask/utils';
2423
import { v1 as random } from 'uuid';
@@ -254,14 +253,11 @@ const defaultState: GasFeeState = {
254253
/**
255254
* Controller that retrieves gas fee estimate data and polls for updated data on a set interval
256255
*/
257-
export class GasFeeController
258-
extends StaticIntervalPollingController<
259-
typeof name,
260-
GasFeeState,
261-
GasFeeMessenger
262-
>
263-
implements IPollingController
264-
{
256+
export class GasFeeController extends StaticIntervalPollingController<
257+
typeof name,
258+
GasFeeState,
259+
GasFeeMessenger
260+
> {
265261
private intervalId?: ReturnType<typeof setTimeout>;
266262

267263
private readonly intervalDelay;

packages/polling-controller/src/AbstractPollingController.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function AbstractPollingControllerBaseMixin<TBase extends Constructor>(
7575
const pollToken = random();
7676
const key = getKey(networkClientId, options);
7777
const pollingTokenSet =
78-
this.#pollingTokenSets.get(key) || new Set<string>();
78+
this.#pollingTokenSets.get(key) ?? new Set<string>();
7979
pollingTokenSet.add(pollToken);
8080
this.#pollingTokenSets.set(key, pollingTokenSet);
8181

@@ -110,15 +110,16 @@ export function AbstractPollingControllerBaseMixin<TBase extends Constructor>(
110110
}
111111

112112
if (keyToDelete) {
113-
// TODO figure out why this is necessary
114-
const nonNullKey = keyToDelete;
115-
this._stopPollingByPollingTokenSetId(nonNullKey);
116-
this.#pollingTokenSets.delete(nonNullKey);
117-
this.#callbacks.get(nonNullKey)?.forEach((callback) => {
118-
// for some reason this typescript can't tell that keyToDelete is not null here
119-
callback(nonNullKey);
120-
});
121-
this.#callbacks.get(nonNullKey)?.clear();
113+
this._stopPollingByPollingTokenSetId(keyToDelete);
114+
this.#pollingTokenSets.delete(keyToDelete);
115+
const callbacks = this.#callbacks.get(keyToDelete);
116+
if (callbacks) {
117+
for (const callback of callbacks) {
118+
// eslint-disable-next-line n/callback-return
119+
callback(keyToDelete);
120+
}
121+
callbacks.clear();
122+
}
122123
}
123124
}
124125

@@ -128,7 +129,7 @@ export function AbstractPollingControllerBaseMixin<TBase extends Constructor>(
128129
options: Json = {},
129130
) {
130131
const key = getKey(networkClientId, options);
131-
const callbacks = this.#callbacks.get(key) || new Set<typeof callback>();
132+
const callbacks = this.#callbacks.get(key) ?? new Set<typeof callback>();
132133
callbacks.add(callback);
133134
this.#callbacks.set(key, callbacks);
134135
}

packages/polling-controller/src/BlockTrackerPollingController.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,15 @@ describe('BlockTrackerPollingController', () => {
254254
controller.stopAllPolling();
255255
});
256256
});
257+
258+
describe('onPollingCompleteByNetworkClientId', () => {
259+
it('should publish "pollingComplete" callback function set by "onPollingCompleteByNetworkClientId" when polling stops', async () => {
260+
const pollingComplete: any = jest.fn();
261+
controller.onPollingCompleteByNetworkClientId('mainnet', pollingComplete);
262+
const pollingToken = controller.startPollingByNetworkClientId('mainnet');
263+
controller.stopPollingByPollingToken(pollingToken);
264+
expect(pollingComplete).toHaveBeenCalledTimes(1);
265+
expect(pollingComplete).toHaveBeenCalledWith('mainnet:{}');
266+
});
267+
});
257268
});

packages/polling-controller/src/StaticIntervalPollingController.test.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useFakeTimers } from 'sinon';
44
import { advanceTime } from '../../../tests/helpers';
55
import { StaticIntervalPollingController } from './StaticIntervalPollingController';
66

7-
const TICK_TIME = 1000;
7+
const TICK_TIME = 5;
88

99
const createExecutePollMock = () => {
1010
const executePollMock = jest.fn().mockImplementation(async () => {
@@ -19,8 +19,6 @@ class ChildBlockTrackerPollingController extends StaticIntervalPollingController
1919
any
2020
> {
2121
_executePoll = createExecutePollMock();
22-
23-
_intervalLength = TICK_TIME;
2422
}
2523

2624
describe('StaticIntervalPollingController', () => {
@@ -35,29 +33,24 @@ describe('StaticIntervalPollingController', () => {
3533
name: 'PollingController',
3634
state: { foo: 'bar' },
3735
});
36+
controller.setIntervalLength(TICK_TIME);
3837
clock = useFakeTimers();
3938
});
4039
afterEach(() => {
4140
clock.restore();
4241
});
4342

4443
describe('startPollingByNetworkClientId', () => {
45-
it('should start polling if not polling', async () => {
44+
it('should start polling if not already polling', async () => {
4645
controller.startPollingByNetworkClientId('mainnet');
4746
await advanceTime({ clock, duration: 0 });
4847
expect(controller._executePoll).toHaveBeenCalledTimes(1);
4948
await advanceTime({ clock, duration: TICK_TIME });
5049
expect(controller._executePoll).toHaveBeenCalledTimes(2);
5150
controller.stopAllPolling();
5251
});
53-
it('should call _executePoll immediately and on interval if polling', async () => {
54-
controller.startPollingByNetworkClientId('mainnet');
55-
await advanceTime({ clock, duration: 0 });
56-
expect(controller._executePoll).toHaveBeenCalledTimes(1);
57-
await advanceTime({ clock, duration: TICK_TIME * 2 });
58-
expect(controller._executePoll).toHaveBeenCalledTimes(3);
59-
});
60-
it('should call _executePoll immediately once and continue calling _executePoll on interval when start is called again with the same networkClientId', async () => {
52+
53+
it('should call _executePoll immediately once and continue calling _executePoll on interval when called again with the same networkClientId', async () => {
6154
controller.startPollingByNetworkClientId('mainnet');
6255
await advanceTime({ clock, duration: 0 });
6356

@@ -184,20 +177,11 @@ describe('StaticIntervalPollingController', () => {
184177
it('should error if no pollingToken is passed', () => {
185178
controller.startPollingByNetworkClientId('mainnet');
186179
expect(() => {
187-
controller.stopPollingByPollingToken(undefined as unknown as any);
180+
controller.stopPollingByPollingToken();
188181
}).toThrow('pollingToken required');
189182
controller.stopAllPolling();
190183
});
191184

192-
it('should publish "pollingComplete" when stop is called', async () => {
193-
const pollingComplete: any = jest.fn();
194-
controller.onPollingCompleteByNetworkClientId('mainnet', pollingComplete);
195-
const pollingToken = controller.startPollingByNetworkClientId('mainnet');
196-
controller.stopPollingByPollingToken(pollingToken);
197-
expect(pollingComplete).toHaveBeenCalledTimes(1);
198-
expect(pollingComplete).toHaveBeenCalledWith('mainnet:{}');
199-
});
200-
201185
it('should start and stop polling sessions for different networkClientIds with the same options', async () => {
202186
controller.setIntervalLength(TICK_TIME);
203187
const pollToken1 = controller.startPollingByNetworkClientId('mainnet', {
@@ -239,4 +223,15 @@ describe('StaticIntervalPollingController', () => {
239223
]);
240224
});
241225
});
226+
227+
describe('onPollingCompleteByNetworkClientId', () => {
228+
it('should publish "pollingComplete" callback function set by "onPollingCompleteByNetworkClientId" when polling stops', async () => {
229+
const pollingComplete: any = jest.fn();
230+
controller.onPollingCompleteByNetworkClientId('mainnet', pollingComplete);
231+
const pollingToken = controller.startPollingByNetworkClientId('mainnet');
232+
controller.stopPollingByPollingToken(pollingToken);
233+
expect(pollingComplete).toHaveBeenCalledTimes(1);
234+
expect(pollingComplete).toHaveBeenCalledWith('mainnet:{}');
235+
});
236+
});
242237
});

packages/polling-controller/src/StaticIntervalPollingController.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@ function StaticIntervalPollingControllerMixin<TBase extends Constructor>(
2929
{
3030
readonly #intervalIds: Record<PollingTokenSetId, NodeJS.Timeout> = {};
3131

32-
intervalLength: number | undefined = 1000;
32+
#intervalLength: number | undefined = 1000;
3333

3434
setIntervalLength(intervalLength: number) {
35-
this.intervalLength = intervalLength;
35+
this.#intervalLength = intervalLength;
3636
}
3737

3838
getIntervalLength() {
39-
return this.intervalLength;
39+
return this.#intervalLength;
4040
}
4141

4242
_startPollingByNetworkClientId(
4343
networkClientId: NetworkClientId,
4444
options: Json,
4545
) {
46-
if (!this.intervalLength) {
46+
if (!this.#intervalLength) {
4747
throw new Error('intervalLength must be defined and greater than 0');
4848
}
4949

@@ -60,7 +60,7 @@ function StaticIntervalPollingControllerMixin<TBase extends Constructor>(
6060
}
6161
this._startPollingByNetworkClientId(networkClientId, options);
6262
},
63-
existingInterval ? this.intervalLength : 0,
63+
existingInterval ? this.#intervalLength : 0,
6464
);
6565
}
6666

0 commit comments

Comments
 (0)