diff --git a/packages/polling-controller/src/PollingController.test.ts b/packages/polling-controller/src/PollingController.test.ts index b7571c8f2c..693edf028e 100644 --- a/packages/polling-controller/src/PollingController.test.ts +++ b/packages/polling-controller/src/PollingController.test.ts @@ -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'); }); @@ -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', }); @@ -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(); }); @@ -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 () => { @@ -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([ @@ -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 = diff --git a/packages/polling-controller/src/PollingController.ts b/packages/polling-controller/src/PollingController.ts index 0080387f1f..4442391316 100644 --- a/packages/polling-controller/src/PollingController.ts +++ b/packages/polling-controller/src/PollingController.ts @@ -51,7 +51,7 @@ function PollingControllerMixin(Base: TBase) { Set<(networkClientId: NetworkClientId) => void> > = new Map(); - #intervalLength = 1000; + #intervalLength: number | undefined = 1000; #getNetworkClientById: | ((networkClientId: NetworkClientId) => NetworkClient) @@ -87,7 +87,7 @@ function PollingControllerMixin(Base: TBase) { this.#getNetworkClientById = getNetworkClientById; // using block times is mutually exclusive with polling on a static interval - this.#intervalLength = 0; + this.#intervalLength = undefined; } /** @@ -143,13 +143,15 @@ function PollingControllerMixin(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); @@ -210,7 +212,7 @@ function PollingControllerMixin(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