Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add destroy method #1330

Merged
merged 4 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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