deno.land / x / jotai@v1.8.4 / src / optics / focusAtom.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import * as O from 'optics-ts'import { atom } from 'jotai'import type { SetStateAction, WritableAtom } from 'jotai'import { createMemoizeAtom } from '../utils/weakCache'
const memoizeAtom = createMemoizeAtom()
const isFunction = <T>(x: T): x is T & ((...args: any[]) => any) => typeof x === 'function'
type NonFunction<T> = [T] extends [(...args: any[]) => any] ? never : T
export function focusAtom<S, A, R extends void | Promise<void>>( baseAtom: WritableAtom<Promise<S>, NonFunction<S>, R>, callback: (optic: O.OpticFor<S>) => O.Prism<S, any, A>): WritableAtom<A | undefined, SetStateAction<A>, R>
export function focusAtom<S, A, R extends void | Promise<void>>( baseAtom: WritableAtom<Promise<S>, NonFunction<S>, R>, callback: (optic: O.OpticFor<S>) => O.Traversal<S, any, A>): WritableAtom<A[], SetStateAction<A>, R>
export function focusAtom<S, A, R extends void | Promise<void>>( baseAtom: WritableAtom<Promise<S>, NonFunction<S>, R>, callback: ( optic: O.OpticFor<S> ) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A>): WritableAtom<A, SetStateAction<A>, R>
export function focusAtom<S, A, R extends void | Promise<void>>( baseAtom: WritableAtom<S, NonFunction<S>, R>, callback: (optic: O.OpticFor<S>) => O.Prism<S, any, A>): WritableAtom<A | undefined, SetStateAction<A>, R>
export function focusAtom<S, A, R extends void | Promise<void>>( baseAtom: WritableAtom<S, NonFunction<S>, R>, callback: (optic: O.OpticFor<S>) => O.Traversal<S, any, A>): WritableAtom<A[], SetStateAction<A>, R>
export function focusAtom<S, A, R extends void | Promise<void>>( baseAtom: WritableAtom<S, NonFunction<S>, R>, callback: ( optic: O.OpticFor<S> ) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A>): WritableAtom<A, SetStateAction<A>, R>
export function focusAtom<S, A, R extends void | Promise<void>>( baseAtom: WritableAtom<S, NonFunction<S>, R>, callback: ( optic: O.OpticFor<S> ) => | O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A> | O.Prism<S, any, A> | O.Traversal<S, any, A>) { return memoizeAtom(() => { const focus = callback(O.optic<S>()) const derivedAtom = atom( (get) => getValueUsingOptic(focus, get(baseAtom)), (get, set, update: SetStateAction<A>) => { const newValueProducer = isFunction(update) ? O.modify(focus)(update) : O.set(focus)(update) return set(baseAtom, newValueProducer(get(baseAtom)) as NonFunction<S>) } ) return derivedAtom }, [baseAtom, callback])}
const getValueUsingOptic = <S, A>( focus: | O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A> | O.Prism<S, any, A> | O.Traversal<S, any, A>, bigValue: S) => { if (focus._tag === 'Traversal') { const values = O.collect(focus)(bigValue) return values } if (focus._tag === 'Prism') { const value = O.preview(focus)(bigValue) return value } const value = O.get(focus)(bigValue) return value}
jotai

Version Info

Tagged at
2 years ago