Skip to content

Commit

Permalink
Add destroy method (#1330)
Browse files Browse the repository at this point in the history
* feat: add destroy method

* test: see if blockTracker is actually undefined

* docs: apply description suggestion

Co-authored-by: Mark Stacey <markjstacey@gmail.com>

---------

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
  • Loading branch information
mikesposito and Gudahtt authored May 5, 2023
1 parent 2f0a542 commit f5c9830
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/network-controller/src/NetworkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,15 @@ export class NetworkController extends BaseControllerV2<
await this.setActiveNetwork(specifier);
}
}

/**
* Deactivates the controller, stopping any ongoing polling.
*
* In-progress requests will not be aborted.
*/
async destroy() {
await this.#blockTrackerProxy?.destroy();
}
}

export default NetworkController;
31 changes: 31 additions & 0 deletions packages/network-controller/tests/NetworkController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5520,6 +5520,37 @@ 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 }) => {
const { blockTracker } = controller.getProviderAndBlockTracker();
expect(blockTracker).toBeUndefined();
expect(async () => await controller.destroy()).not.toThrow();
});
});
});
});
});

/**
Expand Down

0 comments on commit f5c9830

Please sign in to comment.