1
+ import deepEqual from 'fast-deep-equal'
1
2
import { atom } from 'jotai'
3
+ import { atomFamily } from 'jotai/utils'
2
4
3
5
import { lightColorspace } from './theme/base16'
4
6
import type { JsonViewerState , TypeRegistryState } from './type'
@@ -20,19 +22,20 @@ export const collapseStringsAfterLengthAtom = atom<JsonViewerState['collapseStri
20
22
export const defaultInspectDepthAtom = atom < JsonViewerState [ 'defaultInspectDepth' ] | undefined > ( undefined )
21
23
export const objectSortKeysAtom = atom < JsonViewerState [ 'objectSortKeys' ] | undefined > ( undefined )
22
24
export const quotesOnKeysAtom = atom < JsonViewerState [ 'quotesOnKeys' ] | undefined > ( undefined )
23
- export const inspectCacheAtom = atom < JsonViewerState [ 'inspectCache' ] | undefined > ( { } )
25
+ export const inspectCacheAtom = atom < JsonViewerState [ 'inspectCache' ] > ( { } )
24
26
export const hoverPathAtom = atom < JsonViewerState [ 'hoverPath' ] | null > ( null )
25
27
export const registryAtom = atom < TypeRegistryState [ 'registry' ] > ( [ ] )
26
28
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 ) => {
30
33
const target = nestedIndex === undefined
31
34
? path . join ( '.' )
32
35
: `${ path . join ( '.' ) } [${ nestedIndex } ]nt`
33
36
return get ( inspectCacheAtom ) [ target ]
34
37
}
35
- )
38
+ ) , deepEqual )
36
39
export const setInspectCacheAtom = atom (
37
40
( get ) => get ( inspectCacheAtom ) ,
38
41
( get , set , { path, action, nestedIndex } ) => {
@@ -50,10 +53,9 @@ export const setInspectCacheAtom = atom(
50
53
)
51
54
export const setHoverAtom = atom (
52
55
( 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
+ }
57
59
)
58
60
59
61
export const registryTypesAtom = atom (
0 commit comments