Skip to content

Commit de71b37

Browse files
committed
Add mixins for using polling in data services
The `polling-controller` package contains mixins for integrating the polling pattern into a class. Using these mixins on their own is somewhat cumbersome, so the package also contains utility functions which can be used in place of a superclass. Currently, there are variants of these utility functions for controllers and non-controllers, but no variant for data services. That means if engineers want to integrate polling into data services, they are out of luck. This commit corrects that by adding two new mixins: `BlockTrackerPollingDataService` (which subscribes to the block tracker) and `StaticIntervalPollingDataService` (which runs on a cadence).
1 parent ba3c869 commit de71b37

8 files changed

Lines changed: 30 additions & 0 deletions

File tree

packages/polling-controller/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Add `BlockTrackerPollingDataService` and `StaticIntervalPollingDataService` mixins for integrating the polling pattern in data services ([#9352](https://github.com/MetaMask/core/pull/9352))
13+
1014
## [16.0.8]
1115

1216
### Changed

packages/polling-controller/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
},
5353
"dependencies": {
5454
"@metamask/base-controller": "^9.1.0",
55+
"@metamask/base-data-service": "^0.1.3",
5556
"@metamask/network-controller": "^34.0.0",
5657
"@metamask/utils": "^11.11.0",
5758
"@types/uuid": "^8.3.0",

packages/polling-controller/src/BlockTrackerPollingController.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { BaseController } from '@metamask/base-controller';
2+
import { BaseDataService } from '@metamask/base-data-service';
23
import type {
34
NetworkClientId,
45
NetworkClient,
@@ -103,3 +104,13 @@ export const BlockTrackerPollingController = <
103104
BlockTrackerPollingControllerMixin<typeof BaseController, PollingInput>(
104105
BaseController,
105106
);
107+
108+
export const BlockTrackerPollingDataService = <
109+
PollingInput extends BlockTrackerPollingInput,
110+
// The return type is inferred from the class defined inside the function
111+
// scope, so this can't be easily typed.
112+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
113+
>() =>
114+
BlockTrackerPollingControllerMixin<typeof BaseDataService, PollingInput>(
115+
BaseDataService,
116+
);

packages/polling-controller/src/StaticIntervalPollingController.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { BaseController } from '@metamask/base-controller';
2+
import { BaseDataService } from '@metamask/base-data-service';
23
import type { Json } from '@metamask/utils';
34

45
import {
@@ -96,3 +97,11 @@ export const StaticIntervalPollingController = <PollingInput extends Json>() =>
9697
StaticIntervalPollingControllerMixin<typeof BaseController, PollingInput>(
9798
BaseController,
9899
);
100+
101+
// The return type is inferred from the class defined inside the function
102+
// scope, so this can't be easily typed.
103+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
104+
export const StaticIntervalPollingDataService = <PollingInput extends Json>() =>
105+
StaticIntervalPollingControllerMixin<typeof BaseDataService, PollingInput>(
106+
BaseDataService,
107+
);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
export {
22
BlockTrackerPollingControllerOnly,
33
BlockTrackerPollingController,
4+
BlockTrackerPollingDataService,
45
} from './BlockTrackerPollingController';
56

67
export {
78
StaticIntervalPollingControllerOnly,
89
StaticIntervalPollingController,
10+
StaticIntervalPollingDataService,
911
} from './StaticIntervalPollingController';
1012

1113
export type { IPollingController } from './types';

packages/polling-controller/tsconfig.build.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
},
88
"references": [
99
{ "path": "../base-controller/tsconfig.build.json" },
10+
{ "path": "../base-data-service/tsconfig.build.json" },
1011
{ "path": "../controller-utils/tsconfig.build.json" },
1112
{ "path": "../network-controller/tsconfig.build.json" },
1213
{ "path": "../messenger/tsconfig.build.json" }

packages/polling-controller/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
},
77
"references": [
88
{ "path": "../base-controller" },
9+
{ "path": "../base-data-service" },
910
{ "path": "../controller-utils" },
1011
{ "path": "../network-controller" },
1112
{ "path": "../messenger" }

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8029,6 +8029,7 @@ __metadata:
80298029
dependencies:
80308030
"@metamask/auto-changelog": "npm:^6.1.0"
80318031
"@metamask/base-controller": "npm:^9.1.0"
8032+
"@metamask/base-data-service": "npm:^0.1.3"
80328033
"@metamask/messenger": "npm:^1.2.0"
80338034
"@metamask/network-controller": "npm:^34.0.0"
80348035
"@metamask/utils": "npm:^11.11.0"

0 commit comments

Comments
 (0)