deno.land / x / jotai@v1.8.4 / src / utils / selectAtom.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { atom } from 'jotai'import type { Atom } from 'jotai'import { createMemoizeAtom } from './weakCache'
const memoizeAtom = createMemoizeAtom()
export function selectAtom<Value, Slice>( anAtom: Atom<Value>, selector: (v: Awaited<Value>) => Slice, equalityFn: (a: Slice, b: Slice) => boolean = Object.is): Atom<Slice> { return memoizeAtom(() => { // TODO we should revisit this for a better solution than refAtom const refAtom = atom(() => ({} as { prev?: Slice })) const derivedAtom = atom((get) => { const slice = selector(get(anAtom) as Awaited<Value>) const ref = get(refAtom) if ('prev' in ref && equalityFn(ref.prev as Slice, slice)) { return ref.prev as Slice } ref.prev = slice return slice }) return derivedAtom }, [anAtom, selector, equalityFn])}
jotai

Version Info

Tagged at
2 years ago