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'
@@ -21,19 +23,20 @@ export const collapseStringsAfterLengthAtom = atom<JsonViewerState['collapseStri
21
23
export const defaultInspectDepthAtom = atom < JsonViewerState [ 'defaultInspectDepth' ] | undefined > ( undefined )
22
24
export const objectSortKeysAtom = atom < JsonViewerState [ 'objectSortKeys' ] | undefined > ( undefined )
23
25
export const quotesOnKeysAtom = atom < JsonViewerState [ 'quotesOnKeys' ] | undefined > ( undefined )
24
- export const inspectCacheAtom = atom < JsonViewerState [ 'inspectCache' ] | undefined > ( { } )
26
+ export const inspectCacheAtom = atom < JsonViewerState [ 'inspectCache' ] > ( { } )
25
27
export const hoverPathAtom = atom < JsonViewerState [ 'hoverPath' ] | null > ( null )
26
28
export const registryAtom = atom < TypeRegistryState [ 'registry' ] > ( [ ] )
27
29
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 ) => {
31
34
const target = nestedIndex === undefined
32
35
? path . join ( '.' )
33
36
: `${ path . join ( '.' ) } [${ nestedIndex } ]nt`
34
37
return get ( inspectCacheAtom ) [ target ]
35
38
}
36
- )
39
+ ) , deepEqual )
37
40
export const setInspectCacheAtom = atom (
38
41
( get ) => get ( inspectCacheAtom ) ,
39
42
( get , set , { path, action, nestedIndex } ) => {
@@ -51,10 +54,9 @@ export const setInspectCacheAtom = atom(
51
54
)
52
55
export const setHoverAtom = atom (
53
56
( 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
+ }
58
60
)
59
61
60
62
export const registryTypesAtom = atom (
0 commit comments