Skip to content

Commit

Permalink
add IPollingController type
Browse files Browse the repository at this point in the history
  • Loading branch information
adonesky1 committed Dec 11, 2023
1 parent 5ccf0d3 commit 997c806
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 32 deletions.
14 changes: 9 additions & 5 deletions packages/assets-controllers/src/CurrencyRateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
NetworkClientId,
NetworkControllerGetNetworkClientByIdAction,
} from '@metamask/network-controller';
import type { IPollingController } from '@metamask/polling-controller';
import { StaticIntervalPollingController } from '@metamask/polling-controller';
import { Mutex } from 'async-mutex';

Expand Down Expand Up @@ -82,11 +83,14 @@ const defaultState = {
* Controller that passively polls on a set interval for an exchange rate from the current network
* asset to the user's preferred currency.
*/
export class CurrencyRateController extends StaticIntervalPollingController<
typeof name,
CurrencyRateState,
CurrencyRateMessenger
> {
export class CurrencyRateController
extends StaticIntervalPollingController<
typeof name,
CurrencyRateState,
CurrencyRateMessenger
>
implements IPollingController
{
private readonly mutex = new Mutex();

private readonly fetchExchangeRate;
Expand Down
9 changes: 5 additions & 4 deletions packages/assets-controllers/src/NftDetectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
NetworkState,
NetworkClient,
} from '@metamask/network-controller';
import type { IPollingController } from '@metamask/polling-controller';
import { StaticIntervalPollingControllerV1 } from '@metamask/polling-controller';
import type { PreferencesState } from '@metamask/preferences-controller';
import type { Hex } from '@metamask/utils';
Expand Down Expand Up @@ -147,10 +148,10 @@ export interface NftDetectionConfig extends BaseConfig {
/**
* Controller that passively polls on a set interval for NFT auto detection
*/
export class NftDetectionController extends StaticIntervalPollingControllerV1<
NftDetectionConfig,
BaseState
> {
export class NftDetectionController
extends StaticIntervalPollingControllerV1<NftDetectionConfig, BaseState>
implements IPollingController
{
private intervalId?: ReturnType<typeof setTimeout>;

private getOwnerNftApi({
Expand Down
9 changes: 5 additions & 4 deletions packages/assets-controllers/src/TokenDetectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
NetworkController,
NetworkState,
} from '@metamask/network-controller';
import type { IPollingController } from '@metamask/polling-controller';
import { StaticIntervalPollingControllerV1 } from '@metamask/polling-controller';
import type { PreferencesState } from '@metamask/preferences-controller';
import type { Hex } from '@metamask/utils';
Expand Down Expand Up @@ -44,10 +45,10 @@ export interface TokenDetectionConfig extends BaseConfig {
/**
* Controller that passively polls on a set interval for Tokens auto detection
*/
export class TokenDetectionController extends StaticIntervalPollingControllerV1<
TokenDetectionConfig,
BaseState
> {
export class TokenDetectionController
extends StaticIntervalPollingControllerV1<TokenDetectionConfig, BaseState>
implements IPollingController
{
private intervalId?: ReturnType<typeof setTimeout>;

/**
Expand Down
14 changes: 9 additions & 5 deletions packages/assets-controllers/src/TokenListController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
NetworkState,
NetworkControllerGetNetworkClientByIdAction,
} from '@metamask/network-controller';
import type { IPollingController } from '@metamask/polling-controller';
import { StaticIntervalPollingController } from '@metamask/polling-controller';
import type { Hex } from '@metamask/utils';
import { Mutex } from 'async-mutex';
Expand Down Expand Up @@ -91,11 +92,14 @@ const defaultState: TokenListState = {
/**
* Controller that passively polls on a set interval for the list of tokens from metaswaps api
*/
export class TokenListController extends StaticIntervalPollingController<
typeof name,
TokenListState,
TokenListMessenger
> {
export class TokenListController
extends StaticIntervalPollingController<
typeof name,
TokenListState,
TokenListMessenger
>
implements IPollingController
{
private readonly mutex = new Mutex();

private intervalId?: ReturnType<typeof setTimeout>;
Expand Down
9 changes: 5 additions & 4 deletions packages/assets-controllers/src/TokenRatesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
NetworkController,
NetworkState,
} from '@metamask/network-controller';
import type { IPollingController } from '@metamask/polling-controller';
import { StaticIntervalPollingControllerV1 } from '@metamask/polling-controller';
import type { PreferencesState } from '@metamask/preferences-controller';
import type { Hex } from '@metamask/utils';
Expand Down Expand Up @@ -135,10 +136,10 @@ async function getCurrencyConversionRate({
* Controller that passively polls on a set interval for token-to-fiat exchange rates
* for tokens stored in the TokensController
*/
export class TokenRatesController extends StaticIntervalPollingControllerV1<
TokenRatesConfig,
TokenRatesState
> {
export class TokenRatesController
extends StaticIntervalPollingControllerV1<TokenRatesConfig, TokenRatesState>
implements IPollingController
{
private handle?: ReturnType<typeof setTimeout>;

#pollState = PollState.Inactive;
Expand Down
14 changes: 9 additions & 5 deletions packages/gas-fee-controller/src/GasFeeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
NetworkState,
ProviderProxy,
} from '@metamask/network-controller';
import type { IPollingController } from '@metamask/polling-controller';
import { StaticIntervalPollingController } from '@metamask/polling-controller';
import type { Hex } from '@metamask/utils';
import { v1 as random } from 'uuid';
Expand Down Expand Up @@ -253,11 +254,14 @@ const defaultState: GasFeeState = {
/**
* Controller that retrieves gas fee estimate data and polls for updated data on a set interval
*/
export class GasFeeController extends StaticIntervalPollingController<
typeof name,
GasFeeState,
GasFeeMessenger
> {
export class GasFeeController
extends StaticIntervalPollingController<
typeof name,
GasFeeState,
GasFeeMessenger
>
implements IPollingController
{
private intervalId?: ReturnType<typeof setTimeout>;

private readonly intervalDelay;
Expand Down
29 changes: 28 additions & 1 deletion packages/polling-controller/src/AbstractPollingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@ import type { Json } from '@metamask/utils';
import stringify from 'fast-json-stable-stringify';
import { v4 as random } from 'uuid';

export type IPollingController = {
startPollingByNetworkClientId(
networkClientId: NetworkClientId,
options: Json,
): string;

stopAllPolling(): void;

stopPollingByPollingToken(pollingToken: string): void;

onPollingCompleteByNetworkClientId(
networkClientId: NetworkClientId,
callback: (networkClientId: NetworkClientId) => void,
options: Json,
): void;

_executePoll(networkClientId: NetworkClientId, options: Json): Promise<void>;
_startPollingByNetworkClientId(
networkClientId: NetworkClientId,
options: Json,
): void;
_stopPollingByPollingTokenSetId(key: PollingTokenSetId): void;
};

export const getKey = (
networkClientId: NetworkClientId,
options: Json,
Expand All @@ -21,7 +45,10 @@ type Constructor = new (...args: any[]) => object;
export function AbstractPollingControllerBaseMixin<TBase extends Constructor>(
Base: TBase,
) {
abstract class AbstractPollingControllerBase extends Base {
abstract class AbstractPollingControllerBase
extends Base
implements IPollingController
{
readonly #pollingTokenSets: Map<PollingTokenSetId, Set<string>> = new Map();

#callbacks: Map<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import {
AbstractPollingControllerBaseMixin,
getKey,
} from './AbstractPollingController';
import type { PollingTokenSetId } from './AbstractPollingController';
import type {
IPollingController,
PollingTokenSetId,
} from './AbstractPollingController';

type Constructor = new (...args: any[]) => object;

Expand All @@ -20,9 +23,10 @@ type Constructor = new (...args: any[]) => object;
function StaticIntervalPollingControllerMixin<TBase extends Constructor>(
Base: TBase,
) {
abstract class StaticIntervalPollingController extends AbstractPollingControllerBaseMixin(
Base,
) {
abstract class StaticIntervalPollingController
extends AbstractPollingControllerBaseMixin(Base)
implements IPollingController
{
readonly #intervalIds: Record<PollingTokenSetId, NodeJS.Timeout> = {};

intervalLength: number | undefined = 1000;
Expand Down
2 changes: 2 additions & 0 deletions packages/polling-controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export {
StaticIntervalPollingController,
StaticIntervalPollingControllerV1,
} from './StaticIntervalPollingController';

export type { IPollingController } from './AbstractPollingController';

0 comments on commit 997c806

Please sign in to comment.