Skip to content

Commit

Permalink
Changed PollingController to be a mixin so it can be used with both V…
Browse files Browse the repository at this point in the history
…1 and V2 controllers (#1736)

## Explanation
- Currently the PollingController extends from BaseControllerV2 but we
need to use it for both V1 and V2 controllers.
- This PR uses typescript
[mixins](https://www.typescriptlang.org/docs/handbook/mixins.html) to
achieve a faux-multiple inheritance which TypeScript has support for.
The pattern
allows you to create a class which is a merge of many classes. I've
added a V1 and a V2 version of the PollingController that can be used
almost the same way as the original `PollingController`.

---------

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
  • Loading branch information
2 people authored and MajorLift committed Oct 11, 2023
1 parent ae75134 commit 6dd8ce7
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 144 deletions.
28 changes: 7 additions & 21 deletions packages/polling-controller/src/PollingController.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ControllerMessenger } from '@metamask/base-controller';

import type { PollingCompleteType } from './PollingController';
import PollingController from './PollingController';
import { PollingController } from './PollingController';

const TICK_TIME = 1000;

Expand All @@ -27,7 +26,6 @@ describe('PollingController', () => {
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME,
});
controller.start('mainnet');
jest.advanceTimersByTime(TICK_TIME);
Expand All @@ -48,7 +46,6 @@ describe('PollingController', () => {
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME,
});
const pollingToken = controller.start('mainnet');
jest.advanceTimersByTime(TICK_TIME);
Expand All @@ -69,7 +66,6 @@ describe('PollingController', () => {
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME,
});
const pollingToken1 = controller.start('mainnet');
controller.start('mainnet');
Expand All @@ -93,7 +89,6 @@ describe('PollingController', () => {
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME,
});
controller.start('mainnet');
expect(() => {
Expand All @@ -113,7 +108,6 @@ describe('PollingController', () => {
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME,
});
controller.start('mainnet');
expect(() => {
Expand All @@ -136,7 +130,6 @@ describe('PollingController', () => {
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME,
});
controller.start('mainnet');
jest.advanceTimersByTime(TICK_TIME);
Expand All @@ -158,7 +151,6 @@ describe('PollingController', () => {
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME,
});
controller.start('mainnet');
controller.start('mainnet');
Expand All @@ -177,25 +169,20 @@ describe('PollingController', () => {
}
const name = 'PollingController';

const mockMessenger = new ControllerMessenger<
any,
PollingCompleteType<typeof name>
>();

mockMessenger.subscribe(`${name}:pollingComplete`, pollingComplete);
const mockMessenger = new ControllerMessenger<any, any>();

const controller = new MyGasFeeController({
messenger: mockMessenger,
metadata: {},
name,
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME,
});
controller.onPollingComplete('mainnet', pollingComplete);
const pollingToken = controller.start('mainnet');
controller.stop(pollingToken);
expect(pollingComplete).toHaveBeenCalledTimes(1);
});
it('should poll at the interval length passed via the constructor', async () => {
it('should poll at the interval length when set via setIntervalLength', async () => {
jest.useFakeTimers();

class MyGasFeeController extends PollingController<any, any, any> {
Expand All @@ -208,8 +195,8 @@ describe('PollingController', () => {
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME * 3,
});
controller.setIntervalLength(TICK_TIME * 3);
controller.start('mainnet');
jest.advanceTimersByTime(TICK_TIME);
await Promise.resolve();
Expand Down Expand Up @@ -238,7 +225,6 @@ describe('PollingController', () => {
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME,
});
controller.start('mainnet');
controller.start('rinkeby');
Expand All @@ -259,7 +245,7 @@ describe('PollingController', () => {
controller.stopAll();
});

it('should poll multiple networkClientIds at the interval length passed via the constructor', async () => {
it('should poll multiple networkClientIds when setting interval length', async () => {
jest.useFakeTimers();

class MyGasFeeController extends PollingController<any, any, any> {
Expand All @@ -272,8 +258,8 @@ describe('PollingController', () => {
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
pollingIntervalLength: TICK_TIME * 2,
});
controller.setIntervalLength(TICK_TIME * 2);
controller.start('mainnet');
jest.advanceTimersByTime(TICK_TIME);
await Promise.resolve();
Expand Down
Loading

0 comments on commit 6dd8ce7

Please sign in to comment.