1+ import deepEqual from 'fast-deep-equal'
12import { atom } from 'jotai'
3+ import { atomFamily } from 'jotai/utils'
24
35import { lightColorspace } from './theme/base16'
46import type { JsonViewerState , TypeRegistryState } from './type'
@@ -20,19 +22,20 @@ export const collapseStringsAfterLengthAtom = atom<JsonViewerState['collapseStri
2022export const defaultInspectDepthAtom = atom < JsonViewerState [ 'defaultInspectDepth' ] | undefined > ( undefined )
2123export const objectSortKeysAtom = atom < JsonViewerState [ 'objectSortKeys' ] | undefined > ( undefined )
2224export const quotesOnKeysAtom = atom < JsonViewerState [ 'quotesOnKeys' ] | undefined > ( undefined )
23- export const inspectCacheAtom = atom < JsonViewerState [ 'inspectCache' ] | undefined > ( { } )
25+ export const inspectCacheAtom = atom < JsonViewerState [ 'inspectCache' ] > ( { } )
2426export const hoverPathAtom = atom < JsonViewerState [ 'hoverPath' ] | null > ( null )
2527export const registryAtom = atom < TypeRegistryState [ 'registry' ] > ( [ ] )
2628
27- export const getInspectCacheAtom = atom (
28- ( get ) => get ( inspectCacheAtom ) ,
29- ( get , _set , { path, nestedIndex } ) => {
29+ // TODO check: if memory leaks, add to last line of useEffect:
30+ // return () => { atomFamily.remove ... // Anything in here is fired on component unmount }
31+ export const getInspectCacheAtom = atomFamily ( ( { path, nestedIndex } ) => atom (
32+ ( get ) => {
3033 const target = nestedIndex === undefined
3134 ? path . join ( '.' )
3235 : `${ path . join ( '.' ) } [${ nestedIndex } ]nt`
3336 return get ( inspectCacheAtom ) [ target ]
3437 }
35- )
38+ ) , deepEqual )
3639export const setInspectCacheAtom = atom (
3740 ( get ) => get ( inspectCacheAtom ) ,
3841 ( get , set , { path, action, nestedIndex } ) => {
@@ -50,10 +53,9 @@ export const setInspectCacheAtom = atom(
5053)
5154export const setHoverAtom = atom (
5255 ( get ) => get ( hoverPathAtom ) ,
53- ( _get , set , { path, nestedIndex } ) => set ( hoverPathAtom , path
54- ? { path, nestedIndex }
55- : null
56- )
56+ ( _get , set , { path, nestedIndex } ) => {
57+ set ( hoverPathAtom , path ? { path, nestedIndex } : null )
58+ }
5759)
5860
5961export const registryTypesAtom = atom (
0 commit comments