Skip to content

Commit 97ba10e

Browse files
committed
Refactor inspectCache atom
1 parent acf174b commit 97ba10e

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

src/hooks/useInspect.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useAtomValue, useSetAtom } from 'jotai'
1+
import { useAtom, useAtomValue } from 'jotai'
22
import {
33
Dispatch,
44
SetStateAction,
@@ -7,11 +7,7 @@ import {
77
useState
88
} from 'react'
99

10-
import {
11-
defaultInspectDepthAtom,
12-
getInspectCacheAtom,
13-
setInspectCacheAtom
14-
} from '../state'
10+
import { defaultInspectDepthAtom, inspectCacheAtom } from '../state'
1511
import type { HostPath, JsonViewerProps } from '../type'
1612
import { useIsCycleReference } from './useIsCycleReference'
1713

@@ -23,8 +19,7 @@ export function useInspect (
2319
const depth = path.length
2420
const isTrap = useIsCycleReference(path, value)
2521
const defaultInspectDepth = useAtomValue(defaultInspectDepthAtom)
26-
const inspectCache = useAtomValue(getInspectCacheAtom({ path, nestedIndex }))
27-
const setInspectCache = useSetAtom(setInspectCacheAtom)
22+
const [inspectCache, setInspectCache] = useAtom(inspectCacheAtom({ path, nestedIndex }))
2823
useEffect(() => {
2924
if (inspectCache !== undefined) {
3025
return

src/state.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,33 @@ export const collapseStringsAfterLengthAtom = atom<JsonViewerState['collapseStri
2323
export const defaultInspectDepthAtom = atom<JsonViewerState['defaultInspectDepth'] | undefined>(undefined)
2424
export const objectSortKeysAtom = atom<JsonViewerState['objectSortKeys'] | undefined>(undefined)
2525
export const quotesOnKeysAtom = atom<JsonViewerState['quotesOnKeys'] | undefined>(undefined)
26-
export const inspectCacheAtom = atom<JsonViewerState['inspectCache']>({})
2726
export const hoverPathAtom = atom<JsonViewerState['hoverPath'] | null>(null)
2827
export const registryAtom = atom<TypeRegistryState['registry']>([])
2928

29+
const _inspectCacheAtom = atom<JsonViewerState['inspectCache']>({})
3030
// TODO check: if memory leaks, add to last line of useEffect:
3131
// return () => { atomFamily.remove ... // Anything in here is fired on component unmount }
32-
export const getInspectCacheAtom = atomFamily(({ path, nestedIndex }) => atom(
32+
export const inspectCacheAtom = atomFamily(({ path, nestedIndex }) => atom(
3333
(get) => {
3434
const target = nestedIndex === undefined
3535
? path.join('.')
3636
: `${path.join('.')}[${nestedIndex}]nt`
37-
return get(inspectCacheAtom)[target]
38-
}
39-
), deepEqual)
40-
export const setInspectCacheAtom = atom(
41-
(get) => get(inspectCacheAtom),
37+
return get(_inspectCacheAtom)[target]
38+
},
4239
(get, set, { path, action, nestedIndex }) => {
4340
const target = nestedIndex === undefined
4441
? path.join('.')
4542
: `${path.join('.')}[${nestedIndex}]nt`
46-
const inspectCache = get(inspectCacheAtom)
47-
return set(inspectCacheAtom, {
43+
const inspectCache = get(_inspectCacheAtom)
44+
return set(_inspectCacheAtom, {
4845
...inspectCache,
4946
[target]: typeof action === 'function'
5047
? action(inspectCache[target])
5148
: action
5249
})
5350
}
54-
)
51+
), deepEqual)
52+
5553
export const setHoverAtom = atom(
5654
(get) => get(hoverPathAtom),
5755
(_get, set, { path, nestedIndex }) => {

0 commit comments

Comments
 (0)