Skip to content

Commit 9ea41b8

Browse files
committed
Refactor inspectCache atom
1 parent 2859eca commit 9ea41b8

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

src/hooks/useInspect.ts

Lines changed: 3 additions & 8 deletions
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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,33 @@ export const collapseStringsAfterLengthAtom = atom<JsonViewerState['collapseStri
2222
export const defaultInspectDepthAtom = atom<JsonViewerState['defaultInspectDepth'] | undefined>(undefined)
2323
export const objectSortKeysAtom = atom<JsonViewerState['objectSortKeys'] | undefined>(undefined)
2424
export const quotesOnKeysAtom = atom<JsonViewerState['quotesOnKeys'] | undefined>(undefined)
25-
export const inspectCacheAtom = atom<JsonViewerState['inspectCache']>({})
2625
export const hoverPathAtom = atom<JsonViewerState['hoverPath'] | null>(null)
2726
export const registryAtom = atom<TypeRegistryState['registry']>([])
2827

28+
const _inspectCacheAtom = atom<JsonViewerState['inspectCache']>({})
2929
// TODO check: if memory leaks, add to last line of useEffect:
3030
// return () => { atomFamily.remove ... // Anything in here is fired on component unmount }
31-
export const getInspectCacheAtom = atomFamily(({ path, nestedIndex }) => atom(
31+
export const inspectCacheAtom = atomFamily(({ path, nestedIndex }) => atom(
3232
(get) => {
3333
const target = nestedIndex === undefined
3434
? path.join('.')
3535
: `${path.join('.')}[${nestedIndex}]nt`
36-
return get(inspectCacheAtom)[target]
37-
}
38-
), deepEqual)
39-
export const setInspectCacheAtom = atom(
40-
(get) => get(inspectCacheAtom),
36+
return get(_inspectCacheAtom)[target]
37+
},
4138
(get, set, { path, action, nestedIndex }) => {
4239
const target = nestedIndex === undefined
4340
? path.join('.')
4441
: `${path.join('.')}[${nestedIndex}]nt`
45-
const inspectCache = get(inspectCacheAtom)
46-
return set(inspectCacheAtom, {
42+
const inspectCache = get(_inspectCacheAtom)
43+
return set(_inspectCacheAtom, {
4744
...inspectCache,
4845
[target]: typeof action === 'function'
4946
? action(inspectCache[target])
5047
: action
5148
})
5249
}
53-
)
50+
), deepEqual)
51+
5452
export const setHoverAtom = atom(
5553
(get) => get(hoverPathAtom),
5654
(_get, set, { path, nestedIndex }) => {

0 commit comments

Comments
 (0)