Skip to content

Commit eded53b

Browse files
committed
Fix inspectCache atom
1 parent 9360403 commit eded53b

File tree

5 files changed

+30
-18
lines changed

5 files changed

+30
-18
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"@emotion/styled": "^11.10.6",
6666
"@mui/material": "^5.11.13",
6767
"copy-to-clipboard": "^3.3.3",
68+
"fast-deep-equal": "^3.1.3",
6869
"jotai": "^1.13.0"
6970
},
7071
"lint-staged": {

src/hooks/useInspect.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@ import {
1212
getInspectCacheAtom,
1313
setInspectCacheAtom
1414
} from '../state'
15+
import type { HostPath, JsonViewerProps } from '../type'
1516
import { useIsCycleReference } from './useIsCycleReference'
1617

17-
export function useInspect (path: (string | number)[], value: any, nestedIndex?: number) {
18+
export function useInspect (
19+
path: HostPath['path'],
20+
value: JsonViewerProps['value'],
21+
nestedIndex?: HostPath['nestedIndex']
22+
) {
1823
const depth = path.length
1924
const isTrap = useIsCycleReference(path, value)
2025
const defaultInspectDepth = useAtomValue(defaultInspectDepthAtom)
21-
const getInspectCache = useSetAtom(getInspectCacheAtom)
26+
const inspectCache = useAtomValue(getInspectCacheAtom({ path, nestedIndex }))
2227
const setInspectCache = useSetAtom(setInspectCacheAtom)
2328
useEffect(() => {
24-
const inspect = getInspectCache({ path, nestedIndex })
25-
if (inspect !== undefined) {
29+
if (inspectCache !== undefined) {
2630
return
2731
}
2832
if (nestedIndex !== undefined) {
@@ -34,11 +38,10 @@ export function useInspect (path: (string | number)[], value: any, nestedIndex?:
3438
: depth < defaultInspectDepth
3539
setInspectCache({ path, inspect })
3640
}
37-
}, [defaultInspectDepth, depth, isTrap, nestedIndex, path, getInspectCache, setInspectCache])
41+
}, [defaultInspectDepth, depth, isTrap, nestedIndex, path, inspectCache, setInspectCache])
3842
const [inspect, set] = useState<boolean>(() => {
39-
const shouldInspect = getInspectCache({ path, nestedIndex })
40-
if (shouldInspect !== undefined) {
41-
return shouldInspect
43+
if (inspectCache !== undefined) {
44+
return inspectCache
4245
}
4346
if (nestedIndex !== undefined) {
4447
return false

src/state.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import deepEqual from 'fast-deep-equal'
12
import { atom } from 'jotai'
3+
import { atomFamily } from 'jotai/utils'
24

35
import { lightColorspace } from './theme/base16'
46
import type { JsonViewerState, TypeRegistryState } from './type'
@@ -21,19 +23,20 @@ export const collapseStringsAfterLengthAtom = atom<JsonViewerState['collapseStri
2123
export const defaultInspectDepthAtom = atom<JsonViewerState['defaultInspectDepth'] | undefined>(undefined)
2224
export const objectSortKeysAtom = atom<JsonViewerState['objectSortKeys'] | undefined>(undefined)
2325
export const quotesOnKeysAtom = atom<JsonViewerState['quotesOnKeys'] | undefined>(undefined)
24-
export const inspectCacheAtom = atom<JsonViewerState['inspectCache'] | undefined>({})
26+
export const inspectCacheAtom = atom<JsonViewerState['inspectCache']>({})
2527
export const hoverPathAtom = atom<JsonViewerState['hoverPath'] | null>(null)
2628
export const registryAtom = atom<TypeRegistryState['registry']>([])
2729

28-
export const getInspectCacheAtom = atom(
29-
(get) => get(inspectCacheAtom),
30-
(get, _set, { path, nestedIndex }) => {
30+
// TODO check: if memory leaks, add to last line of useEffect:
31+
// return () => { atomFamily.remove ... // Anything in here is fired on component unmount }
32+
export const getInspectCacheAtom = atomFamily(({ path, nestedIndex }) => atom(
33+
(get) => {
3134
const target = nestedIndex === undefined
3235
? path.join('.')
3336
: `${path.join('.')}[${nestedIndex}]nt`
3437
return get(inspectCacheAtom)[target]
3538
}
36-
)
39+
), deepEqual)
3740
export const setInspectCacheAtom = atom(
3841
(get) => get(inspectCacheAtom),
3942
(get, set, { path, action, nestedIndex }) => {
@@ -51,10 +54,9 @@ export const setInspectCacheAtom = atom(
5154
)
5255
export const setHoverAtom = atom(
5356
(get) => get(hoverPathAtom),
54-
(_get, set, { path, nestedIndex }) => set(hoverPathAtom, path
55-
? { path, nestedIndex }
56-
: null
57-
)
57+
(_get, set, { path, nestedIndex }) => {
58+
set(hoverPathAtom, path ? { path, nestedIndex } : null)
59+
}
5860
)
5961

6062
export const registryTypesAtom = atom(

src/type.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,14 @@ export type JsonViewerProps<T = unknown> = {
173173
displayObjectSize?: boolean
174174
}
175175

176+
export type HostPath = {
177+
path: Path
178+
nestedIndex?: number
179+
}
180+
176181
export type JsonViewerState<T = unknown> = {
177182
inspectCache: Record<string, boolean>
178-
hoverPath: { path: Path; nestedIndex?: number } | null
183+
hoverPath: HostPath | null
179184
indentWidth: number
180185
groupArraysAfterLength: number
181186
enableClipboard: boolean

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,7 @@ __metadata:
20022002
eslint-plugin-simple-import-sort: ^10.0.0
20032003
eslint-plugin-unused-imports: ^2.0.0
20042004
expect-type: ^0.15.0
2005+
fast-deep-equal: ^3.1.3
20052006
husky: ^8.0.3
20062007
jotai: ^1.13.0
20072008
jsdom: ^21.1.1

0 commit comments

Comments
 (0)