Skip to content

Commit

Permalink
small cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
adonesky1 committed Dec 7, 2023
1 parent eaec80e commit ac73471
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
26 changes: 12 additions & 14 deletions packages/polling-controller/src/PollingController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('PollingController', () => {
});

describe('setIntervalLength', () => {
it('should set getNetworkClientById if previously set to undefined when setting interval length', async () => {
it('should set getNetworkClientById (if previously set by setPollWithBlockTracker) to undefined when setting interval length', async () => {
controller.setPollWithBlockTracker(() => {
throw new Error('should not be called');
});
Expand Down Expand Up @@ -136,6 +136,7 @@ describe('PollingController', () => {
expect(controller._executePoll).toHaveBeenCalledTimes(2);
});
it('should start and stop polling sessions for different networkClientIds with the same options', async () => {
controller.setIntervalLength(TICK_TIME);
const pollToken1 = controller.startPollingByNetworkClientId('mainnet', {
address: '0x1',
});
Expand Down Expand Up @@ -180,30 +181,30 @@ describe('PollingController', () => {
controller.startPollingByNetworkClientId('mainnet');
await advanceTime({ clock, duration: 0 });

controller.startPollingByNetworkClientId('goerli');
controller.startPollingByNetworkClientId('rinkeby');
await advanceTime({ clock, duration: 0 });

expect(controller._executePoll.mock.calls).toMatchObject([
['mainnet', {}],
['goerli', {}],
['rinkeby', {}],
]);
await advanceTime({ clock, duration: TICK_TIME });

expect(controller._executePoll.mock.calls).toMatchObject([
['mainnet', {}],
['goerli', {}],
['rinkeby', {}],
['mainnet', {}],
['goerli', {}],
['rinkeby', {}],
]);
await advanceTime({ clock, duration: TICK_TIME });

expect(controller._executePoll.mock.calls).toMatchObject([
['mainnet', {}],
['goerli', {}],
['rinkeby', {}],
['mainnet', {}],
['goerli', {}],
['rinkeby', {}],
['mainnet', {}],
['goerli', {}],
['rinkeby', {}],
]);
controller.stopAllPolling();
});
Expand Down Expand Up @@ -327,10 +328,10 @@ describe('PollingController', () => {
});
});

it('should set the interval length to 0', () => {
it('should set the interval length to undefined', () => {
controller.setPollWithBlockTracker(getNetworkClientById);

expect(controller.getIntervalLength()).toBe(0);
expect(controller.getIntervalLength()).toBeUndefined();
});

it('should start polling for the specified networkClientId', async () => {
Expand Down Expand Up @@ -368,7 +369,6 @@ describe('PollingController', () => {
expect(controller._executePoll).toHaveBeenCalledTimes(1);
expect(controller._executePoll).toHaveBeenCalledWith('mainnet', {}, 1);

// Start polling for goerli, 10ms interval
await advanceTime({ clock, duration: 5 });

expect(controller._executePoll.mock.calls).toMatchObject([
Expand Down Expand Up @@ -450,11 +450,9 @@ describe('PollingController', () => {
['mainnet', {}, 2],
['mainnet', {}, 3],
]);

controller.stopAllPolling();
});

it('should should stop polling when all polling tokens for a networkClientId are deleted, even if other networkClientIds are still polling', async () => {
it('should should stop polling for one networkClientId when all polling tokens for that networkClientId are deleted, without stopping polling for networkClientIds with active pollingTokens', async () => {
controller.setPollWithBlockTracker(getNetworkClientById);

const pollingToken1 =
Expand Down
16 changes: 9 additions & 7 deletions packages/polling-controller/src/PollingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function PollingControllerMixin<TBase extends Constructor>(Base: TBase) {
Set<(networkClientId: NetworkClientId) => void>
> = new Map();

#intervalLength = 1000;
#intervalLength: number | undefined = 1000;

#getNetworkClientById:
| ((networkClientId: NetworkClientId) => NetworkClient)
Expand Down Expand Up @@ -87,7 +87,7 @@ function PollingControllerMixin<TBase extends Constructor>(Base: TBase) {
this.#getNetworkClientById = getNetworkClientById;

// using block times is mutually exclusive with polling on a static interval
this.#intervalLength = 0;
this.#intervalLength = undefined;
}

/**
Expand Down Expand Up @@ -143,13 +143,15 @@ function PollingControllerMixin<TBase extends Constructor>(Base: TBase) {
found = true;
tokenSet.delete(pollingToken);
if (tokenSet.size === 0) {
// if applicable stop polling on a static interval
if (this.#intervalIds[key]) {
clearTimeout(this.#intervalIds[key]);
delete this.#intervalIds[key];
}

// if applicable stop listening for new blocks
if (this.#getNetworkClientById !== undefined) {
} else if (
// if applicable stop listening for new blocks
this.#getNetworkClientById !== undefined &&
this.#activeListeners[key]
) {
const [networkClientId] = key.split(':');
const { blockTracker } =
this.#getNetworkClientById(networkClientId);
Expand Down Expand Up @@ -210,7 +212,7 @@ function PollingControllerMixin<TBase extends Constructor>(Base: TBase) {
}

throw new Error(`
Unable to retreive blockTracker for networkClientId ${networkClientId} `);
Unable to retrieve blockTracker for networkClientId ${networkClientId} `);
}

// if we're not polling on new blocks, use setTimeout
Expand Down

0 comments on commit ac73471

Please sign in to comment.