Skip to content

Commit 6b4321a

Browse files
committed
allow AffineFold and Fold optics
1 parent b4f18ac commit 6b4321a

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

src/focusAtom.ts

+47-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ type ModifiableLensLike<S, A> =
2727

2828
type SettableLensLike<S, A> = ModifiableLensLike<S, A> | O.Setter<S, any, A>;
2929

30-
type LensLike<S, A> = SettableLensLike<S, A> | O.Getter<S, A>;
30+
type LensLike<S, A> =
31+
| SettableLensLike<S, A>
32+
| O.Getter<S, A>
33+
| O.AffineFold<S, A>
34+
| O.Fold<S, A>;
3135

3236
// Pattern 1: Promise
3337

@@ -58,6 +62,16 @@ export function focusAtom<S, A, R>(
5862
callback: (optic: O.OpticFor_<S>) => O.Getter<S, A>,
5963
): Atom<Promise<A>>;
6064

65+
export function focusAtom<S, A, R>(
66+
baseAtom: WritableAtom<Promise<S>, [Promise<S>], R>,
67+
callback: (optic: O.OpticFor_<S>) => O.AffineFold<S, A>,
68+
): Atom<Promise<A | undefined>>;
69+
70+
export function focusAtom<S, A, R>(
71+
baseAtom: WritableAtom<Promise<S>, [Promise<S>], R>,
72+
callback: (optic: O.OpticFor_<S>) => O.Fold<S, A>,
73+
): Atom<Promise<A[]>>;
74+
6175
// Pattern 2: Promise with undefined type
6276

6377
export function focusAtom<S, A, R>(
@@ -87,6 +101,16 @@ export function focusAtom<S, A, R>(
87101
callback: (optic: O.OpticFor_<S | undefined>) => O.Getter<S, A>,
88102
): Atom<Promise<A>>;
89103

104+
export function focusAtom<S, A, R>(
105+
baseAtom: WritableAtom<Promise<S | undefined>, [Promise<S>], R>,
106+
callback: (optic: O.OpticFor_<S | undefined>) => O.AffineFold<S, A>,
107+
): Atom<Promise<A | undefined>>;
108+
109+
export function focusAtom<S, A, R>(
110+
baseAtom: WritableAtom<Promise<S | undefined>, [Promise<S>], R>,
111+
callback: (optic: O.OpticFor_<S | undefined>) => O.Fold<S, A>,
112+
): Atom<Promise<A[]>>;
113+
90114
// Pattern 3: Default
91115

92116
export function focusAtom<S, A, R>(
@@ -116,6 +140,16 @@ export function focusAtom<S, A, R>(
116140
callback: (optic: O.OpticFor_<S>) => O.Getter<S, A>,
117141
): Atom<A>;
118142

143+
export function focusAtom<S, A, R>(
144+
baseAtom: WritableAtom<S, [NonFunction<S>], R>,
145+
callback: (optic: O.OpticFor_<S>) => O.AffineFold<S, A>,
146+
): Atom<A | undefined>;
147+
148+
export function focusAtom<S, A, R>(
149+
baseAtom: WritableAtom<S, [NonFunction<S>], R>,
150+
callback: (optic: O.OpticFor_<S>) => O.Fold<S, A>,
151+
): Atom<A[]>;
152+
119153
// Pattern 4: Default with undefined type
120154

121155
export function focusAtom<S, A, R>(
@@ -145,6 +179,16 @@ export function focusAtom<S, A, R>(
145179
callback: (optic: O.OpticFor_<S | undefined>) => O.Getter<S, A>,
146180
): Atom<A>;
147181

182+
export function focusAtom<S, A, R>(
183+
baseAtom: WritableAtom<S | undefined, [NonFunction<S>], R>,
184+
callback: (optic: O.OpticFor_<S | undefined>) => O.AffineFold<S, A>,
185+
): Atom<A | undefined>;
186+
187+
export function focusAtom<S, A, R>(
188+
baseAtom: WritableAtom<S | undefined, [NonFunction<S>], R>,
189+
callback: (optic: O.OpticFor_<S | undefined>) => O.Fold<S, A>,
190+
): Atom<A[]>;
191+
148192
// Implementation
149193

150194
export function focusAtom<S, A, R>(
@@ -182,11 +226,11 @@ export function focusAtom<S, A, R>(
182226
}
183227

184228
const getValueUsingOptic = <S, A>(focus: LensLike<S, A>, bigValue: S) => {
185-
if (focus._tag === 'Traversal') {
229+
if (focus._tag === 'Traversal' || focus._tag === 'Fold') {
186230
const values = O.collect(focus)(bigValue);
187231
return values;
188232
}
189-
if (focus._tag === 'Prism') {
233+
if (focus._tag === 'Prism' || focus._tag === 'AffineFold') {
190234
const value = O.preview(focus)(bigValue);
191235
return value;
192236
}

0 commit comments

Comments
 (0)