Skip to content

Commit 1c31e97

Browse files
committed
refactor(utils): extract getReduxExtension
1 parent c5e1401 commit 1c31e97

File tree

3 files changed

+45
-27
lines changed

3 files changed

+45
-27
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Original but incomplete type of the redux extension package
2+
type Extension = NonNullable<typeof window.__REDUX_DEVTOOLS_EXTENSION__>;
3+
4+
export type ReduxExtension = {
5+
/** Create a connection to the extension.
6+
* This will connect a store (like an atom) to the extension and
7+
* display it within the extension tab.
8+
*
9+
* @param options https://github.com/reduxjs/redux-devtools/blob/main/extension/docs/API/Arguments.md
10+
* @returns https://github.com/reduxjs/redux-devtools/blob/main/extension/docs/API/Methods.md#connectoptions
11+
*/
12+
connect: Extension['connect'];
13+
14+
/** Disconnects all existing connections to the redux extension.
15+
* Only use this when you are sure that no other connection exists
16+
* or you want to remove all existing connections.
17+
*/
18+
disconnect?: () => void;
19+
20+
/** Have a look at the documentation for more methods:
21+
* https://github.com/reduxjs/redux-devtools/blob/main/extension/docs/API/Methods.md
22+
*/
23+
};
24+
25+
/** Returns the global redux extension object if available */
26+
export const getReduxExtension = (
27+
enabled = __DEV__,
28+
): ReduxExtension | undefined => {
29+
if (!enabled) {
30+
return undefined;
31+
}
32+
33+
const reduxExtension = window.__REDUX_DEVTOOLS_EXTENSION__;
34+
if (!reduxExtension && __DEV__) {
35+
console.warn('Please install/enable Redux devtools extension');
36+
return undefined;
37+
}
38+
39+
return reduxExtension;
40+
};

src/utils/useAtomDevtools.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useEffect, useRef } from 'react';
22
import { useAtom } from 'jotai/react';
33
import type { Atom, WritableAtom } from 'jotai/vanilla';
4+
import { getReduxExtension } from './redux-extension/getReduxExtension';
45
import { Message } from './types';
56

67
type DevtoolOptions = Parameters<typeof useAtom>[1] & {
@@ -14,19 +15,7 @@ export function useAtomDevtools<Value, Result>(
1415
): void {
1516
const { enabled, name } = options || {};
1617

17-
let extension: typeof window['__REDUX_DEVTOOLS_EXTENSION__'] | false;
18-
19-
try {
20-
extension = (enabled ?? __DEV__) && window.__REDUX_DEVTOOLS_EXTENSION__;
21-
} catch {
22-
// ignored
23-
}
24-
25-
if (!extension) {
26-
if (__DEV__ && enabled) {
27-
console.warn('Please install/enable Redux devtools extension');
28-
}
29-
}
18+
const extension = getReduxExtension(enabled);
3019

3120
const [value, setValue] = useAtom(anAtom, options);
3221

src/utils/useAtomsDevtools.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useEffect, useRef } from 'react';
22
import { AnyAtom, AnyAtomValue, AtomsSnapshot, Options } from '../types';
3+
import { getReduxExtension } from './redux-extension/getReduxExtension';
34
import { Message } from './types';
45
import { useAtomsSnapshot } from './useAtomsSnapshot';
56
import { useGotoAtomsSnapshot } from './useGotoAtomsSnapshot';
@@ -32,19 +33,7 @@ export function useAtomsDevtools(
3233
): void {
3334
const { enabled } = options || {};
3435

35-
let extension: typeof window['__REDUX_DEVTOOLS_EXTENSION__'] | false;
36-
37-
try {
38-
extension = (enabled ?? __DEV__) && window.__REDUX_DEVTOOLS_EXTENSION__;
39-
} catch {
40-
// ignored
41-
}
42-
43-
if (!extension) {
44-
if (__DEV__ && enabled) {
45-
console.warn('Please install/enable Redux devtools extension');
46-
}
47-
}
36+
const extension = getReduxExtension(enabled);
4837

4938
// This an exception, we don't usually use utils in themselves!
5039
const atomsSnapshot = useAtomsSnapshot(options);
@@ -112,7 +101,7 @@ export function useAtomsDevtools(
112101
devtools.current = connection;
113102
devtools.current.shouldInit = true;
114103
return () => {
115-
(extension as any).disconnect();
104+
extension?.disconnect?.();
116105
devtoolsUnsubscribe?.();
117106
};
118107
}, [extension, goToSnapshot, name]);

0 commit comments

Comments
 (0)