From 09af9d5e7d400cd71e4de9cb45bf4fef3c7f2fe5 Mon Sep 17 00:00:00 2001 From: Michele Esposito Date: Fri, 5 May 2023 11:48:55 +0200 Subject: [PATCH 1/3] feat: add destroy method --- .../src/NetworkController.ts | 7 +++++ .../tests/NetworkController.test.ts | 29 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/packages/network-controller/src/NetworkController.ts b/packages/network-controller/src/NetworkController.ts index d07369e095..d486e9430e 100644 --- a/packages/network-controller/src/NetworkController.ts +++ b/packages/network-controller/src/NetworkController.ts @@ -791,6 +791,13 @@ export class NetworkController extends BaseControllerV2< await this.setActiveNetwork(specifier); } } + + /** + * Stops the block tracker polling. + */ + async destroy() { + await this.#blockTrackerProxy?.destroy(); + } } export default NetworkController; diff --git a/packages/network-controller/tests/NetworkController.test.ts b/packages/network-controller/tests/NetworkController.test.ts index 723409a753..33d7a32c24 100644 --- a/packages/network-controller/tests/NetworkController.test.ts +++ b/packages/network-controller/tests/NetworkController.test.ts @@ -5464,6 +5464,35 @@ describe('NetworkController', () => { ); }); }); + + describe('destroy', () => { + describe('if the blockTracker is defined', () => { + it('should stop the blockTracker', async () => { + await withController({}, async ({ controller }) => { + const fakeProvider = buildFakeProvider(); + const fakeNetworkClient = buildFakeClient(fakeProvider); + createNetworkClientMock.mockReturnValue(fakeNetworkClient); + await controller.initializeProvider(); + const destroySpy = jest.spyOn( + fakeNetworkClient.blockTracker, + 'destroy', + ); + + await controller.destroy(); + + expect(destroySpy).toHaveBeenCalled(); + }); + }); + }); + + describe('if the blockTracker is undefined', () => { + it('should not throw errors', async () => { + await withController({}, async ({ controller }) => { + expect(async () => await controller.destroy()).not.toThrow(); + }); + }); + }); + }); }); /** From 79c72ae614d6e051f9b058837a40a635ac7d85aa Mon Sep 17 00:00:00 2001 From: Michele Esposito Date: Fri, 5 May 2023 12:32:22 +0200 Subject: [PATCH 2/3] test: see if blockTracker is actually undefined --- packages/network-controller/tests/NetworkController.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/network-controller/tests/NetworkController.test.ts b/packages/network-controller/tests/NetworkController.test.ts index 33d7a32c24..2f9a4b4b56 100644 --- a/packages/network-controller/tests/NetworkController.test.ts +++ b/packages/network-controller/tests/NetworkController.test.ts @@ -5488,6 +5488,8 @@ describe('NetworkController', () => { describe('if the blockTracker is undefined', () => { it('should not throw errors', async () => { await withController({}, async ({ controller }) => { + const { blockTracker } = controller.getProviderAndBlockTracker(); + expect(blockTracker).toBeUndefined(); expect(async () => await controller.destroy()).not.toThrow(); }); }); From 7b7a6d412fe41a8bf2ad01a6885109146843680c Mon Sep 17 00:00:00 2001 From: Michele Esposito <34438276+mikesposito@users.noreply.github.com> Date: Fri, 5 May 2023 13:40:04 +0200 Subject: [PATCH 3/3] docs: apply description suggestion Co-authored-by: Mark Stacey --- packages/network-controller/src/NetworkController.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/network-controller/src/NetworkController.ts b/packages/network-controller/src/NetworkController.ts index 47738bf807..9b0e61b932 100644 --- a/packages/network-controller/src/NetworkController.ts +++ b/packages/network-controller/src/NetworkController.ts @@ -808,7 +808,9 @@ export class NetworkController extends BaseControllerV2< } /** - * Stops the block tracker polling. + * Deactivates the controller, stopping any ongoing polling. + * + * In-progress requests will not be aborted. */ async destroy() { await this.#blockTrackerProxy?.destroy();