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

fix(useAtomDevtools): disconnect from redux devtools on unmount #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 16 additions & 10 deletions __tests__/utils/useAtomDevtools.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@ const extension = {
init: jest.fn(),
error: jest.fn(),
};
const extensionConnector = { connect: jest.fn(() => extension) };

const disconnect = () => {
extensionConnector.connect.mockClear();
extension.subscribe.mockClear();
extension.unsubscribe.mockClear();
extension.send.mockClear();
extension.init.mockClear();
extension.error.mockClear();
extensionSubscriber = undefined;
};

const extensionConnector = {
connect: jest.fn(() => extension),
disconnect: jest.fn(disconnect),
};
(window as any).__REDUX_DEVTOOLS_EXTENSION__ = extensionConnector;
describe('useAtomDevtools', () => {
beforeEach(() => {
extensionConnector.connect.mockClear();
extension.subscribe.mockClear();
extension.unsubscribe.mockClear();
extension.send.mockClear();
extension.init.mockClear();
extension.error.mockClear();
extensionSubscriber = undefined;
});
beforeEach(disconnect);

it('[DEV-ONLY] connects to the extension by initializing', () => {
__DEV__ = true;
Expand Down
5 changes: 4 additions & 1 deletion src/utils/useAtomDevtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ export function useAtomDevtools<Value, Result>(
}
});
devtools.current.shouldInit = true;
return unsubscribe;
return () => {
(extension as any).disconnect();
unsubscribe?.();
};
Comment on lines +110 to +113
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this looks like the correct fix. Thanks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I misunderstood. Thought it's connection.disconnect().

cc @PrettyCoffee

}, [anAtom, extension, atomName, setValue]);

useEffect(() => {
Expand Down