Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for Setter, Getter, AffineFold and Fold optics #18

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 105 additions & 21 deletions src/focusAtom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import type { Atom } from 'jotai';
ikeyan marked this conversation as resolved.
Show resolved Hide resolved
import { atom } from 'jotai/vanilla';
import type { SetStateAction, WritableAtom } from 'jotai/vanilla';
import * as O from 'optics-ts';
Expand All @@ -17,6 +18,21 @@ const isFunction = <T>(x: T): x is T & ((...args: any[]) => any) =>

type NonFunction<T> = [T] extends [(...args: any[]) => any] ? never : T;

type ModifiableLensLike<S, A> =
| 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>;

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

type LensLike<S, A> =
| SettableLensLike<S, A>
| O.Getter<S, A>
| O.AffineFold<S, A>
| O.Fold<S, A>;

// Pattern 1: Promise

export function focusAtom<S, A, R>(
Expand All @@ -36,6 +52,26 @@ export function focusAtom<S, A, R>(
) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A>,
): WritableAtom<Promise<A>, [SetStateAction<A>], R>;

export function focusAtom<S, A, R>(
baseAtom: WritableAtom<Promise<S>, [Promise<S>], R>,
callback: (optic: O.OpticFor_<S>) => O.Setter<S, any, A>,
): WritableAtom<Promise<void>, [NonFunction<A>], R>;

export function focusAtom<S, A, R>(
baseAtom: WritableAtom<Promise<S>, [Promise<S>], R>,
callback: (optic: O.OpticFor_<S>) => O.Getter<S, A>,
): Atom<Promise<A>>;

export function focusAtom<S, A, R>(
baseAtom: WritableAtom<Promise<S>, [Promise<S>], R>,
callback: (optic: O.OpticFor_<S>) => O.AffineFold<S, A>,
): Atom<Promise<A | undefined>>;

export function focusAtom<S, A, R>(
baseAtom: WritableAtom<Promise<S>, [Promise<S>], R>,
callback: (optic: O.OpticFor_<S>) => O.Fold<S, A>,
): Atom<Promise<A[]>>;

// Pattern 2: Promise with undefined type

export function focusAtom<S, A, R>(
Expand All @@ -55,6 +91,26 @@ export function focusAtom<S, A, R>(
) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A>,
): WritableAtom<Promise<A>, [SetStateAction<A>], R>;

export function focusAtom<S, A, R>(
baseAtom: WritableAtom<Promise<S | undefined>, [Promise<S>], R>,
callback: (optic: O.OpticFor_<S | undefined>) => O.Setter<S, any, A>,
): WritableAtom<Promise<void>, [NonFunction<A>], R>;

export function focusAtom<S, A, R>(
baseAtom: WritableAtom<Promise<S | undefined>, [Promise<S>], R>,
callback: (optic: O.OpticFor_<S | undefined>) => O.Getter<S, A>,
): Atom<Promise<A>>;

export function focusAtom<S, A, R>(
baseAtom: WritableAtom<Promise<S | undefined>, [Promise<S>], R>,
callback: (optic: O.OpticFor_<S | undefined>) => O.AffineFold<S, A>,
): Atom<Promise<A | undefined>>;

export function focusAtom<S, A, R>(
baseAtom: WritableAtom<Promise<S | undefined>, [Promise<S>], R>,
callback: (optic: O.OpticFor_<S | undefined>) => O.Fold<S, A>,
): Atom<Promise<A[]>>;

// Pattern 3: Default

export function focusAtom<S, A, R>(
Expand All @@ -74,6 +130,26 @@ export function focusAtom<S, A, R>(
) => 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>(
baseAtom: WritableAtom<S, [NonFunction<S>], R>,
callback: (optic: O.OpticFor_<S>) => O.Setter<S, any, A>,
): WritableAtom<void, [NonFunction<A>], R>;

export function focusAtom<S, A, R>(
baseAtom: WritableAtom<S, [NonFunction<S>], R>,
callback: (optic: O.OpticFor_<S>) => O.Getter<S, A>,
): Atom<A>;

export function focusAtom<S, A, R>(
baseAtom: WritableAtom<S, [NonFunction<S>], R>,
callback: (optic: O.OpticFor_<S>) => O.AffineFold<S, A>,
): Atom<A | undefined>;

export function focusAtom<S, A, R>(
baseAtom: WritableAtom<S, [NonFunction<S>], R>,
callback: (optic: O.OpticFor_<S>) => O.Fold<S, A>,
): Atom<A[]>;

// Pattern 4: Default with undefined type

export function focusAtom<S, A, R>(
Expand All @@ -93,18 +169,31 @@ export function focusAtom<S, A, R>(
) => 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>(
baseAtom: WritableAtom<S | undefined, [NonFunction<S>], R>,
callback: (optic: O.OpticFor_<S | undefined>) => O.Setter<S, any, A>,
): WritableAtom<void, [NonFunction<A>], R>;

export function focusAtom<S, A, R>(
baseAtom: WritableAtom<S | undefined, [NonFunction<S>], R>,
callback: (optic: O.OpticFor_<S | undefined>) => O.Getter<S, A>,
): Atom<A>;

export function focusAtom<S, A, R>(
baseAtom: WritableAtom<S | undefined, [NonFunction<S>], R>,
callback: (optic: O.OpticFor_<S | undefined>) => O.AffineFold<S, A>,
): Atom<A | undefined>;

export function focusAtom<S, A, R>(
baseAtom: WritableAtom<S | undefined, [NonFunction<S>], R>,
callback: (optic: O.OpticFor_<S | undefined>) => O.Fold<S, A>,
): Atom<A[]>;

// Implementation

export function focusAtom<S, A, R>(
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>,
callback: (optic: O.OpticFor_<S>) => LensLike<S, A>,
) {
return memo2(
() => {
Expand All @@ -118,8 +207,8 @@ export function focusAtom<S, A, R>(
},
(get, set, update: SetStateAction<A>) => {
const newValueProducer = isFunction(update)
? O.modify(focus)(update)
: O.set(focus)(update);
? O.modify(focus as ModifiableLensLike<S, A>)(update)
: O.set(focus as SettableLensLike<S, A>)(update);
const base = get(baseAtom);
return set(
baseAtom,
Expand All @@ -136,23 +225,18 @@ export function focusAtom<S, A, R>(
);
}

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 getValueUsingOptic = <S, A>(focus: LensLike<S, A>, bigValue: S) => {
if (focus._tag === 'Traversal' || focus._tag === 'Fold') {
const values = O.collect(focus)(bigValue);
return values;
}
if (focus._tag === 'Prism') {
if (focus._tag === 'Prism' || focus._tag === 'AffineFold') {
const value = O.preview(focus)(bigValue);
return value;
}
if (focus._tag === 'Setter') {
return undefined;
}
const value = O.get(focus)(bigValue);
return value;
};
Loading